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 "
2022-04-19 06:20:28 +00:00
__version__ = " 1.0.1 "
2022-04-12 07:50:24 +00:00
2022-03-14 22:40:36 +00:00
import os
2022-03-17 08:26:25 +00:00
from apiflask import APIBlueprint
2022-04-05 13:30:30 +00:00
from app . auth import auth
2022-03-30 08:46:54 +00:00
from app . db import database as db
from app . helper_functions import make_response , get_email_or_abort_401
2022-04-07 10:40:55 +00:00
from app . models import SharePrice
2022-04-05 13:30:30 +00:00
from app . schema import PortfolioResponseSchema
2022-03-14 22:40:36 +00:00
2022-03-17 08:26:25 +00:00
portfolio_blueprint = APIBlueprint ( ' portfolio ' , __name__ , url_prefix = ' /api ' )
2022-03-14 22:40:36 +00:00
__location__ = os . path . realpath ( os . path . join ( os . getcwd ( ) , os . path . dirname ( __file__ ) ) )
@portfolio_blueprint.route ( ' /portfolio ' , methods = [ ' GET ' ] )
2022-03-27 15:23:33 +00:00
@portfolio_blueprint.output ( PortfolioResponseSchema ( many = True ) , 200 )
2022-03-17 08:26:25 +00:00
@portfolio_blueprint.auth_required ( auth )
@portfolio_blueprint.doc ( summary = " Returns portfolio " , description = " Returns all shares of current user " )
2022-03-14 22:40:36 +00:00
def get_portfolio ( ) :
2022-03-27 15:23:33 +00:00
email = get_email_or_abort_401 ( )
2022-03-14 22:40:36 +00:00
2022-03-27 15:23:33 +00:00
return_portfolio = [ ]
2022-04-12 09:36:23 +00:00
# Get all transactions of current user
2022-04-19 06:20:28 +00:00
transactions = db . session . execute ( " SELECT isin, comment, SUM(count), SUM(price), MAX(time) FROM `transactions` WHERE email = ' " + email + " ' GROUP BY isin, comment; " ) . all ( )
2022-03-14 22:40:36 +00:00
2022-04-12 09:36:23 +00:00
# If there are no transactions, return empty portfolio
# Otherwise calculate portfolio
2022-03-14 22:40:36 +00:00
if transactions is not None :
for row in transactions :
2022-05-12 15:41:55 +00:00
if row [ 2 ] == 0 :
2022-05-12 15:25:10 +00:00
continue
else :
data = {
" isin " : row [ 0 ] ,
" comment " : row [ 1 ] ,
" count " : row [ 2 ] ,
# "calculated_price": row[3],
" last_transaction " : row [ 4 ] ,
' current_price ' : 0
}
# Add current share value to portfolio
query_share_price = db . session . query ( SharePrice ) . filter_by ( isin = row [ 0 ] ) . order_by ( SharePrice . date . desc ( ) ) . first ( )
if query_share_price is not None :
data [ ' current_price ' ] = query_share_price . as_dict ( ) [ ' price ' ]
return_portfolio . append ( data )
2022-03-14 22:40:36 +00:00
2022-03-22 10:21:39 +00:00
return make_response ( return_portfolio , 200 , " Successfully loaded symbols " )