TelegramAktienBot/api/app/blueprints/keyword.py

100 lines
3.5 KiB
Python
Raw Normal View History

2022-04-12 07:50:24 +00:00
__author__ = "Florian Kaiser"
__copyright__ = "Copyright 2022, Project Aktienbot"
__credits__ = ["Florian Kaiser", "Florian Kellermann", "Linus Eickhof", "Kevin Pauer"]
__license__ = "GPL 3.0"
__version__ = "1.0.0"
import os
2022-03-17 08:26:25 +00:00
from apiflask import APIBlueprint, abort
2022-05-11 21:33:48 +00:00
from app.auth import auth
from app.db import database as db
from app.helper_functions import make_response, get_email_or_abort_401
from app.models import Keyword
2022-05-11 21:33:48 +00:00
from app.schema import KeywordResponseSchema, KeywordSchema, DeleteSuccessfulSchema
2022-03-17 08:26:25 +00:00
keyword_blueprint = APIBlueprint('keyword', __name__, url_prefix='/api')
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
@keyword_blueprint.route('/keyword', methods=['POST'])
2022-03-17 08:26:25 +00:00
@keyword_blueprint.output(KeywordResponseSchema(many=True), 200)
@keyword_blueprint.input(schema=KeywordSchema)
@keyword_blueprint.auth_required(auth)
@keyword_blueprint.doc(summary="Add new keyword", description="Adds new keyword for current user")
def add_keyword(data):
2022-03-27 15:23:33 +00:00
email = get_email_or_abort_401()
if not check_if_keyword_data_exists(data):
abort(400, message="Keyword missing")
2022-03-17 08:26:25 +00:00
key = data['keyword']
2022-04-12 09:36:23 +00:00
# Check if keyword already exists
2022-03-27 15:23:33 +00:00
check_keyword = db.session.query(Keyword).filter_by(keyword=key, email=email).first()
if check_keyword is None:
2022-04-12 09:36:23 +00:00
# Keyword doesn't exist yet for this user -> add it
new_keyword = Keyword(
2022-03-27 15:23:33 +00:00
email=email,
keyword=key
)
db.session.add(new_keyword)
db.session.commit()
2022-03-22 10:21:39 +00:00
return make_response(new_keyword.as_dict(), 200, "Successfully added keyword")
else:
2022-03-17 08:26:25 +00:00
abort(500, message="Keyword already exist for this user")
@keyword_blueprint.route('/keyword', methods=['DELETE'])
2022-03-17 08:26:25 +00:00
@keyword_blueprint.output(DeleteSuccessfulSchema, 200)
@keyword_blueprint.input(schema=KeywordSchema)
@keyword_blueprint.auth_required(auth)
@keyword_blueprint.doc(summary="Removes existing keyword", description="Removes existing keyword for current user")
def remove_keyword(data):
2022-03-27 15:23:33 +00:00
email = get_email_or_abort_401()
2022-03-17 08:26:25 +00:00
2022-04-12 09:36:23 +00:00
# Check if request data is valid
if not check_if_keyword_data_exists(data):
abort(400, message="Keyword missing")
2022-04-12 09:36:23 +00:00
# Check if keyword exists
check_keyword = db.session.query(Keyword).filter_by(keyword=data['keyword'], email=email).first()
2022-03-27 15:23:33 +00:00
if check_keyword is None:
return abort(500, "Keyword doesn't exist for this user")
2022-03-27 15:23:33 +00:00
else:
2022-04-12 09:36:23 +00:00
# Keyword exists -> delete it
db.session.query(Keyword).filter_by(keyword=data['keyword'], email=email).delete()
2022-03-27 15:23:33 +00:00
db.session.commit()
return make_response({}, 200, "Successfully removed keyword")
@keyword_blueprint.route('/keywords', methods=['GET'])
2022-03-17 08:26:25 +00:00
@keyword_blueprint.output(KeywordResponseSchema(many=True), 200)
@keyword_blueprint.auth_required(auth)
@keyword_blueprint.doc(summary="Returns all keywords", description="Returns all keywords for current user")
def get_keywords():
2022-03-27 15:23:33 +00:00
email = get_email_or_abort_401()
return_keywords = []
2022-03-27 15:23:33 +00:00
keywords = db.session.query(Keyword).filter_by(email=email).all()
2022-04-12 09:36:23 +00:00
# If no keywords exist for this user -> return empty list
# Otherwise iterate over all keywords, convert them to json and add them to the return list
if keywords is not None:
for row in keywords:
return_keywords.append(row.as_dict())
2022-03-22 10:21:39 +00:00
return make_response(return_keywords, 200, "Successfully loaded keywords")
2022-03-17 08:26:25 +00:00
def check_if_keyword_data_exists(data):
if "keyword" not in data:
return False
2022-03-17 08:26:25 +00:00
if data['keyword'] == "" or data['keyword'] is None:
return False
return True