TelegramAktienBot/api/api_blueprint_shares.py

84 lines
2.9 KiB
Python
Raw Normal View History

import os
2022-03-17 08:26:25 +00:00
from apiflask import APIBlueprint, abort
from flask import jsonify
2022-03-17 08:26:25 +00:00
from auth import auth
2022-03-14 16:36:38 +00:00
from db import db
2022-03-17 08:26:25 +00:00
from helper_functions import get_user_id_from_username, get_username_or_abort_401
2022-03-14 16:36:38 +00:00
from models import Share
2022-03-17 08:26:25 +00:00
from scheme import SymbolSchema, SymbolResponseSchema, DeleteSuccessfulSchema
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):
username = get_username_or_abort_401()
2022-03-17 08:26:25 +00:00
check_if_symbol_data_exists(data)
symbol = data['symbol']
check_share = db.session.query(Share).filter_by(symbol=symbol, user_id=get_user_id_from_username(username)).first()
if check_share is None:
# Keyword doesn't exist yet for this user
new_symbol = Share(
user_id=get_user_id_from_username(username),
symbol=symbol
)
db.session.add(new_symbol)
db.session.commit()
return jsonify({"status": 200, "text": "Successfully added symbol", "data": new_symbol.as_dict()})
else:
return jsonify({"status": 500, "text": "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=SymbolSchema)
@shares_blueprint.auth_required(auth)
@shares_blueprint.doc(summary="Removes existing symbol", description="Removes existing symbol for current user")
def remove_symbol(data):
username = get_username_or_abort_401()
check_if_symbol_data_exists(data)
2022-03-17 08:26:25 +00:00
symbol = data['symbol']
db.session.query(Share).filter_by(symbol=symbol, user_id=get_user_id_from_username(username)).delete()
db.session.commit()
2022-03-17 08:26:25 +00:00
return jsonify({"status": 200, "text": "Successfully removed symbol", "data": {}})
@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-17 08:26:25 +00:00
username = get_username_or_abort_401()
return_symbols = []
symbols = db.session.query(Share).filter_by(user_id=get_user_id_from_username(username)).all()
if symbols is not None:
for row in symbols:
return_symbols.append(row.as_dict())
return jsonify({"status": 200, "text": "Successfully loaded symbols", "data": return_symbols})
2022-03-17 08:26:25 +00:00
def check_if_symbol_data_exists(data):
if "symbol" not in data:
abort(400, message="Symbol missing")
if data['symbol'] == "" or data['symbol'] is None:
abort(400, message="Symbol missing")