TelegramAktienBot/api/app/blueprints/shares.py

113 lines
3.8 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.1"
2022-04-12 07:50:24 +00:00
import os
2022-03-17 08:26:25 +00:00
from apiflask import APIBlueprint, abort
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 Share
from app.schema import SymbolSchema, SymbolResponseSchema, DeleteSuccessfulSchema, SymbolRemoveSchema
2022-03-17 08:26:25 +00:00
shares_blueprint = APIBlueprint('share', __name__, url_prefix='/api')
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
@shares_blueprint.route('/share', methods=['POST'])
2022-03-17 08:26:25 +00:00
@shares_blueprint.output(SymbolResponseSchema(many=True), 200)
@shares_blueprint.input(schema=SymbolSchema)
@shares_blueprint.auth_required(auth)
@shares_blueprint.doc(summary="Add new symbol", description="Adds new symbol for current user")
def add_symbol(data):
2022-03-27 15:23:33 +00:00
email = get_email_or_abort_401()
2022-04-12 09:36:23 +00:00
# Check if required data is available
if not check_if_isin_data_exists(data):
abort(400, message="ISIN missing")
if not check_if_comment_data_exists(data):
abort(400, message="Comment missing")
2022-03-17 08:26:25 +00:00
2022-04-12 09:36:23 +00:00
# Check if share already exists
check_share = db.session.query(Share).filter_by(isin=data['isin'], email=email).first()
if check_share is None:
2022-04-12 09:36:23 +00:00
# Keyword doesn't exist yet for this user -> add it
new_symbol = Share(
2022-03-27 15:23:33 +00:00
email=email,
isin=data['isin'],
comment=data['comment']
)
db.session.add(new_symbol)
db.session.commit()
2022-03-22 10:21:39 +00:00
return make_response(new_symbol.as_dict(), 200, "Successfully added symbol")
else:
abort(500, "Symbol already exist for this user")
@shares_blueprint.route('/share', methods=['DELETE'])
2022-03-17 08:26:25 +00:00
@shares_blueprint.output(DeleteSuccessfulSchema, 200)
@shares_blueprint.input(schema=SymbolRemoveSchema)
2022-03-17 08:26:25 +00:00
@shares_blueprint.auth_required(auth)
@shares_blueprint.doc(summary="Removes existing symbol", description="Removes existing symbol for current user")
def remove_symbol(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 required data is available
if not check_if_isin_data_exists(data):
abort(400, message="ISIN missing")
2022-04-12 09:36:23 +00:00
# Check if share exists
check_share = db.session.query(Share).filter_by(isin=data['isin'], email=email).first()
2022-03-27 15:23:33 +00:00
if check_share is None:
abort(500, "Symbol doesn't exist for this user")
2022-03-27 15:23:33 +00:00
else:
2022-04-12 09:36:23 +00:00
# Delete share
db.session.query(Share).filter_by(isin=data['isin'], email=email).delete()
2022-03-27 15:23:33 +00:00
db.session.commit()
return make_response({}, 200, "Successfully removed symbol")
@shares_blueprint.route('/shares', methods=['GET'])
2022-03-17 08:26:25 +00:00
@shares_blueprint.output(SymbolResponseSchema(many=True), 200)
@shares_blueprint.auth_required(auth)
@shares_blueprint.doc(summary="Returns all symbols", description="Returns all symbols for current user")
def get_symbol():
2022-03-27 15:23:33 +00:00
email = get_email_or_abort_401()
return_symbols = []
2022-03-27 15:23:33 +00:00
symbols = db.session.query(Share).filter_by(email=email).all()
2022-04-12 09:36:23 +00:00
# If no shares exist for this user -> return empty list
# Otherwise iterate over all shares, convert them to json and add them to the return list
if symbols is not None:
for row in symbols:
return_symbols.append(row.as_dict())
2022-03-22 10:21:39 +00:00
return make_response(return_symbols, 200, "Successfully loaded symbols")
2022-03-17 08:26:25 +00:00
def check_if_isin_data_exists(data):
if "isin" not in data:
return False
if data['isin'] == "" or data['isin'] is None:
return False
return True
def check_if_comment_data_exists(data):
if "comment" not in data:
return False
2022-03-17 08:26:25 +00:00
if data['comment'] == "" or data['comment'] is None:
return False
return True