This commit is contained in:
Takeus committed 2025-02-05 13:25:36 +01:00
1 parent 9f9f90422a
commit 4870f0b089
12 files changed
+433 -68

No files matched your search

+87 -11
View File
@@ -1,26 +1,102 @@
from datetime import datetime
from flask import Blueprint, request, render_template, flash, url_for, redirect, abort
from flask_login import login_required, current_user
from flask_login import login_required, current_user, logout_user
from werkzeug.security import generate_password_hash
from app._config import app_version
from app.extensions import *
from app.models import *
from app.forms import NewUserForm, ChangePasswordForm
admin = Blueprint('admin', __name__)
@admin.route('/')
@admin.route('/', methods=['GET','POST'])
@login_required
def home():
if current_user.is_admin:
return redirect(url_for('admin.dash'))
else:
return abort(403)
@admin.route('/dashboard')
@login_required
def dash():
if current_user.is_admin == False:
return abort(403)
return f"Welcome {current_user.username} to the admin dashboard"
db_users = User.query.all()
form = NewUserForm()
if form.validate_on_submit() and request.method == 'POST':
# IF user exist
if User.query.filter_by(username=form.username.data).first() != None:
flash("User already exist", 'warning')
return redirect(url_for('admin.home'))
is_admin = False
if int(form.is_admin.data) == 1:
is_admin = True
db.session.add(User(username=form.username.data, password=generate_password_hash(form.password.data), is_admin=is_admin))
db.session.commit()
flash("User Created", 'success')
return redirect(url_for('admin.home'))
return render_template('admin/home.html', title="Admin", app_version=app_version, is_admin=True, navcolor='danger', users=db_users, form=form)
## MODAL ROUTES
@admin.get('/user/<int:user_id>/modal/delete')
@login_required
def user_modal_delete(user_id):
if current_user.is_admin == False:
return abort(403)
user = User.query.get(user_id)
modal = {
'title': 'Delete User',
'body': f'Are you sure you want to delete <b>{user.username}</b> user ?',
'color': 'danger',
'action': 'delete',
'action_text': 'Delete',
'action_url': url_for('admin.delete_user',user_id=user.id)
}
return render_template('panel/_modal.html', modal=modal)
@admin.route('/user/<int:user_id>/modal/edit', methods=['GET','POST'])
@login_required
def user_modal_edit(user_id):
if current_user.is_admin == False:
return abort(403)
user = User.query.get(user_id)
form = ChangePasswordForm()
if form.validate_on_submit() and request.method == 'POST':
user.password = generate_password_hash(form.password.data)
user.is_admin = False
if int(form.is_admin.data) == 1:
user.is_admin = True
db.session.commit()
flash("Informations Changed", 'success')
return redirect(url_for('admin.home'))
modal = {
'title': 'Edit User',
'body': f'Change Informations for <b>{user.username}</b>',
'color': 'primary',
'modal_type': 'form',
'action_text': 'Save',
'form_url': url_for('admin.user_modal_edit',user_id=user.id),
}
return render_template('panel/_modal.html', modal=modal, form=form)
## MODIFICATION ROUTES
@admin.get('/user/<int:user_id>/delete')
@login_required
def delete_user(user_id):
if current_user.is_admin == False:
return abort(403)
if User.query.filter_by(is_admin=True).count() == 1 and User.query.get(user_id).is_admin == True:
flash("You can't delete the last admin user !", 'danger')
return redirect(url_for('admin.home'))
user = User.query.get(user_id)
db.session.delete(user)
db.session.commit()
flash("User Deleted", 'warning')
return redirect(url_for('admin.home'))
+64 -19
View File
@@ -7,7 +7,7 @@ from app._config import app_version
from app.extensions import *
from app.models import *
from app.forms import NewPrefixForm, NewAllocationForm
from app.forms import NewPrefixForm, NewAllocationForm, ChangeAllocationForm
base = Blueprint('base', __name__)
@@ -22,7 +22,28 @@ def home():
@login_required
def dashboard():
return render_template('panel/dashboard.html', title="Dashboard", is_dash=True, app_version=app_version)
db_networks = Network.query.count()
db_allocations = Allocation.query.count()
db_inventory = Inventory.query.count()
db_users = User.query.count()
available_alloc = 0
for available_ip in Network.query.all():
available_alloc += int(available_ip.hosts)
return render_template(
'panel/dashboard.html',
title="Dashboard",
is_dash=True,
app_version=app_version,
## Variables
db_networks=db_networks,
db_allocations=db_allocations,
db_inventory=db_inventory,
available_alloc=available_alloc,
db_users=db_users,
navcolor='dark'
)
@base.route('/network',methods=['GET','POST'])
@login_required
@@ -45,7 +66,7 @@ def 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, Allocation=Allocation)
return render_template('panel/networks.html', title="Prefixes", is_networks=True, app_version=app_version, prefixes=networks, form=form, Allocation=Allocation, navcolor='dark')
@base.route('/network/<int:net_id>/allocations',methods=['GET','POST'])
@login_required
@@ -53,7 +74,7 @@ def allocations(net_id):
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 )
db_allocations = db.paginate(db.select(Allocation).filter_by(net_id=net_id), per_page=12 )
form = NewAllocationForm()
if request.method == 'POST' and form.validate_on_submit():
@@ -76,23 +97,10 @@ def allocations(net_id):
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)
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, navcolor='dark')
## 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, old_page=request.args.get('page', type=int))
@base.get('/network/<int:net_id>/del_modal')
@login_required
def network_del_modal(net_id):
@@ -107,7 +115,44 @@ def network_del_modal(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>/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, old_page=request.args.get('page', type=int), pagination_active=True)
@base.route('/network/<int:net_id>/allocations/<string:ipv4>/edit_modal', methods=['GET','POST'])
@login_required
def allocation_edit_modal(net_id, ipv4):
db_alloc = Allocation.query.filter_by(ipv4=ipv4).first()
form = ChangeAllocationForm()
if form.validate_on_submit() and request.method == 'POST':
db_alloc.hostname = str(form.hostname.data)
db_alloc.description = str(form.description.data)
db.session.commit()
flash("Allocation Updated", 'success')
return redirect(url_for('base.allocations',net_id=net_id,page=request.args.get('page', type=int)))
modal = {
'title': 'Update Allocation',
'body': f'Change informations for <b>{db_alloc.ipv4}</b> allocation.',
'color': 'primary',
'modal_type': 'form',
'action_text': 'Save',
'form_url': url_for('base.allocation_edit_modal',net_id=db_alloc.net_id,ipv4=db_alloc.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/<int:net_id>/allocations/<string:ipv4>/delete')
@login_required