2022-03-14 16:10:00 +00:00
|
|
|
import os
|
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
from apiflask import APIBlueprint, abort
|
2022-03-14 16:10:00 +00:00
|
|
|
|
2022-03-30 08:46:54 +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 Share
|
|
|
|
from app.schema import SymbolSchema, SymbolResponseSchema, DeleteSuccessfulSchema
|
2022-03-14 16:10:00 +00:00
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
shares_blueprint = APIBlueprint('share', __name__, url_prefix='/api')
|
2022-03-14 16:10:00 +00:00
|
|
|
__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-03-14 21:57:03 +00:00
|
|
|
|
2022-03-30 08:46:54 +00:00
|
|
|
if not check_if_symbol_data_exists(data):
|
|
|
|
abort(400, message="Symbol missing")
|
2022-03-17 08:26:25 +00:00
|
|
|
|
|
|
|
symbol = data['symbol']
|
2022-03-14 16:10:00 +00:00
|
|
|
|
2022-03-27 15:23:33 +00:00
|
|
|
check_share = db.session.query(Share).filter_by(symbol=symbol, email=email).first()
|
2022-03-14 16:10:00 +00:00
|
|
|
if check_share is None:
|
|
|
|
# Keyword doesn't exist yet for this user
|
|
|
|
new_symbol = Share(
|
2022-03-27 15:23:33 +00:00
|
|
|
email=email,
|
2022-03-14 16:10:00 +00:00
|
|
|
symbol=symbol
|
|
|
|
)
|
|
|
|
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")
|
2022-03-14 16:10:00 +00:00
|
|
|
else:
|
2022-03-30 08:46:54 +00:00
|
|
|
abort(500, "Symbol already exist for this user")
|
2022-03-14 16:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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):
|
2022-03-27 15:23:33 +00:00
|
|
|
email = get_email_or_abort_401()
|
2022-03-17 08:26:25 +00:00
|
|
|
|
2022-03-30 08:46:54 +00:00
|
|
|
if not check_if_symbol_data_exists(data):
|
|
|
|
abort(400, message="Symbol missing")
|
2022-03-14 21:57:03 +00:00
|
|
|
|
2022-03-17 08:26:25 +00:00
|
|
|
symbol = data['symbol']
|
2022-03-14 16:10:00 +00:00
|
|
|
|
2022-03-27 15:23:33 +00:00
|
|
|
check_share = db.session.query(Share).filter_by(symbol=symbol, email=email).first()
|
2022-03-14 16:10:00 +00:00
|
|
|
|
2022-03-27 15:23:33 +00:00
|
|
|
if check_share is None:
|
2022-03-30 08:46:54 +00:00
|
|
|
abort(500, "Symbol doesn't exist for this user")
|
2022-03-27 15:23:33 +00:00
|
|
|
else:
|
|
|
|
db.session.query(Share).filter_by(symbol=symbol, email=email).delete()
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return make_response({}, 200, "Successfully removed symbol")
|
2022-03-14 16:10:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
@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")
|
2022-03-14 16:10:00 +00:00
|
|
|
def get_symbol():
|
2022-03-27 15:23:33 +00:00
|
|
|
email = get_email_or_abort_401()
|
2022-03-14 16:10:00 +00:00
|
|
|
|
|
|
|
return_symbols = []
|
2022-03-27 15:23:33 +00:00
|
|
|
symbols = db.session.query(Share).filter_by(email=email).all()
|
2022-03-14 16:10:00 +00:00
|
|
|
|
|
|
|
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_symbol_data_exists(data):
|
|
|
|
if "symbol" not in data:
|
2022-03-30 08:46:54 +00:00
|
|
|
return False
|
2022-03-17 08:26:25 +00:00
|
|
|
|
|
|
|
if data['symbol'] == "" or data['symbol'] is None:
|
2022-03-30 08:46:54 +00:00
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|