TelegramAktienBot/api/app/blueprints/transactions.py

128 lines
3.6 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
2022-03-14 17:13:16 +00:00
import datetime
2022-04-05 13:08:57 +00:00
import os
2022-03-14 17:13:16 +00:00
2022-03-17 08:26:25 +00:00
from apiflask import abort, APIBlueprint
2022-04-05 13:08:57 +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 Transaction
from app.schema import TransactionSchema, TransactionResponseSchema
2022-03-14 17:13:16 +00:00
2022-03-17 08:26:25 +00:00
transaction_blueprint = APIBlueprint('transaction', __name__, url_prefix='/api')
2022-03-14 17:13:16 +00:00
__location__ = os.path.realpath(os.path.join(os.getcwd(), os.path.dirname(__file__)))
@transaction_blueprint.route('/transaction', methods=['POST'])
2022-03-27 15:23:33 +00:00
@transaction_blueprint.output(TransactionResponseSchema(), 200)
2022-03-17 08:26:25 +00:00
@transaction_blueprint.input(schema=TransactionSchema)
@transaction_blueprint.auth_required(auth)
@transaction_blueprint.doc(summary="Adds new transaction", description="Adds new transaction for current user")
def add_transaction(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, "ISIN missing")
if not check_if_time_data_exists(data):
abort(400, "Time missing")
if not check_if_comment_data_exists(data):
abort(400, "Comment missing")
if not check_if_count_data_exists(data):
abort(400, "Count missing")
if not check_if_price_data_exists(data):
abort(400, "Price missing")
2022-03-17 08:26:25 +00:00
2022-04-12 09:36:23 +00:00
# Add transaction
2022-03-17 08:26:25 +00:00
new_transaction = Transaction(
2022-03-27 15:23:33 +00:00
email=email,
isin=data['isin'],
comment=data['comment'],
2022-03-17 08:26:25 +00:00
time=datetime.datetime.strptime(data['time'], '%Y-%m-%dT%H:%M:%S.%fZ'),
count=data['count'],
price=data['price']
2022-03-14 17:13:16 +00:00
)
2022-03-17 08:26:25 +00:00
db.session.add(new_transaction)
2022-03-14 17:13:16 +00:00
db.session.commit()
2022-03-22 10:21:39 +00:00
return make_response(new_transaction.as_dict(), 200, "Successfully added transaction")
2022-03-14 17:13:16 +00:00
@transaction_blueprint.route('/transactions', methods=['GET'])
2022-03-17 08:26:25 +00:00
@transaction_blueprint.output(TransactionSchema(), 200)
@transaction_blueprint.auth_required(auth)
@transaction_blueprint.doc(summary="Returns all transactions", description="Returns all transactions for current user")
2022-03-14 17:13:16 +00:00
def get_transaction():
2022-03-27 15:23:33 +00:00
email = get_email_or_abort_401()
2022-03-14 17:13:16 +00:00
return_transactions = []
2022-04-12 09:36:23 +00:00
# Get all transactions
2022-05-12 16:19:00 +00:00
transactions = db.session.query(Transaction).filter_by(email=email).order_by(Transaction.time.desc()).all()
2022-03-14 17:13:16 +00:00
2022-04-12 09:36:23 +00:00
# Iterate over transactions and add them to return_transactions
2022-03-14 17:13:16 +00:00
if transactions is not None:
for row in transactions:
return_transactions.append(row.as_dict())
2022-03-22 10:21:39 +00:00
return make_response(return_transactions, 200, "Successfully loaded transactions")
2022-03-17 08:26:25 +00:00
def check_if_isin_data_exists(data):
if "isin" not in data:
return False
2022-03-17 08:26:25 +00:00
if data['isin'] == "" or data['isin'] is None:
return False
return True
2022-03-17 08:26:25 +00:00
def check_if_time_data_exists(data):
2022-03-17 08:26:25 +00:00
if "time" not in data:
return False
2022-03-17 08:26:25 +00:00
if data['time'] == "" or data['time'] is None:
return False
2022-03-17 08:26:25 +00:00
return True
def check_if_comment_data_exists(data):
if "comment" not in data:
return False
if data['comment'] == "" or data['comment'] is None:
return False
return True
def check_if_count_data_exists(data):
2022-03-17 08:26:25 +00:00
if "count" not in data:
return False
2022-03-17 08:26:25 +00:00
if data['count'] == "" or data['count'] is None:
return False
return True
2022-03-17 08:26:25 +00:00
def check_if_price_data_exists(data):
2022-03-17 08:26:25 +00:00
if "price" not in data:
return False
2022-03-17 08:26:25 +00:00
if data['price'] == "" or data['price'] is None:
return False
return True