151 lines
No EOL
4.5 KiB
Python
151 lines
No EOL
4.5 KiB
Python
"""
|
|
Core routes for the NetViz application.
|
|
"""
|
|
from flask import render_template, redirect, url_for, flash, request, current_app, abort
|
|
from flask_login import login_required, current_user
|
|
|
|
from app.core import core_bp
|
|
from app.core.forms import NetworkForm, SubnetForm, DeviceForm, FirewallRuleForm
|
|
from app.models.network import Network, Subnet, Device, FirewallRule
|
|
from app.extensions import db
|
|
from app.utils.visualization import generate_network_diagram
|
|
|
|
|
|
@core_bp.route("/")
|
|
def index():
|
|
"""Landing page route."""
|
|
if current_user.is_authenticated:
|
|
networks = Network.query.filter_by(user_id=current_user.id).all()
|
|
return render_template("core/dashboard.html", networks=networks)
|
|
|
|
return render_template("core/index.html")
|
|
|
|
|
|
@core_bp.route("/dashboard")
|
|
@login_required
|
|
def dashboard():
|
|
"""User dashboard route."""
|
|
networks = Network.query.filter_by(user_id=current_user.id).all()
|
|
return render_template("core/dashboard.html", networks=networks)
|
|
|
|
|
|
# Network CRUD routes
|
|
@core_bp.route("/networks/new", methods=["GET", "POST"])
|
|
@login_required
|
|
def create_network():
|
|
"""Create a new network."""
|
|
form = NetworkForm()
|
|
|
|
if form.validate_on_submit():
|
|
network = Network(
|
|
name=form.name.data,
|
|
description=form.description.data,
|
|
user_id=current_user.id
|
|
)
|
|
|
|
db.session.add(network)
|
|
db.session.commit()
|
|
|
|
flash("Network created successfully", "success")
|
|
return redirect(url_for("core.view_network", network_id=network.id))
|
|
|
|
return render_template("core/network_form.html", form=form, title="Create Network")
|
|
|
|
|
|
@core_bp.route("/networks/<int:network_id>")
|
|
@login_required
|
|
def view_network(network_id):
|
|
"""View a network and its details."""
|
|
network = Network.query.get_or_404(network_id)
|
|
|
|
# Check if the user owns this network
|
|
if network.user_id != current_user.id:
|
|
abort(403)
|
|
|
|
# Generate network visualization
|
|
diagram = generate_network_diagram(network)
|
|
|
|
return render_template(
|
|
"core/network_view.html",
|
|
network=network,
|
|
diagram=diagram,
|
|
subnets=network.subnets,
|
|
devices=network.devices,
|
|
firewall_rules=network.firewall_rules
|
|
)
|
|
|
|
|
|
@core_bp.route("/networks/<int:network_id>/edit", methods=["GET", "POST"])
|
|
@login_required
|
|
def edit_network(network_id):
|
|
"""Edit an existing network."""
|
|
network = Network.query.get_or_404(network_id)
|
|
|
|
# Check if the user owns this network
|
|
if network.user_id != current_user.id:
|
|
abort(403)
|
|
|
|
form = NetworkForm(obj=network)
|
|
|
|
if form.validate_on_submit():
|
|
network.name = form.name.data
|
|
network.description = form.description.data
|
|
|
|
db.session.commit()
|
|
|
|
flash("Network updated successfully", "success")
|
|
return redirect(url_for("core.view_network", network_id=network.id))
|
|
|
|
return render_template("core/network_form.html", form=form, title="Edit Network", network=network)
|
|
|
|
|
|
@core_bp.route("/networks/<int:network_id>/delete", methods=["POST"])
|
|
@login_required
|
|
def delete_network(network_id):
|
|
"""Delete a network."""
|
|
network = Network.query.get_or_404(network_id)
|
|
|
|
# Check if the user owns this network
|
|
if network.user_id != current_user.id:
|
|
abort(403)
|
|
|
|
db.session.delete(network)
|
|
db.session.commit()
|
|
|
|
flash("Network deleted successfully", "success")
|
|
return redirect(url_for("core.dashboard"))
|
|
|
|
|
|
# Subnet routes
|
|
@core_bp.route("/networks/<int:network_id>/subnets/new", methods=["GET", "POST"])
|
|
@login_required
|
|
def create_subnet(network_id):
|
|
"""Create a new subnet within a network."""
|
|
network = Network.query.get_or_404(network_id)
|
|
|
|
# Check if the user owns this network
|
|
if network.user_id != current_user.id:
|
|
abort(403)
|
|
|
|
form = SubnetForm()
|
|
|
|
if form.validate_on_submit():
|
|
subnet = Subnet(
|
|
name=form.name.data,
|
|
cidr=form.cidr.data,
|
|
vlan=form.vlan.data,
|
|
description=form.description.data,
|
|
network_id=network.id
|
|
)
|
|
|
|
db.session.add(subnet)
|
|
db.session.commit()
|
|
|
|
flash("Subnet created successfully", "success")
|
|
return redirect(url_for("core.view_network", network_id=network.id))
|
|
|
|
return render_template("core/subnet_form.html", form=form, title="Create Subnet", network=network)
|
|
|
|
|
|
# Similar routes for devices, firewall rules, etc.
|
|
# For brevity, not all routes are shown here but would follow the same pattern |