split item managers and prepare for activate function

This commit is contained in:
deflax 2017-07-14 02:46:51 +03:00
parent 168fbde4e5
commit 85cdfef330
8 changed files with 149 additions and 12 deletions

View file

@ -38,12 +38,12 @@ moment.init_app(app)
from .main import main as main_blueprint from .main import main as main_blueprint
app.register_blueprint(main_blueprint) app.register_blueprint(main_blueprint)
from .news import news as news_blueprint
app.register_blueprint(news_blueprint, url_prefix='/news')
from .auth import auth as auth_blueprint from .auth import auth as auth_blueprint
app.register_blueprint(auth_blueprint, url_prefix='/auth') app.register_blueprint(auth_blueprint, url_prefix='/auth')
from .news import news as news_blueprint
app.register_blueprint(news_blueprint, url_prefix='/news')
from .admin import admin as admin_blueprint from .admin import admin as admin_blueprint
app.register_blueprint(admin_blueprint, url_prefix='/' + app.config['ADMIN_PREFIX']) app.register_blueprint(admin_blueprint, url_prefix='/' + app.config['ADMIN_PREFIX'])
@ -53,6 +53,12 @@ app.register_blueprint(settings_blueprint, url_prefix='/settings')
from .vmanager import vmanager as vmanager_blueprint from .vmanager import vmanager as vmanager_blueprint
app.register_blueprint(vmanager_blueprint, url_prefix='/vmanager') app.register_blueprint(vmanager_blueprint, url_prefix='/vmanager')
from .smanager import smanager as smanager_blueprint
app.register_blueprint(smanager_blueprint, url_prefix='/smanager')
from .dmanager import dmanager as dmanager_blueprint
app.register_blueprint(dmanager_blueprint, url_prefix='/dmanager')
from .uinvoice import uinvoice as uinvoice_blueprint from .uinvoice import uinvoice as uinvoice_blueprint
app.register_blueprint(uinvoice_blueprint, url_prefix='/uinvoice') app.register_blueprint(uinvoice_blueprint, url_prefix='/uinvoice')

3
app/dmanager/__init__.py Normal file
View file

@ -0,0 +1,3 @@
from flask import Blueprint
dmanager = Blueprint('dmanager', __name__)
from . import routes

44
app/dmanager/routes.py Normal file
View file

@ -0,0 +1,44 @@
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 dmanager
from .. import db
from ..email import send_email
from ..models import User, Permission, Service
from ..decorators import admin_required, permission_required
from datetime import datetime, timedelta, date, time
@dmanager.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
@dmanager.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 items only
result = current_user.inv_domains.filter_by(enabled=False)
inventory = []
for invcls in result:
inventory.extend([invcls.pid])
#checks if current user owns this item
if not itemid in inventory:
current_app.logger.warning('[{}] Access violation with domain id: {}'.format(current_user.email, itemid))
#TODO: log ips
abort(404)
else:
current_app.logger.info('[{}] Disabled services: {}'.format(current_user.email, inventory))
abort(403)
#if current_user.confirmed and form.validate_on_submit():
# client_id = current_user.pid
return render_template('dmanager/activate.html', page=page, form=form)

3
app/smanager/__init__.py Normal file
View file

@ -0,0 +1,3 @@
from flask import Blueprint
smanager = Blueprint('smanager', __name__)
from . import routes

45
app/smanager/routes.py Normal file
View file

@ -0,0 +1,45 @@
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
from .. import db
from ..email import send_email
from ..models import User, Permission, Service
from ..decorators import admin_required, permission_required
from datetime import datetime, timedelta, date, time
@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
@smanager.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 items only
result = current_user.inv_services.all()
current_app.logger.info(result)
inventory = []
for invcls in result:
inventory.extend([invcls.pid])
#checks if current user owns this item
if not itemid in inventory:
current_app.logger.warning('[{}] Access violation with service id: {}'.format(current_user.email, itemid))
#TODO: log ips
abort(404)
else:
current_app.logger.info('[{}] Disabled services: {}'.format(current_user.email, inventory))
abort(403)
#if current_user.confirmed and form.validate_on_submit():
# client_id = current_user.pid
return render_template('smanager/activate.html', page=page, form=form)

View file

@ -0,0 +1,30 @@
{% extends "base.html" %}
{% block title %}Activate{% endblock %}
{% block page_content %}
<div class="row">
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading">Activate</div>
<div class="panel-body">
<form method="POST" action"{{ url_for('vmanager.activate', vmid=vmid) }}">
<p>{{ form.period.label }}<br />{{ form.period }}<br />
{% for error in form.period.errors %}
{{ error }}<br />
{% endfor %}
</p>
<p>
{{ form.csrf_token() }}
{{ form.submit }}
</p>
</div>
</div>
</div>
{% endblock %}

View file

@ -21,4 +21,6 @@ class DeployForm(FlaskForm):
submit = SubmitField('Deploy') 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)])
submit = SubmitField('Activate')

View file

@ -3,7 +3,7 @@ from flask_login import login_required, login_user, logout_user, current_user
from flask_sqlalchemy import get_debug_queries from flask_sqlalchemy import get_debug_queries
from . import vmanager from . import vmanager
from .forms import DeployForm from .forms import DeployForm, ActivateForm
from .. import db from .. import db
from ..email import send_email from ..email import send_email
from ..models import User, Permission, Deployment, Service, Region, Address, Domain, contact_proxmaster from ..models import User, Permission, Deployment, Service, Region, Address, Domain, contact_proxmaster
@ -121,18 +121,22 @@ def deploy(product_id=None):
@login_required @login_required
def activate(vmid=0): def activate(vmid=0):
#work with disabled deploys only #work with disabled deploys only
result = current_user.inv_deployments.filter_by(enabled=False) result = current_user.inv_deployments.all()
inventory = [] inventory = []
for invcls in result: for invcls in result:
inventory.extend([invcls.machine_id]) inventory.extend([invcls.machine_id])
#checks if current user owns this vmid
if not vmid in inventory: if current_user.is_administrator():
current_app.logger.info('[ADMIN] Access override for cube id:{}'.format(vmid))
elif not vmid in inventory:
current_app.logger.warning('[{}] Access violation with cube id: {}'.format(current_user.email, vmid)) current_app.logger.warning('[{}] Access violation with cube id: {}'.format(current_user.email, vmid))
#TODO: log ips
abort(404) abort(404)
else:
current_app.logger.info('[{}] Disabled deployments: {}'.format(current_user.email, inventory)) #current_app.logger.info('[{}] Disabled deployments: {}'.format(current_user.email, inventory))
abort(403) form = ActivateForm(period=Deployment.query.filter_by(machine_id=vmid))
if current_user.confirmed and form.validate_on_submit():
current_app.logger.info('meh')
return render_template('vmanager/activate.html', form=form, vmid=vmid)
@vmanager.route('/<cmd>/<int:vmid>') @vmanager.route('/<cmd>/<int:vmid>')
@login_required @login_required