diff --git a/app/forms.py b/app/forms.py index 53b231b..7cd387f 100644 --- a/app/forms.py +++ b/app/forms.py @@ -8,11 +8,11 @@ class LoginForm(FlaskForm): class NewPrefixForm(FlaskForm): subnet = StringField(validators=[DataRequired(),IPAddress(ipv4=True),Length(max=128)]) - prefix = IntegerField(validators=[DataRequired(),NumberRange(min=0,max=32)]) + prefix = IntegerField(validators=[DataRequired(),NumberRange(min=20,max=32)]) description = StringField(validators=[Length(max=128)]) class NewAllocationForm(FlaskForm): ipv4 = StringField(validators=[DataRequired(),IPAddress(ipv4=True),Length(max=128)]) - host_name = StringField(validators=[DataRequired(),Length(max=128)]) + host_name = StringField(validators=[Length(max=128)]) host_desc = StringField(validators=[Length(max=128)]) #host_type = SelectField(choices=[(0,'None'),(1,'Computer'),(2,'Server'),(3,'Virtual Machine'),(4,'Network Device'),(5,'Other')],validators=[DataRequired()]) \ No newline at end of file diff --git a/app/models.py b/app/models.py index 5403bbd..329b6a3 100644 --- a/app/models.py +++ b/app/models.py @@ -18,7 +18,7 @@ class Network(db.Model): class Allocation(db.Model): ipv4 = db.Column(db.String(16),primary_key=True,unique=True) net_id = db.Column(db.Integer(),db.ForeignKey('network.id')) - hostname = db.Column(db.String(32),unique=True) + hostname = db.Column(db.String(32)) description = db.Column(db.String(64)) device_id = db.Column(db.Integer(),db.ForeignKey('inventory.id')) diff --git a/app/routes/r_base.py b/app/routes/r_base.py index 1f32b92..b637e6b 100644 --- a/app/routes/r_base.py +++ b/app/routes/r_base.py @@ -44,15 +44,16 @@ def networks(): return redirect(url_for('base.networks')) elif form.errors: flash(form.errors, 'danger') - - return render_template('panel/networks.html', title="Prefixes", is_networks=True, app_version=app_version, prefixes=networks, form=form) + + return render_template('panel/networks.html', title="Prefixes", is_networks=True, app_version=app_version, prefixes=networks, form=form, Allocation=Allocation) @base.route('/network//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() + active_page = request.args.get('page', type=int) + 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 ) form = NewAllocationForm() if request.method == 'POST' and form.validate_on_submit(): @@ -60,24 +61,22 @@ def allocations(net_id): # 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)) + 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)) - # 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 + 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() - return redirect(url_for('base.allocations',net_id=net_id)) + 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)) + return redirect(url_for('base.allocations', net_id=net_id, page=active_page)) + # If Form Errors + 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) + 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) ## MODAL ROUTES @@ -92,7 +91,7 @@ def allocation_del_modal(net_id,ipv4): '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) + return render_template('panel/_modal.html', modal=modal, net_id=net_id, ipv4=ipv4, old_page=request.args.get('page', type=int)) @base.get('/network//del_modal') @login_required @@ -117,7 +116,7 @@ def allocation_delete(net_id,ipv4): db.session.delete(db_alloc) db.session.commit() flash("Allocation Deleted", 'warning') - return redirect(url_for('base.allocations',net_id=net_id)) + return redirect(url_for('base.allocations',net_id=net_id,page=request.args.get('page', type=int))) @base.get('/network//delete') @login_required diff --git a/app/templates/panel/_modal.html b/app/templates/panel/_modal.html index f14d924..efbf393 100644 --- a/app/templates/panel/_modal.html +++ b/app/templates/panel/_modal.html @@ -10,7 +10,7 @@ + {% endblock %} \ No newline at end of file diff --git a/app/templates/panel/networks.html b/app/templates/panel/networks.html index 832b417..dede52a 100644 --- a/app/templates/panel/networks.html +++ b/app/templates/panel/networks.html @@ -23,7 +23,7 @@ {{network.id}} {{network.network_address}} {{network.prefix}} - X / {{network.hosts}} + {{Allocation.query.filter_by(net_id=network.id).count()}} / {{network.hosts}} {{network.description}} Allocations