This commit is contained in:
Takeus committed 2025-02-12 16:35:23 +01:00
1 parent 711d1fbba3
commit a9e8a70df5
4 files changed
+70 -75

No files matched your search

+1
View File
@@ -23,6 +23,7 @@ class Allocation(db.Model):
is_used = db.Column(db.Boolean(),default=False) is_used = db.Column(db.Boolean(),default=False)
is_dhcp = db.Column(db.Boolean(),default=False) is_dhcp = db.Column(db.Boolean(),default=False)
is_gateway = 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')) device_id = db.Column(db.Integer(),db.ForeignKey('inventory.id'))
class Inventory(db.Model): class Inventory(db.Model):
+54 -23
View File
@@ -1,5 +1,5 @@
from datetime import datetime 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 flask_login import login_required, current_user
from ipaddress import IPv4Network, IPv4Address from ipaddress import IPv4Network, IPv4Address
@@ -78,34 +78,42 @@ def allocations(net_id):
active_page = request.args.get('page', type=int) active_page = request.args.get('page', type=int)
if active_page == None: if active_page == None:
active_page = 0 active_page = 1
next_page = active_page+1 next_page = active_page+1
prev_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_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() form = NewAllocationForm()
if request.method == 'POST' and form.validate_on_submit(): ##if request.method == 'POST' and form.validate_on_submit():
network = IPv4Network(f"{str(db_network.network_address)}/{str(db_network.prefix)}") ## network = IPv4Network(f"{str(db_network.network_address)}/{str(db_network.prefix)}")
# Check if IPv4 Address is already allocated # Check if IPv4 Address is already allocated
if Allocation.query.filter_by(ipv4=str(form.ipv4.data)).first() != None: ## if Allocation.query.filter_by(ipv4=str(form.ipv4.data)).first() != None:
flash("IPv4 Address Already Allocated", 'danger') ## flash("IPv4 Address Already Allocated", 'danger')
return redirect(url_for('base.allocations', net_id=net_id, page=active_page)) ## return redirect(url_for('base.allocations', net_id=net_id, page=active_page))
# Check if IPv4 Address is in the Network ## # Check if IPv4 Address is in the Network
if IPv4Address(form.ipv4.data) in network.hosts(): ## 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)) ## 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.add(add_alloc)
db.session.commit() ## db.session.commit()
flash("Allocation Added", 'success') ## flash("Allocation Added", 'success')
return redirect(url_for('base.allocations', net_id=net_id, page=active_page)) ## return redirect(url_for('base.allocations', net_id=net_id, page=active_page))
# If not in Network # If not in Network
flash("Invalid IPv4 Address", 'danger') ## flash("Invalid IPv4 Address", 'danger')
return redirect(url_for('base.allocations', net_id=net_id, page=active_page)) ## return redirect(url_for('base.allocations', net_id=net_id, page=active_page))
# If Form Errors # If Form Errors
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, next_page=next_page, prev_page=prev_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')
@@ -193,7 +201,18 @@ def allocation_delete(net_id,ipv4):
db_alloc.is_dhcp = False db_alloc.is_dhcp = False
db_alloc.is_gateway = False db_alloc.is_gateway = False
db.session.commit() 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))) return redirect(url_for('base.allocations',net_id=net_id,page=request.args.get('page', type=int)))
@base.get('/network/<int:net_id>/delete') @base.get('/network/<int:net_id>/delete')
@@ -206,6 +225,7 @@ def network_delete(net_id):
if db_alloc != None: if db_alloc != None:
for alloc in db_alloc: for alloc in db_alloc:
db.session.delete(alloc) db.session.delete(alloc)
db.session.delete(db_network) db.session.delete(db_network)
db.session.commit() db.session.commit()
flash("Network Deleted", 'warning') flash("Network Deleted", 'warning')
@@ -230,7 +250,6 @@ def allocation_search(net_id):
# AND # AND
if ',' in search: if ',' in search:
for alloc in db_alloc: for alloc in db_alloc:
print(search.split(','))
if alloc.ipv4.lower() in search.split(','): if alloc.ipv4.lower() in search.split(','):
obj_list.append(alloc) obj_list.append(alloc)
# BETWEEN # 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: if int(alloc.ipv4.split('.')[-1]) <= temp_2 and int(alloc.ipv4.split('.')[-1]) >= temp_1:
obj_list.append(alloc) obj_list.append(alloc)
# DHCP # DHCP
print(search)
if 'dhcp' in search: if 'dhcp' in search:
for alloc in db_alloc: for alloc in db_alloc:
if alloc.is_dhcp: if alloc.is_dhcp:
@@ -256,6 +274,19 @@ def allocation_search(net_id):
for alloc in db_alloc: for alloc in db_alloc:
if alloc.is_gateway: if alloc.is_gateway:
obj_list.append(alloc) 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: else:
# BASIC SEARCH # BASIC SEARCH
for alloc in db_alloc: for alloc in db_alloc:
@@ -268,4 +299,4 @@ def allocation_search(net_id):
if alloc.description.lower().startswith(search): if alloc.description.lower().startswith(search):
obj_list.append(alloc) 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> <tr>
<td><b>{{allocation.ipv4}}</b></td> <td><b>{{allocation.ipv4}}</b></td>
<td>{{allocation.hostname}}</td> <td>{{allocation.hostname}}</td>
@@ -10,6 +10,9 @@
N/A N/A
{% endif %} {% endif %}
</td> </td>
<td>{{allocation.is_dhcp}}</td>
<td>{{allocation.is_gateway}}</td>
<td>{{allocation.is_decom}}</td>
<td> <td>
{% if allocation.is_used %} {% if allocation.is_used %}
<button <button
@@ -22,13 +25,18 @@
style="padding-top:1px;padding-bottom:1px" style="padding-top:1px;padding-bottom:1px"
> Edit > Edit
</button> </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 <button
hx-get="{{ url_for('base.allocation_del_modal',net_id=network.id,ipv4=allocation.ipv4) }}?page={{active_page}}" hx-get="{{ url_for('base.allocation_del_modal',net_id=network.id,ipv4=allocation.ipv4) }}?page={{active_page}}"
hx-trigger="click" hx-trigger="click"
hx-target="#modal-del" hx-target="#modal-del"
data-bs-toggle="modal" data-bs-toggle="modal"
data-bs-target="#modal-del" 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" style="padding-top:1px;padding-bottom:1px"
> Unallocate > Unallocate
</button> </button>
@@ -46,4 +54,4 @@
{% endif %} {% endif %}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}
+4 -49
View File
@@ -25,59 +25,14 @@
<th scope="col">Hostname</th> <th scope="col">Hostname</th>
<th scope="col">Description</th> <th scope="col">Description</th>
<th scope="col">Device</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> <th scope="col">Actions</th>
</tr> </tr>
</thead> </thead>
<tbody id="search-results"> <tbody id="search-results">
{% for allocation in allocations %} {% include 'panel/_table-alloc.html' %}
<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 %}
</tbody> </tbody>
</table> </table>
</div> </div>