diff --git a/app/models.py b/app/models.py index 95d6c82..f7842f4 100644 --- a/app/models.py +++ b/app/models.py @@ -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): diff --git a/app/routes/r_base.py b/app/routes/r_base.py index 1e05ec7..cef2f08 100644 --- a/app/routes/r_base.py +++ b/app/routes/r_base.py @@ -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//allocations//decom/') +@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//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) \ No newline at end of file + return render_template('panel/_table-alloc.html',allocations=obj_list,Inventory=Inventory,network=db_network) \ No newline at end of file diff --git a/app/templates/panel/_search-entry.html b/app/templates/panel/_table-alloc.html similarity index 70% rename from app/templates/panel/_search-entry.html rename to app/templates/panel/_table-alloc.html index dea87fc..f09d178 100644 --- a/app/templates/panel/_search-entry.html +++ b/app/templates/panel/_table-alloc.html @@ -1,4 +1,4 @@ -{% for allocation in db_alloc %} +{% for allocation in allocations %} {{allocation.ipv4}} {{allocation.hostname}} @@ -10,6 +10,9 @@ N/A {% endif %} + {{allocation.is_dhcp}} + {{allocation.is_gateway}} + {{allocation.is_decom}} {% if allocation.is_used %} + {% if allocation.is_decom %} + Undecom + {% else %} + Decom + {% endif %} @@ -46,4 +54,4 @@ {% endif %} -{% endfor %} \ No newline at end of file +{% endfor %} diff --git a/app/templates/panel/allocations.html b/app/templates/panel/allocations.html index fe2245f..6ea42ba 100644 --- a/app/templates/panel/allocations.html +++ b/app/templates/panel/allocations.html @@ -25,59 +25,14 @@ Hostname Description Device + DHCP + GATEWAY + DECOM Actions - {% for allocation in allocations %} - - {{allocation.ipv4}} - {{allocation.hostname}} - {{allocation.description}} - - {% if Inventory.query.filter_by(id=allocation.device_id).first() %} - link - {% else %} - N/A - {% endif %} - - - {% if allocation.is_used %} - - - {% else %} - - {% endif %} - - - {% endfor %} + {% include 'panel/_table-alloc.html' %}