proxadmin/app/smanager/routes.py

77 lines
3.2 KiB
Python
Raw Normal View History

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
from .. import db
from ..email import send_email
2017-07-15 20:48:18 -04:00
from ..models import User, Permission, Service, Transaction
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
@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\nParameters: %s\nDuration: %fs\nContext: %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)
@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))
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-08-01 07:21:22 -04:00
transaction = Transaction(user_id=int(owner.pid), description='Deployment {} activated for {} month(s)'.format(str(service.description), form.period.data), value=-total)
2017-07-15 20:48:18 -04:00
db.session.add(transaction)
db.session.commit()
owner.wallet = owner.wallet - total
2017-07-15 20:48:18 -04:00
db.session.commit()
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:
return redirect(url_for('main.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)