From 4628df54e186f3b8a331a0178177afc24f759678 Mon Sep 17 00:00:00 2001 From: Takeus <68923554+TakeusV1@users.noreply.github.com> Date: Wed, 12 Feb 2025 12:01:08 +0100 Subject: [PATCH] V1.1 --- app/_config.py | 2 +- app/forms.py | 3 +- app/models.py | 2 + app/routes/r_base.py | 80 ++++++++++++++++++++++++-- app/templates/panel/_modal.html | 4 +- app/templates/panel/_search-entry.html | 49 ++++++++++++++++ app/templates/panel/allocations.html | 37 ++++++++++-- app/templates/panel/networks.html | 2 +- 8 files changed, 163 insertions(+), 16 deletions(-) create mode 100644 app/templates/panel/_search-entry.html diff --git a/app/_config.py b/app/_config.py index 58f195b..6052792 100644 --- a/app/_config.py +++ b/app/_config.py @@ -8,7 +8,7 @@ app_debug = True app_maintenance = False app_name = 'takeus-ipam' -app_version = 'V1.0' +app_version = 'V1.1' app_release_date = '02/2025' app_url = "http://127.0.0.1" diff --git a/app/forms.py b/app/forms.py index 013827a..36dcce2 100644 --- a/app/forms.py +++ b/app/forms.py @@ -28,4 +28,5 @@ class ChangePasswordForm(FlaskForm): class ChangeAllocationForm(FlaskForm): hostname = StringField(validators=[Length(max=128)]) - description = StringField(validators=[Length(max=128)]) \ No newline at end of file + description = StringField(validators=[Length(max=128)]) + is_special = SelectField(choices=[(0,'None'),(1,'DHCP'),(2,'Gateway')],validators=[DataRequired()],label='Special USE ?') diff --git a/app/models.py b/app/models.py index 99ceccf..95d6c82 100644 --- a/app/models.py +++ b/app/models.py @@ -21,6 +21,8 @@ class Allocation(db.Model): hostname = db.Column(db.String(32)) description = db.Column(db.String(64)) is_used = db.Column(db.Boolean(),default=False) + is_dhcp = db.Column(db.Boolean(),default=False) + is_gateway = 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 a980138..b08d998 100644 --- a/app/routes/r_base.py +++ b/app/routes/r_base.py @@ -129,11 +129,11 @@ def network_del_modal(net_id): @login_required def allocation_del_modal(net_id,ipv4): modal = { - 'title': 'Delete Allocation', - 'body': f'Are you sure you want to delete {ipv4} allocation ?', + 'title': 'Remove Allocation', + 'body': f'Are you sure you want to remove {ipv4} allocation ?', 'color': 'warning', 'action': 'delete', - 'action_text': 'Delete', + 'action_text': 'Remove', 'action_url': url_for('base.allocation_delete',net_id=net_id,ipv4=ipv4) } return render_template('panel/_modal.html', modal=modal, net_id=net_id, ipv4=ipv4, old_page=request.args.get('page', type=int), pagination_active=True) @@ -148,6 +148,23 @@ def allocation_edit_modal(net_id, ipv4): if form.validate_on_submit() and request.method == 'POST': db_alloc.hostname = str(form.hostname.data) db_alloc.description = str(form.description.data) + db_alloc.is_used = True + # Special Allocation + if int(form.is_special.data) == 1: + # DHCP + db_alloc.is_dhcp == True + db_alloc.is_gateway == False + db_alloc.hostname = None + db_alloc.description = "DHCP Allocation" + elif int(form.is_special.data) == 2: + # Gateway + db_alloc.is_dhcp == False + db_alloc.is_gateway == True + else: + # None + db_alloc.is_dhcp == False + db_alloc.is_gateway == False + db.session.commit() flash("Allocation Updated", 'success') return redirect(url_for('base.allocations',net_id=net_id,page=request.args.get('page', type=int))) @@ -163,12 +180,13 @@ def allocation_edit_modal(net_id, ipv4): return render_template('panel/_modal.html', modal=modal, form=form, old_page=request.args.get('page', type=int), pagination_active=True) ## MODIFICATION ROUTES - -@base.get('/network//allocations//delete') +@base.get('/network//allocations//unallocate') @login_required def allocation_delete(net_id,ipv4): db_alloc = Allocation.query.filter_by(net_id=net_id,ipv4=ipv4).first() - db.session.delete(db_alloc) + db_alloc.is_used = False + db_alloc.hostname = None + db_alloc.description = None db.session.commit() flash("Allocation Deleted", 'warning') return redirect(url_for('base.allocations',net_id=net_id,page=request.args.get('page', type=int))) @@ -190,3 +208,53 @@ def network_delete(net_id): else: flash("Network Not Found", 'danger') return redirect(url_for('base.networks')) + +## HTMX SEARCH +@base.post('/network//allocations/search') +@login_required +def allocation_search(net_id): + search = str(request.form['search']).lower() + + db_network = Network.query.filter_by(id=net_id).first() + db_alloc = Allocation.query.filter_by(net_id=net_id).all() + obj_list = [] + + # Advance Search + if '*' in search: + search = search.split("*")[1] + # AND + if ',' in search: + for alloc in db_alloc: + print(search.split(',')) + if alloc.ipv4.lower() in search.split(','): + obj_list.append(alloc) + # BETWEEN + if '-' in search: + search = search.split('-') + if search[0].split('.')[-1] == '' or search[1].split('.')[-1] == '': + return "Invalid(s) Number(s)" + temp_1 = int(search[0].split('.')[-1]) + temp_2 = int(search[1].split('.')[-1]) + + for alloc in db_alloc: + 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: + obj_list.append(alloc) + else: + # BASIC SEARCH + for alloc in db_alloc: + if alloc.ipv4.lower().startswith(search): + obj_list.append(alloc) + elif alloc.hostname != None: + if alloc.hostname.lower().startswith(search): + obj_list.append(alloc) + elif alloc.description != None: + 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 diff --git a/app/templates/panel/_modal.html b/app/templates/panel/_modal.html index 64de5fb..ac918b8 100644 --- a/app/templates/panel/_modal.html +++ b/app/templates/panel/_modal.html @@ -9,11 +9,13 @@