mirror of
https://github.com/TakeusV1/flask_ipam.git
synced 2026-10-13 17:15:33 +02:00
V1.0
This commit is contained in:
1 parent
a8d9727527
commit
602bb7d706
8 files changed
+174
-21
No files matched your search
+76
-8
@@ -47,24 +47,92 @@ def networks():
|
||||
|
||||
return render_template('panel/networks.html', title="Prefixes", is_networks=True, app_version=app_version, prefixes=networks, form=form)
|
||||
|
||||
@base.route('/network/allocations/<int:net_id>',methods=['GET','POST'])
|
||||
@base.route('/network/<int:net_id>/allocations',methods=['GET','POST'])
|
||||
@login_required
|
||||
def allocations(net_id):
|
||||
|
||||
db_allocations = Allocation.query.filter_by(net_id=net_id).all()
|
||||
db_network = Network.query.filter_by(id=net_id).first()
|
||||
|
||||
db_network = Network.query.filter_by(id=net_id).first()
|
||||
|
||||
form = NewAllocationForm()
|
||||
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))
|
||||
# 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.hostname.data),description=str(form.description.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))
|
||||
# Add Inventory if Host Type is not None
|
||||
##if int(form.host_type.data) != 0:
|
||||
## add_inv = Inventory(ip_addr=str(form.ipv4.data)+',',host_type=form.host_type.data,host_name=form.host_name.data,host_desc=form.host_desc.data)
|
||||
## db.session.add(add_inv)
|
||||
##
|
||||
## mod_alloc = Allocation.query.filter_by(ipv4=str(form.ipv4.data)).first()
|
||||
## mod_alloc.device_id = add_inv.id
|
||||
db.session.add(add_alloc)
|
||||
db.session.commit()
|
||||
return redirect(url_for('base.allocations',net_id=net_id))
|
||||
|
||||
return 'nok'
|
||||
flash("Invalid IPv4 Address", 'danger')
|
||||
return redirect(url_for('base.allocations',net_id=net_id))
|
||||
|
||||
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)
|
||||
|
||||
## MODAL ROUTES
|
||||
|
||||
@base.get('/network/<int:net_id>/allocations/<string:ipv4>/del_modal')
|
||||
@login_required
|
||||
def allocation_del_modal(net_id,ipv4):
|
||||
modal = {
|
||||
'title': 'Delete Allocation',
|
||||
'body': f'Are you sure you want to delete <b>{ipv4}</b> allocation ?',
|
||||
'color': 'warning',
|
||||
'action': 'delete',
|
||||
'action_text': 'Delete',
|
||||
'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)
|
||||
|
||||
@base.get('/network/<int:net_id>/del_modal')
|
||||
@login_required
|
||||
def network_del_modal(net_id):
|
||||
db_network = Network.query.filter_by(id=net_id).first()
|
||||
modal = {
|
||||
'title': 'Delete Network',
|
||||
'body': f'Are you sure you want to delete <b>{db_network.network_address}/{db_network.prefix}</b> network ?',
|
||||
'color': 'danger',
|
||||
'action': 'delete',
|
||||
'action_text': 'Delete',
|
||||
'action_url': url_for('base.network_delete',net_id=net_id)
|
||||
}
|
||||
return render_template('panel/_modal.html', modal=modal, net_id=net_id)
|
||||
|
||||
## DELETE ROUTES
|
||||
|
||||
@base.get('/network/<int:net_id>/allocations/<string:ipv4>/delete')
|
||||
@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.session.commit()
|
||||
flash("Allocation Deleted", 'warning')
|
||||
return redirect(url_for('base.allocations',net_id=net_id))
|
||||
|
||||
@base.get('/network/<int:net_id>/delete')
|
||||
@login_required
|
||||
def network_delete(net_id):
|
||||
db_network = Network.query.filter_by(id=net_id).first()
|
||||
db_alloc = Allocation.query.filter_by(net_id=db_network.id).all()
|
||||
|
||||
|
||||
return render_template('panel/allocations.html', title="Allocations", is_networks=True, app_version=app_version, form=form, allocations=db_allocations, network=db_network)
|
||||
if db_network != None:
|
||||
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')
|
||||
return redirect(url_for('base.networks'))
|
||||
else:
|
||||
flash("Network Not Found", 'danger')
|
||||
return redirect(url_for('base.networks'))
|
||||
Reference in new issue
Block a user