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
711d1fbba3
commit
a9e8a70df5
4 files changed
+70
-75
No files matched your search
@@ -23,6 +23,7 @@ class Allocation(db.Model):
|
||||
is_used = db.Column(db.Boolean(),default=False)
|
||||
is_dhcp = db.Column(db.Boolean(),default=False)
|
||||
is_gateway = db.Column(db.Boolean(),default=False)
|
||||
is_decom = db.Column(db.Boolean(),default=False)
|
||||
device_id = db.Column(db.Integer(),db.ForeignKey('inventory.id'))
|
||||
|
||||
class Inventory(db.Model):
|
||||
|
||||
+54
-23
@@ -1,5 +1,5 @@
|
||||
from datetime import datetime
|
||||
from flask import Blueprint, request, render_template, flash, redirect, url_for
|
||||
from flask import Blueprint, request, render_template, flash, redirect, url_for, session
|
||||
from flask_login import login_required, current_user
|
||||
from ipaddress import IPv4Network, IPv4Address
|
||||
|
||||
@@ -78,34 +78,42 @@ def allocations(net_id):
|
||||
|
||||
active_page = request.args.get('page', type=int)
|
||||
if active_page == None:
|
||||
active_page = 0
|
||||
active_page = 1
|
||||
|
||||
next_page = active_page+1
|
||||
prev_page = active_page-1
|
||||
|
||||
if request.args.get('items', type=int) != None:
|
||||
session['items'] = request.args.get('items', type=int)
|
||||
try:
|
||||
if session['items'] == None:
|
||||
session['items'] = 15
|
||||
except:
|
||||
session['items'] = 15
|
||||
|
||||
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=15 )
|
||||
db_allocations = db.paginate(db.select(Allocation).filter_by(net_id=net_id), per_page=session['items'] )
|
||||
|
||||
form = NewAllocationForm()
|
||||
if request.method == 'POST' and form.validate_on_submit():
|
||||
network = IPv4Network(f"{str(db_network.network_address)}/{str(db_network.prefix)}")
|
||||
##if request.method == 'POST' and form.validate_on_submit():
|
||||
## network = IPv4Network(f"{str(db_network.network_address)}/{str(db_network.prefix)}")
|
||||
# Check if IPv4 Address is already allocated
|
||||
if Allocation.query.filter_by(ipv4=str(form.ipv4.data)).first() != None:
|
||||
flash("IPv4 Address Already Allocated", 'danger')
|
||||
return redirect(url_for('base.allocations', net_id=net_id, page=active_page))
|
||||
# Check if IPv4 Address is in the Network
|
||||
if IPv4Address(form.ipv4.data) in network.hosts():
|
||||
add_alloc = Allocation(ipv4=str(form.ipv4.data), net_id=db_network.id, hostname=str(form.host_name.data), description=str(form.host_desc.data))
|
||||
db.session.add(add_alloc)
|
||||
db.session.commit()
|
||||
flash("Allocation Added", 'success')
|
||||
return redirect(url_for('base.allocations', net_id=net_id, page=active_page))
|
||||
## if Allocation.query.filter_by(ipv4=str(form.ipv4.data)).first() != None:
|
||||
## flash("IPv4 Address Already Allocated", 'danger')
|
||||
## return redirect(url_for('base.allocations', net_id=net_id, page=active_page))
|
||||
## # Check if IPv4 Address is in the Network
|
||||
## if IPv4Address(form.ipv4.data) in network.hosts():
|
||||
## add_alloc = Allocation(ipv4=str(form.ipv4.data), net_id=db_network.id, hostname=str(form.host_name.data), description=str(form.host_desc.data))
|
||||
## db.session.add(add_alloc)
|
||||
## db.session.commit()
|
||||
## flash("Allocation Added", 'success')
|
||||
## return redirect(url_for('base.allocations', net_id=net_id, page=active_page))
|
||||
# If not in Network
|
||||
flash("Invalid IPv4 Address", 'danger')
|
||||
return redirect(url_for('base.allocations', net_id=net_id, page=active_page))
|
||||
## flash("Invalid IPv4 Address", 'danger')
|
||||
## return redirect(url_for('base.allocations', net_id=net_id, page=active_page))
|
||||
# If Form Errors
|
||||
elif form.errors:
|
||||
flash(form.errors, 'danger')
|
||||
##elif form.errors:
|
||||
## 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, next_page=next_page, prev_page=prev_page, navcolor='dark')
|
||||
|
||||
@@ -193,7 +201,18 @@ def allocation_delete(net_id,ipv4):
|
||||
db_alloc.is_dhcp = False
|
||||
db_alloc.is_gateway = False
|
||||
db.session.commit()
|
||||
flash("Allocation Deleted", 'warning')
|
||||
flash("Allocation Removed", 'warning')
|
||||
return redirect(url_for('base.allocations',net_id=net_id,page=request.args.get('page', type=int)))
|
||||
|
||||
@base.get('/network/<int:net_id>/allocations/<string:ipv4>/decom/<int:action>')
|
||||
@login_required
|
||||
def allocation_decom(net_id,ipv4,action):
|
||||
db_alloc = Allocation.query.filter_by(net_id=net_id,ipv4=ipv4).first()
|
||||
if action == 1:
|
||||
db_alloc.is_decom = True
|
||||
else:
|
||||
db_alloc.is_decom = False
|
||||
db.session.commit()
|
||||
return redirect(url_for('base.allocations',net_id=net_id,page=request.args.get('page', type=int)))
|
||||
|
||||
@base.get('/network/<int:net_id>/delete')
|
||||
@@ -206,6 +225,7 @@ def network_delete(net_id):
|
||||
if db_alloc != None:
|
||||
for alloc in db_alloc:
|
||||
db.session.delete(alloc)
|
||||
|
||||
db.session.delete(db_network)
|
||||
db.session.commit()
|
||||
flash("Network Deleted", 'warning')
|
||||
@@ -230,7 +250,6 @@ def allocation_search(net_id):
|
||||
# AND
|
||||
if ',' in search:
|
||||
for alloc in db_alloc:
|
||||
print(search.split(','))
|
||||
if alloc.ipv4.lower() in search.split(','):
|
||||
obj_list.append(alloc)
|
||||
# BETWEEN
|
||||
@@ -245,7 +264,6 @@ def allocation_search(net_id):
|
||||
if int(alloc.ipv4.split('.')[-1]) <= temp_2 and int(alloc.ipv4.split('.')[-1]) >= temp_1:
|
||||
obj_list.append(alloc)
|
||||
# DHCP
|
||||
print(search)
|
||||
if 'dhcp' in search:
|
||||
for alloc in db_alloc:
|
||||
if alloc.is_dhcp:
|
||||
@@ -256,6 +274,19 @@ def allocation_search(net_id):
|
||||
for alloc in db_alloc:
|
||||
if alloc.is_gateway:
|
||||
obj_list.append(alloc)
|
||||
|
||||
## DECOM
|
||||
if 'decom' in search:
|
||||
for alloc in db_alloc:
|
||||
if alloc.is_decom:
|
||||
obj_list.append(alloc)
|
||||
|
||||
## DECOM
|
||||
if 'used' in search:
|
||||
for alloc in db_alloc:
|
||||
if alloc.is_used:
|
||||
obj_list.append(alloc)
|
||||
|
||||
else:
|
||||
# BASIC SEARCH
|
||||
for alloc in db_alloc:
|
||||
@@ -268,4 +299,4 @@ def allocation_search(net_id):
|
||||
if alloc.description.lower().startswith(search):
|
||||
obj_list.append(alloc)
|
||||
|
||||
return render_template('panel/_search-entry.html',db_alloc=obj_list,Inventory=Inventory,network=db_network)
|
||||
return render_template('panel/_table-alloc.html',allocations=obj_list,Inventory=Inventory,network=db_network)
|
||||
@@ -1,4 +1,4 @@
|
||||
{% for allocation in db_alloc %}
|
||||
{% for allocation in allocations %}
|
||||
<tr>
|
||||
<td><b>{{allocation.ipv4}}</b></td>
|
||||
<td>{{allocation.hostname}}</td>
|
||||
@@ -10,6 +10,9 @@
|
||||
N/A
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{{allocation.is_dhcp}}</td>
|
||||
<td>{{allocation.is_gateway}}</td>
|
||||
<td>{{allocation.is_decom}}</td>
|
||||
<td>
|
||||
{% if allocation.is_used %}
|
||||
<button
|
||||
@@ -22,13 +25,18 @@
|
||||
style="padding-top:1px;padding-bottom:1px"
|
||||
> Edit
|
||||
</button>
|
||||
{% if allocation.is_decom %}
|
||||
<a href="{{ url_for('base.allocation_decom',action=2,net_id=network.id,ipv4=allocation.ipv4) }}?page={{active_page}}" class="btn btn-sm btn-outline-primary" style="padding-top:1px;padding-bottom:1px">Undecom</a>
|
||||
{% else %}
|
||||
<a href="{{ url_for('base.allocation_decom',action=1,net_id=network.id,ipv4=allocation.ipv4) }}?page={{active_page}}" class="btn btn-sm btn-outline-warning" style="padding-top:1px;padding-bottom:1px">Decom</a>
|
||||
{% endif %}
|
||||
<button
|
||||
hx-get="{{ url_for('base.allocation_del_modal',net_id=network.id,ipv4=allocation.ipv4) }}?page={{active_page}}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modal-del"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#modal-del"
|
||||
class="btn btn-sm btn-outline-warning"
|
||||
class="btn btn-sm btn-outline-danger"
|
||||
style="padding-top:1px;padding-bottom:1px"
|
||||
> Unallocate
|
||||
</button>
|
||||
@@ -46,4 +54,4 @@
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% endfor %}
|
||||
@@ -25,59 +25,14 @@
|
||||
<th scope="col">Hostname</th>
|
||||
<th scope="col">Description</th>
|
||||
<th scope="col">Device</th>
|
||||
<th scope="col">DHCP</th>
|
||||
<th scope="col">GATEWAY</th>
|
||||
<th scope="col">DECOM</th>
|
||||
<th scope="col">Actions</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody id="search-results">
|
||||
{% for allocation in allocations %}
|
||||
<tr>
|
||||
<td><b>{{allocation.ipv4}}</b></td>
|
||||
<td>{{allocation.hostname}}</td>
|
||||
<td>{{allocation.description}}</td>
|
||||
<td>
|
||||
{% 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>
|
||||
{% else %}
|
||||
N/A
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>
|
||||
{% if allocation.is_used %}
|
||||
<button
|
||||
hx-get="{{ url_for('base.allocation_edit_modal',net_id=network.id,ipv4=allocation.ipv4) }}?page={{active_page}}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modal-edit"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#modal-edit"
|
||||
class="btn btn-sm btn-outline-info"
|
||||
style="padding-top:1px;padding-bottom:1px"
|
||||
> Edit
|
||||
</button>
|
||||
<button
|
||||
hx-get="{{ url_for('base.allocation_del_modal',net_id=network.id,ipv4=allocation.ipv4) }}?page={{active_page}}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modal-del"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#modal-del"
|
||||
class="btn btn-sm btn-outline-warning"
|
||||
style="padding-top:1px;padding-bottom:1px"
|
||||
> Unallocate
|
||||
</button>
|
||||
{% else %}
|
||||
<button
|
||||
hx-get="{{ url_for('base.allocation_edit_modal',net_id=network.id,ipv4=allocation.ipv4) }}?page={{active_page}}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modal-edit"
|
||||
data-bs-toggle="modal"
|
||||
data-bs-target="#modal-edit"
|
||||
class="btn btn-sm btn-outline-success"
|
||||
style="padding-top:1px;padding-bottom:1px"
|
||||
> Allocate
|
||||
</button>
|
||||
{% endif %}
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
{% include 'panel/_table-alloc.html' %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user