activate functions amd templates

This commit is contained in:
deflax 2017-07-15 07:13:57 +03:00
parent a65bdd5f2e
commit 7bfdd479f2
4 changed files with 45 additions and 20 deletions

View file

@ -1,6 +1,6 @@
{% extends "base.html" %}
{% block title %}Activate{% endblock %}
{% block title %}Activatei deployment #{{ deploy.machine_id }}{% endblock %}
{% block page_content %}
@ -11,12 +11,19 @@
<div class="panel-heading">Activate</div>
<div class="panel-body">
<form method="POST" action"{{ url_for('vmanager.activate', itemid=itemid) }}">
<p>{{ form.period.label }}<br />{{ form.period }}<br />
<form method="POST" action"{{ url_for('vmanager.activate', itemid=deploy.machine_id) }}">
<h3>
CPU x {{ deploy.machine_cpu }} cores = {{ cpu_cost }}<br />
Memory x {{ deploy.machine_mem }} MB = {{ mem_cost }}<br />
Storage x {{ deploy.machine_hdd }} GB = {{ hdd_cost }}<br />
{{ form.period.label }} {{ form.period }}<br />
{% for error in form.period.errors %}
{{ error }}<br />
{% endfor %}
</p>
---<br />
Total = {{ total }}
</h3>
<p>
{{ form.csrf_token() }}
{{ form.submit }}

View file

@ -22,5 +22,5 @@ class DeployForm(FlaskForm):
submit = SubmitField('Deploy')
class ActivateForm(FlaskForm):
period = SelectField('Deploy Period', choices=[('1 Month', 1), ('3 Months', 3), ('6 Months', 6), ('1 Year', 12), ('2 Years', 24)])
period = SelectField('Deploy Period', choices=[(1, '1 Month'), (3, '3 Months'), (6, '6 Months'), (12, '1 Year'), (24, '2 Years')], coerce=int)
submit = SubmitField('Activate')

View file

@ -6,13 +6,14 @@ from . import vmanager
from .forms import DeployForm, ActivateForm
from .. import db
from ..email import send_email
from ..models import User, Permission, Deployment, Service, Region, Address, Domain, contact_proxmaster
from ..models import User, Permission, Transaction, Deployment, Service, Region, Address, Domain, contact_proxmaster
from ..decorators import admin_required, permission_required
import base64
import string
import random
from datetime import datetime, timedelta, date, time
from dateutil.relativedelta import relativedelta
import ast
def randstr(n):
@ -120,27 +121,49 @@ def deploy(product_id=None):
@vmanager.route('/activate/<int:itemid>', methods=['GET', 'POST'])
@login_required
def activate(itemid=0):
if current_user.wallet < 1:
flash('Insufficient Funds')
return redirect(url_for('uinvoice.addfunds'))
#work with disabled deploys only
result = current_user.inv_deployments.all()
inventory = []
for invcls in result:
inventory.extend([invcls.machine_id])
if current_user.is_administrator():
current_app.logger.info('[ADMIN] Access override for deployment id:{}'.format(itemid))
elif not itemid in inventory:
current_app.logger.warning('[{}] Access violation with deployment id: {}'.format(current_user.email, itemid))
abort(404)
#current_app.logger.info('[{}] Disabled deployments: {}'.format(current_user.email, inventory))
form = ActivateForm(period=Deployment.query.filter_by(machine_id=itemid))
deploy = Deployment.query.filter_by(machine_id=itemid).first()
cpu_cost = deploy.machine_cpu * current_app.config['CPU_RATIO']
mem_cost = ( deploy.machine_mem / 1024 ) * current_app.config['MEM_RATIO']
hdd_cost = deploy.machine_hdd * current_app.config['HDD_RATIO']
total = round(cpu_cost + mem_cost + hdd_cost,2)
form = ActivateForm(period=int(deploy.period))
if current_user.confirmed and form.validate_on_submit():
current_app.logger.info('payment')
return render_template('vmanager/activate.html', form=form, itemid=itemid)
if current_user.wallet < total:
flash('Insufficient Funds')
return redirect(url_for('uinvoice.transactions'))
current_app.logger.info('[{}] Charge deployments: {}'.format(current_user.email, inventory))
today = datetime.utcnow()
current_app.logger.info(form.period.data)
daysleft = relativedelta(today, months=+(form.period.data))
deploy.last_charge_date = today
deploy.period = form.period.data
deploy.daysleft = form.period.data * daysleft.days
deploy.warning = False
deploy.active = True
db.session.commit()
transaction = Transaction(user_id=int(current_user.pid), description='Deployment {} activated for {} month(s)', value=-total)
db.session.add(transaction)
db.session.commit()
current_user.wallet = current_user.wallet - total
db.session.commit()
return redirect(url_for('main.dashboard'))
return render_template('vmanager/activate.html', form=form, deploy=deploy, cpu_cost=cpu_cost, mem_cost=mem_cost, hdd_cost=hdd_cost, total=total)
@vmanager.route('/<cmd>/<int:vmid>')
@login_required

View file

@ -91,12 +91,7 @@ def autowarn():
lastcharge = deploy.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
daysleft = expiry - today
cpu_cost = deploy.machine_cpu * app.config['CPU_RATIO']
mem_cost = ( deploy.machine_mem / 1024 ) * app.config['MEM_RATIO']
hdd_cost = deploy.machine_hdd * app.config['HDD_RATIO']
total = cpu_cost + mem_cost + hdd_cost
deploy.daysleft = daysleft.days
deploy.price = total
db.session.commit()
warndays = deploy.period * 5
if daysleft.days < warndays: