2017-03-08 13:53:09 -05:00
from flask import render_template , redirect , request , url_for , flash , session , abort , current_app
from flask_login import login_required , login_user , logout_user , current_user
from sqlalchemy import desc
from . import uinvoice
2017-06-04 11:22:59 -04:00
from . forms import PaymentForm
2017-03-08 13:53:09 -05:00
from . . email import send_email
from . . import db
2017-06-10 23:26:10 -04:00
from . . models import User , Transaction
2017-03-08 13:53:09 -05:00
2017-06-10 23:26:10 -04:00
@uinvoice.route ( ' /transactions ' , methods = [ ' GET ' ] )
2017-03-08 13:53:09 -05:00
@login_required
2017-06-10 23:26:10 -04:00
def transactions ( ) :
page = { ' title ' : ' Payment Transaction ' }
2017-06-13 19:24:39 -04:00
cuser = current_user
2017-06-25 10:11:52 -04:00
transactions = Transaction . query . filter_by ( user_id = cuser . pid ) . order_by ( Transaction . date_created . desc ( ) ) . limit ( 100 )
2017-06-13 19:24:39 -04:00
labelslist = [ ' today ' ]
translist = [ cuser . wallet ]
prevvalue = cuser . wallet
2017-06-10 23:26:10 -04:00
for tr in transactions :
2017-06-13 19:24:39 -04:00
labelslist . insert ( 0 , str ( tr . date_created . strftime ( ' %d . % m ' ) ) )
translist . insert ( 0 , prevvalue - tr . value )
prevvalue - = tr . value
if len ( labelslist ) < = 1 :
labelslist . insert ( 0 , ' before ' )
translist . insert ( 0 , 0 )
2017-03-08 13:53:09 -05:00
2017-06-13 19:24:39 -04:00
#current_app.logger.info('[{}] transactions: {} {} '.format(cuser.email, translist, labelslist))
2017-06-25 10:11:52 -04:00
return render_template ( ' uinvoice/transactions.html ' , transactions = transactions , translist = translist , labelslist = labelslist , cuser = cuser )
2017-03-08 13:53:09 -05:00
@uinvoice.route ( ' /order/<int:document_id> ' , methods = [ ' GET ' , ' POST ' ] )
@login_required
def order ( document_id ) :
page = { ' title ' : ' Preview ' + str ( document_id ) }
2017-06-10 23:26:10 -04:00
order = Transaction . query . filter_by ( pid = document_id ) . first ( )
2017-03-08 13:53:09 -05:00
db . session . commit ( )
#check if document_id is owned by you.
try :
if order . user_id != current_user . pid :
print ( ' WARNING: user {} violates order {} ' . format ( current_user . pid , order . pid ) )
abort ( 404 )
except :
abort ( 404 )
form = PaymentForm ( )
if form . validate_on_submit ( ) :
#TODO: contact payment processor
send_email ( current_app . config [ ' MAIL_USERNAME ' ] , current_user . email + ' plati ' + str ( order . units * order . unitvalue ) + ' v koshnicata. ' , ' uinvoice/email/adm_payment ' , user = current_user , order = order )
order . paid = True
return redirect ( url_for ( ' uinvoice.documents ' ) )
#except:
# abort(404)
return render_template ( ' uinvoice/invoice.html ' , page = page , form = form , document = invoice , document_id = document_id )