mirror of
https://github.com/TakeusV1/flask_ipam.git
synced 2026-10-13 17:15:33 +02:00
V1.1
This commit is contained in:
1 parent
4870f0b089
commit
e5fbf15828
5 files changed
+44
-20
No files matched your search
+1
-1
@@ -8,7 +8,7 @@ class LoginForm(FlaskForm):
|
|||||||
|
|
||||||
class NewPrefixForm(FlaskForm):
|
class NewPrefixForm(FlaskForm):
|
||||||
subnet = StringField(validators=[DataRequired(),IPAddress(ipv4=True),Length(max=128)])
|
subnet = StringField(validators=[DataRequired(),IPAddress(ipv4=True),Length(max=128)])
|
||||||
prefix = IntegerField(validators=[DataRequired(),NumberRange(min=20,max=32)])
|
prefix = SelectField(choices=[(30,'/30'),(29,'/29'),(28,'/28'),(27,'/27'),(26,'/26'),(25,'/25'),(24,'/24'),(23,'/23'),(22,'/22')],validators=[DataRequired()])
|
||||||
description = StringField(validators=[Length(max=128)])
|
description = StringField(validators=[Length(max=128)])
|
||||||
|
|
||||||
class NewAllocationForm(FlaskForm):
|
class NewAllocationForm(FlaskForm):
|
||||||
|
|||||||
+2
-1
@@ -13,13 +13,14 @@ class Network(db.Model):
|
|||||||
prefix = db.Column(db.Integer())
|
prefix = db.Column(db.Integer())
|
||||||
network_address = db.Column(db.String(16),unique=True)
|
network_address = db.Column(db.String(16),unique=True)
|
||||||
hosts = db.Column(db.Integer())
|
hosts = db.Column(db.Integer())
|
||||||
description = db.Column(db.String(32),unique=True)
|
description = db.Column(db.String(32))
|
||||||
|
|
||||||
class Allocation(db.Model):
|
class Allocation(db.Model):
|
||||||
ipv4 = db.Column(db.String(16),primary_key=True,unique=True)
|
ipv4 = db.Column(db.String(16),primary_key=True,unique=True)
|
||||||
net_id = db.Column(db.Integer(),db.ForeignKey('network.id'))
|
net_id = db.Column(db.Integer(),db.ForeignKey('network.id'))
|
||||||
hostname = db.Column(db.String(32))
|
hostname = db.Column(db.String(32))
|
||||||
description = db.Column(db.String(64))
|
description = db.Column(db.String(64))
|
||||||
|
is_used = db.Column(db.Boolean(),default=False)
|
||||||
device_id = db.Column(db.Integer(),db.ForeignKey('inventory.id'))
|
device_id = db.Column(db.Integer(),db.ForeignKey('inventory.id'))
|
||||||
|
|
||||||
class Inventory(db.Model):
|
class Inventory(db.Model):
|
||||||
|
|||||||
+15
-5
@@ -23,7 +23,7 @@ def home():
|
|||||||
def dashboard():
|
def dashboard():
|
||||||
|
|
||||||
db_networks = Network.query.count()
|
db_networks = Network.query.count()
|
||||||
db_allocations = Allocation.query.count()
|
db_allocations = Allocation.query.filter_by(is_used=True).count()
|
||||||
db_inventory = Inventory.query.count()
|
db_inventory = Inventory.query.count()
|
||||||
db_users = User.query.count()
|
db_users = User.query.count()
|
||||||
available_alloc = 0
|
available_alloc = 0
|
||||||
@@ -52,12 +52,16 @@ def networks():
|
|||||||
form = NewPrefixForm()
|
form = NewPrefixForm()
|
||||||
if request.method == 'POST' and form.validate_on_submit():
|
if request.method == 'POST' and form.validate_on_submit():
|
||||||
try:
|
try:
|
||||||
network = IPv4Network(f"{str(form.subnet.data)}/{str(form.prefix.data)}")
|
network = IPv4Network(f"{str(form.subnet.data)}/{str(form.prefix.data)}", False)
|
||||||
|
|
||||||
add_net = Network(network_address=str(network.network_address),prefix=network.prefixlen,hosts=network.num_addresses,description=str(form.description.data))
|
add_net = Network(network_address=str(network.network_address),prefix=network.prefixlen,hosts=network.num_addresses-2,description=str(form.description.data))
|
||||||
db.session.add(add_net)
|
db.session.add(add_net)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
for ip in network.hosts():
|
||||||
|
db.session.add(Allocation(ipv4=str(ip), net_id=add_net.id))
|
||||||
|
|
||||||
|
db.session.commit()
|
||||||
return redirect(url_for('base.networks'))
|
return redirect(url_for('base.networks'))
|
||||||
|
|
||||||
except:
|
except:
|
||||||
@@ -73,8 +77,14 @@ def networks():
|
|||||||
def allocations(net_id):
|
def allocations(net_id):
|
||||||
|
|
||||||
active_page = request.args.get('page', type=int)
|
active_page = request.args.get('page', type=int)
|
||||||
|
if active_page == None:
|
||||||
|
active_page = 0
|
||||||
|
|
||||||
|
next_page = active_page+1
|
||||||
|
prev_page = active_page-1
|
||||||
|
|
||||||
db_network = Network.query.filter_by(id=net_id).first()
|
db_network = Network.query.filter_by(id=net_id).first()
|
||||||
db_allocations = db.paginate(db.select(Allocation).filter_by(net_id=net_id), per_page=12 )
|
db_allocations = db.paginate(db.select(Allocation).filter_by(net_id=net_id), per_page=15 )
|
||||||
|
|
||||||
form = NewAllocationForm()
|
form = NewAllocationForm()
|
||||||
if request.method == 'POST' and form.validate_on_submit():
|
if request.method == 'POST' and form.validate_on_submit():
|
||||||
@@ -97,7 +107,7 @@ def allocations(net_id):
|
|||||||
elif form.errors:
|
elif form.errors:
|
||||||
flash(form.errors, 'danger')
|
flash(form.errors, 'danger')
|
||||||
|
|
||||||
return render_template('panel/allocations.html', title="Allocations", is_networks=True, app_version=app_version, form=form, allocations=db_allocations, network=db_network, Inventory=Inventory, active_page=active_page, navcolor='dark')
|
return render_template('panel/allocations.html', title="Allocations", is_networks=True, app_version=app_version, form=form, allocations=db_allocations, network=db_network, Inventory=Inventory, active_page=active_page, next_page=next_page, prev_page=prev_page, navcolor='dark')
|
||||||
|
|
||||||
## MODAL ROUTES
|
## MODAL ROUTES
|
||||||
|
|
||||||
|
|||||||
@@ -6,10 +6,9 @@
|
|||||||
<div class="row">
|
<div class="row">
|
||||||
<!-- IPV4 TABLE -->
|
<!-- IPV4 TABLE -->
|
||||||
<div class="col-9">
|
<div class="col-9">
|
||||||
<table class="table table-striped table-hover">
|
<table class="table table-striped table-hover table-sm">
|
||||||
<thead>
|
<thead>
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="col">Network</th>
|
|
||||||
<th scope="col">IPv4</th>
|
<th scope="col">IPv4</th>
|
||||||
<th scope="col">Hostname</th>
|
<th scope="col">Hostname</th>
|
||||||
<th scope="col">Description</th>
|
<th scope="col">Description</th>
|
||||||
@@ -20,10 +19,9 @@
|
|||||||
<tbody>
|
<tbody>
|
||||||
{% for allocation in allocations %}
|
{% for allocation in allocations %}
|
||||||
<tr>
|
<tr>
|
||||||
<th scope="row">{{network.network_address}}</th>
|
<td><b>{{allocation.ipv4}}</b></td>
|
||||||
<td>{{allocation.ipv4}}</td>
|
|
||||||
<td>{{allocation.hostname}}</td>
|
<td>{{allocation.hostname}}</td>
|
||||||
<td><b>{{allocation.description}}</b></td>
|
<td>{{allocation.description}}</td>
|
||||||
<td>
|
<td>
|
||||||
{% if Inventory.query.filter_by(id=allocation.device_id).first() %}
|
{% if Inventory.query.filter_by(id=allocation.device_id).first() %}
|
||||||
<a href="{{Inventory.query.filter_by(id=allocation.device_id).first().id}}" style="text-decoration:none;">link</a>
|
<a href="{{Inventory.query.filter_by(id=allocation.device_id).first().id}}" style="text-decoration:none;">link</a>
|
||||||
@@ -81,14 +79,29 @@
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
<div class="d-flex justify-content-between">
|
||||||
|
<nav>
|
||||||
|
<ul class="pagination">
|
||||||
|
{% if allocations.has_prev %}
|
||||||
|
<li class="page-item"><a class="page-link" href="?page={{prev_page}}">Previous</a></li>
|
||||||
|
{% endif %}
|
||||||
|
{% if allocations.has_next %}
|
||||||
|
<li class="page-item"><a class="page-link" href="?page={{next_page}}">Next</a></li>
|
||||||
|
{% endif %}
|
||||||
|
<!--
|
||||||
|
{% for page in range(allocations.pages+1) %}
|
||||||
|
{% if page != 0%}
|
||||||
|
<li class="page-item"><a class="page-link {%if page==active_page%}active{%endif%}" href="?page={{page}}">{{page}}</a></li>
|
||||||
|
{% endif %}
|
||||||
|
{% endfor %}-->
|
||||||
|
</ul>
|
||||||
|
</nav>
|
||||||
|
<div>
|
||||||
|
{{ allocations.first }} - {{ allocations.last }} of {{ allocations.total }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<nav class="d-flex flex-row justify-content-center align-items-center">
|
|
||||||
<ul class="pagination">
|
|
||||||
{% for page in allocations.iter_pages() %}
|
|
||||||
<li class="page-item"><a class="page-link" href="?page={{page}}">{{page}}</a></li>
|
|
||||||
{% endfor %}
|
|
||||||
</ul>
|
|
||||||
</nav>
|
|
||||||
<!-- MODAL -->
|
<!-- MODAL -->
|
||||||
<div id="modal-del"
|
<div id="modal-del"
|
||||||
class="modal modal-blur fade"
|
class="modal modal-blur fade"
|
||||||
|
|||||||
@@ -23,7 +23,7 @@
|
|||||||
<th scope="row">{{network.id}}</th>
|
<th scope="row">{{network.id}}</th>
|
||||||
<td>{{network.network_address}}</td>
|
<td>{{network.network_address}}</td>
|
||||||
<td>{{network.prefix}}</td>
|
<td>{{network.prefix}}</td>
|
||||||
<td>{{Allocation.query.filter_by(net_id=network.id).count()}} / {{network.hosts}} </td>
|
<td>{{Allocation.query.filter_by(net_id=network.id,is_used=True).count()}} / {{network.hosts}} </td>
|
||||||
<td><b>{{network.description}}</b></td>
|
<td><b>{{network.description}}</b></td>
|
||||||
<td>
|
<td>
|
||||||
<a href="{{ url_for('base.allocations',net_id=network.id) }}" class="btn btn-sm btn-outline-info" style="padding-top:1px;padding-bottom:1px">Allocations</a>
|
<a href="{{ url_for('base.allocations',net_id=network.id) }}" class="btn btn-sm btn-outline-info" style="padding-top:1px;padding-bottom:1px">Allocations</a>
|
||||||
|
|||||||
Reference in new issue
Block a user