addr2pool admin function

This commit is contained in:
deflax 2017-07-24 13:33:33 +03:00
parent 11c5712675
commit e7cde727b3
11 changed files with 111 additions and 18 deletions

View file

@ -1,6 +1,8 @@
import string
import random
from ..models import User, Role
from .. import db
from ..models import User, Role, Region
from flask_wtf import FlaskForm, RecaptchaField
from wtforms import StringField, PasswordField, BooleanField, SubmitField, SelectField, DecimalField
@ -11,3 +13,14 @@ class ChargeForm(FlaskForm):
amount = DecimalField('Стойност:', [validators.DataRequired(), validators.NumberRange(min=1, max=500)])
submit = SubmitField('Зареди')
class Addr2PoolForm(FlaskForm):
#regions = Region.query.all()
#region_choices = []
#for region in regions:
# region_choices.expand((region.pid, str(region.description)))
region_choices = [(1, 'Plovdiv, Bulgaria')]
ip = StringField('IP Address:', [validators.DataRequired(), validators.Regexp(message='172.16.0.1', regex='^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$')])
region = SelectField('Region', choices=region_choices, coerce=int)
reserved = BooleanField('Reserved:')
submit = SubmitField('Add IP')

View file

@ -3,7 +3,7 @@ from flask_login import login_required, login_user, logout_user
from flask_sqlalchemy import get_debug_queries
from . import admin
from .forms import ChargeForm
from .forms import ChargeForm, Addr2PoolForm
from .. import db
from ..email import send_email
@ -68,7 +68,26 @@ def charge(user_pid=0):
return redirect(url_for('admin.list_users'))
return render_template('admin/charge.html', form=form, usr=cuser)
@admin.route("/addr2pool", methods=['GET', 'POST'])
@login_required
@admin_required
def addr2pool():
alladdrlist = []
alladdr = Address.query.all()
for addr in alladdr:
alladdrlist.append(str(addr.ip))
current_app.logger.info('Current IP pool: {}'.format(alladdrlist))
form = Addr2PoolForm()
if form.validate_on_submit():
if form.ip.data in alladdrlist:
flash('IP address {} is already in the pool!'.format(form.ip.data))
return redirect(url_for('admin.addr2pool'))
address = Address(ip=form.ip.data, region_id=form.region.data, enabled=True, reserved=form.reserved.data)
db.session.add(address)
db.session.commit()
flash('Address {} added to region {}'.format(form.ip.data, form.region.data))
return redirect(url_for('admin.list_items'))
return render_template('admin/addr2pool.html', form=form, alladdresses=alladdrlist)
@admin.route("/dashboard/<int:user_pid>", methods=['GET'])
@login_required

View file

@ -282,15 +282,16 @@ class Address(db.Model):
rdns = db.Column(db.String)
reserved = db.Column(db.Boolean, default=False) #this ip SHOULD NOT be listed as available to assign even if its not currently owned by anyone
def __init__(self):
def __init__(self, **kwargs):
super(Address, self).__init__(**kwargs)
if self.mac is None:
self.mac = self.genmac()
def genmac(self):
addr = Address.query.all()
alladdr = Address.query.all()
allmacs = []
for addr in alladdr:
allmacs.expand(str(addr.mac))
allmacs.append(str(addr.mac))
while True:
mac = [ random.randint(0, 255) for x in range(0, 6) ]
mac[0] = (mac[0] & 0xfc) | 0x02

View file

@ -67,5 +67,5 @@ def activate(itemid=0):
return redirect(url_for('admin.list_items'))
else:
return redirect(url_for('main.dashboard'))
return render_template('smanager/activate.html', form=form, service=service, ppm=ppm, total=(ppm * service.period))
return render_template('smanager/activate.html', form=form, service=service, ppm=ppm, total=(ppm * service.period), currency=owner.currency)

View file

@ -0,0 +1,61 @@
{% extends "base.html" %}
{% block title %}Add IP address to ip pool{% endblock %}
{% block page_content %}
<div class="container-fluid">
<div class="row">
{% include "admin/admin_tasks.html" %}
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading">Add IP address to the pool</div>
<div class="panel-body">
<form method="POST" action="{{ url_for('admin.addr2pool') }}">
<p>
{{ form.ip.label }} {{ form.ip }}<br />
{% for error in form.ip.errors %}
{{ error }}<br />
{% endfor %}
</p>
<p>
{{ form.region.label }} {{ form.region }}<br />
{% for error in form.region.errors %}
{{ error }}<br />
{% endfor %}
</p>
<p>
{{ form.reserved.label }} {{ form.reserved }}<br />
{% for error in form.reserved.errors %}
{{ error }}<br />
{% endfor %}
</p>
<p>
{{ form.csrf_token() }}
{{ form.submit }}
</p>
</div>
</div>
</div>
<div class="col-md-12">
<div class="panel panel-info">
<div class="panel-heading">Current IP Pool</div>
<div class="panel-body">
{% for addr in alladdresses %}
{{ addr }}<br />
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}

View file

@ -6,7 +6,7 @@
<button class="btn btn-default" onclick="window.open('{{ url_for('admin.list_users') }}','_self')"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> Users</button>
<button class="btn btn-default" onclick="window.open('{{ url_for('admin.list_items') }}','_self')"><span class="glyphicon glyphicon-list" aria-hidden="true"></span> Items</button>
<button class="btn btn-default" onclick="window.open('{{ url_for('admin.list_transactions') }}','_self')"><span class="glyphicon glyphicon-usd" aria-hidden="true"></span> Transactions</button>
<button class="btn btn-default" onclick="alert('yes. this button does nothing.')"><span class="glyphicon glyphicon-cd" aria-hidden="true"></span> Nothing</button>
<button class="btn btn-default" onclick="alert('Как ще си събираш зъбите със счупени ръце?')"><span class="glyphicon glyphicon-cd" aria-hidden="true"></span> Nothing</button>
</center>
</div>
</div>

View file

@ -138,7 +138,6 @@
<table class="table table-hover table-striped table-condensed cf">
<thead>
<tr>
<th>Owner</th>
<th>IP</th>
<th>MAC Addr.</th>
<th>Reverse DNS</th>
@ -148,7 +147,6 @@
{% for address in addresses %}
<tr>
{% if address.enabled == False %}<tr class="danger">{% else %}<tr>{% endif %}
<td><a href="{{ url_for('admin.dashboard', user_pid=address.user_id) }}">{{ address.owner.email }}</a></td>
<td>{{ address.ip }}</td>
<td>{{ address.mac }}</td>
<td>{{ address.rdns }}</td>
@ -156,6 +154,7 @@
</tr>
</tbody>
</table>
<button class="btn btn-default" onclick="window.open('{{ url_for('admin.addr2pool') }}')"><span class="glyphicon glyphicon-plus" aria-hiddent="true"></span> Add IP to pool</button>
</div>
</div>
</div>

View file

@ -18,7 +18,7 @@ function calculateTotal()
var total = 0;
total = period * ppm;
var divobj = document.getElementById('totalPrice');
divobj.innerHTML = "Total Price: "+total;
divobj.innerHTML = "Total Price: "+total+" {{ currency }}";
}
</script>
{% endblock %}
@ -42,7 +42,7 @@ function calculateTotal()
{% endfor %}
<h3>
---<br />
<div id="totalPrice">Previous Period: {{ total }}</div>
<div id="totalPrice">Previous Period: {{ total }} {{ currency }}</div>
</h3>
{{ form.csrf_token() }}

View file

@ -18,7 +18,7 @@ function calculateTotal()
var total = 0;
total = period * ppm;
var divobj = document.getElementById('totalPrice');
divobj.innerHTML = "Total Price: "+total;
divobj.innerHTML = "Total Price: "+total+" {{ currency }}";
}
</script>
{% endblock %}
@ -44,7 +44,7 @@ Storage: {{ deploy.machine_hdd }} GB<br />
{{ error }}<br />
{% endfor %}
---<br />
<div id="totalPrice">Previous Period: {{ total }}</div>
<div id="totalPrice">Previous Period: {{ total }} {{ currency }}</div>
</h3>
<p>

View file

@ -171,7 +171,7 @@ def activate(itemid=0):
return redirect(url_for('admin.list_items'))
else:
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, ppm=ppm, total=(ppm * deploy.period))
return render_template('vmanager/activate.html', form=form, deploy=deploy, cpu_cost=cpu_cost, mem_cost=mem_cost, hdd_cost=hdd_cost, ppm=ppm, total=(ppm * deploy.period), currency=owner.currency)
@vmanager.route('/<cmd>/<int:vmid>')
@login_required

View file

@ -91,7 +91,7 @@ def autowarn():
lastcharge = deploy.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(deploy.period))
daysleft = expiry - today
deploy.daysleft = daysleft.days
deploy.daysleft = daysleft.days + 1
db.session.commit()
warndays = deploy.period * 5
if daysleft.days < warndays:
@ -107,7 +107,7 @@ def autowarn():
lastcharge = service.date_last_charge
expiry = lastcharge + relativedelta(lastcharge, months=+(service.period))
daysleft = expiry - today
service.daysleft = daysleft.days
service.daysleft = daysleft.days + 1
db.session.commit()
warndays = service.period * 5
if daysleft.days < warndays:
@ -122,7 +122,7 @@ def autowarn():
for domain in domains_ena:
expiry = domain.date_expire
daysleft = expiry - today
domain.daysleft = daysleft.days
domain.daysleft = daysleft.days + 1
db.session.commit()
warndays = service.period * 5