2017-07-13 19:46:51 -04:00
from flask import render_template , abort , redirect , url_for , abort , flash , request , current_app , make_response , g
from flask_login import login_required , login_user , logout_user , current_user
from flask_sqlalchemy import get_debug_queries
from . import smanager
2017-07-14 09:04:27 -04:00
from . forms import ActivateForm
2017-07-13 19:46:51 -04:00
from . . import db
from . . email import send_email
2017-07-15 20:48:18 -04:00
from . . models import User , Permission , Service , Transaction
2017-07-13 19:46:51 -04:00
from . . decorators import admin_required , permission_required
from datetime import datetime , timedelta , date , time
2017-07-15 20:48:18 -04:00
from dateutil . relativedelta import relativedelta
2017-07-13 19:46:51 -04:00
@smanager.after_app_request
def after_request ( response ) :
for query in get_debug_queries ( ) :
if query . duration > = current_app . config [ ' SLOW_DB_QUERY_TIME ' ] :
current_app . logger . warning ( ' Slow query: %s \n Parameters: %s \n Duration: %f s \n Context: %s \n ' % ( query . statement , query . parameters , query . duration , query . context ) )
return response
2017-07-28 21:31:44 -04:00
@smanager.route ( ' /requestservice ' , methods = [ ' GET ' , ' POST ' ] )
@login_required
def requestservice ( ) :
abort ( 403 )
2017-07-13 19:46:51 -04:00
@smanager.route ( ' /activate/<int:itemid> ' , methods = [ ' GET ' , ' POST ' ] )
@login_required
def activate ( itemid = 0 ) :
result = current_user . inv_services . all ( )
inventory = [ ]
for invcls in result :
inventory . extend ( [ invcls . pid ] )
2017-07-14 09:00:04 -04:00
if current_user . is_administrator ( ) :
2017-07-15 20:48:18 -04:00
current_app . logger . warning ( ' [ADMIN] Access override for service id: {} ' . format ( itemid ) )
2017-07-14 09:00:04 -04:00
elif not itemid in inventory :
2017-07-15 20:48:18 -04:00
current_app . logger . error ( ' [ {} ] Access violation with service id: {} ' . format ( current_user . email , itemid ) )
2017-07-13 19:46:51 -04:00
abort ( 404 )
2017-07-15 20:48:18 -04:00
service = Service . query . filter_by ( pid = itemid ) . first ( )
2017-07-20 19:30:09 -04:00
if service . enabled == True and service . warning == False :
abort ( 404 )
ppm = float ( service . price )
2017-07-15 20:48:18 -04:00
form = ActivateForm ( period = int ( service . period ) )
owner = service . owner
if owner . confirmed and form . validate_on_submit ( ) :
2017-07-20 19:30:09 -04:00
total = ppm * form . period . data
if owner . wallet < total :
flash ( ' Activation costs {} {} . Insufficient Funds ' . format ( total , owner . currency ) )
2017-07-15 20:48:18 -04:00
return redirect ( url_for ( ' uinvoice.transactions ' ) )
current_app . logger . info ( ' [ {} ] Charge service: {} ' . format ( owner . email , service . description ) )
today = datetime . utcnow ( )
expiry = today + relativedelta ( today , months = + ( form . period . data ) )
daysleft = expiry - today
extradays = relativedelta ( today , days = + ( service . daysleft ) )
service . date_last_charge = today + extradays
service . period = form . period . data
2017-07-20 19:30:09 -04:00
service . daysleft = daysleft . days + extradays . days
2017-07-15 20:48:18 -04:00
service . warning = False
service . enabled = True
db . session . commit ( )
2017-12-26 20:47:26 -05:00
email_content = ' Service {} is activated for {} month(s). It will expire at {} ' . format ( str ( service . description ) , form . period . data , str ( ( expiry + extradays ) . strftime ( ' %c ' ) ) )
send_email ( current_app . config [ ' MAIL_USERNAME ' ] , str ( email_content ) , ' email/adm_logger ' , user = owner , content = str ( email_content ) )
send_email ( str ( owner . email ) , str ( email_content ) , ' email/client_logger ' , content = str ( email_content ) )
transaction = Transaction ( user_id = int ( owner . pid ) , description = str ( email_content ) , value = - total )
2017-07-15 20:48:18 -04:00
db . session . add ( transaction )
db . session . commit ( )
2017-09-09 02:32:04 -04:00
owner . wallet = owner . wallet - total
2017-07-15 20:48:18 -04:00
db . session . commit ( )
2017-12-26 20:54:50 -05:00
flash ( ' Deployment {} activated for {} month(s) ' . format ( str ( service . description ) , form . period . data ) )
2017-07-15 20:48:18 -04:00
if owner . is_administrator :
2017-07-25 10:33:23 -04:00
return redirect ( url_for ( ' admin.list_services ' ) )
2017-07-15 20:48:18 -04:00
else :
2018-01-11 08:19:59 -05:00
return redirect ( url_for ( ' panel.dashboard ' ) )
2017-07-24 06:33:33 -04:00
return render_template ( ' smanager/activate.html ' , form = form , service = service , ppm = ppm , total = ( ppm * service . period ) , currency = owner . currency )
2017-07-13 19:46:51 -04:00