95 lines
No EOL
3.3 KiB
Python
95 lines
No EOL
3.3 KiB
Python
"""
|
|
Forms for the core functionality of the NetViz application.
|
|
"""
|
|
from flask_wtf import FlaskForm
|
|
from wtforms import StringField, TextAreaField, IntegerField, SelectField, SubmitField
|
|
from wtforms.validators import DataRequired, Length, Optional, NumberRange, ValidationError
|
|
import ipaddress
|
|
|
|
|
|
class NetworkForm(FlaskForm):
|
|
"""Form for creating and editing networks."""
|
|
name = StringField("Network Name", validators=[
|
|
DataRequired(),
|
|
Length(min=3, max=128)
|
|
])
|
|
description = TextAreaField("Description", validators=[Optional()])
|
|
submit = SubmitField("Save Network")
|
|
|
|
|
|
class SubnetForm(FlaskForm):
|
|
"""Form for creating and editing subnets."""
|
|
name = StringField("Subnet Name", validators=[
|
|
DataRequired(),
|
|
Length(min=3, max=128)
|
|
])
|
|
cidr = StringField("CIDR Notation", validators=[DataRequired()])
|
|
vlan = IntegerField("VLAN ID", validators=[
|
|
Optional(),
|
|
NumberRange(min=0, max=4095)
|
|
])
|
|
description = TextAreaField("Description", validators=[Optional()])
|
|
submit = SubmitField("Save Subnet")
|
|
|
|
def validate_cidr(self, cidr):
|
|
"""Validate CIDR notation."""
|
|
try:
|
|
ipaddress.IPv4Network(cidr.data, strict=False)
|
|
except ValueError:
|
|
try:
|
|
ipaddress.IPv6Network(cidr.data, strict=False)
|
|
except ValueError:
|
|
raise ValidationError("Invalid CIDR notation. Example formats: 192.168.1.0/24 or 2001:db8::/64")
|
|
|
|
|
|
class DeviceForm(FlaskForm):
|
|
"""Form for creating and editing devices."""
|
|
name = StringField("Device Name", validators=[
|
|
DataRequired(),
|
|
Length(min=3, max=128)
|
|
])
|
|
ip_address = StringField("IP Address", validators=[Optional()])
|
|
mac_address = StringField("MAC Address", validators=[Optional()])
|
|
device_type = SelectField("Device Type", choices=[
|
|
("server", "Server"),
|
|
("router", "Router"),
|
|
("switch", "Switch"),
|
|
("firewall", "Firewall"),
|
|
("client", "Client"),
|
|
("other", "Other")
|
|
])
|
|
os = StringField("Operating System", validators=[Optional()])
|
|
subnet_id = SelectField("Subnet", coerce=int, validators=[Optional()])
|
|
description = TextAreaField("Description", validators=[Optional()])
|
|
submit = SubmitField("Save Device")
|
|
|
|
def validate_ip_address(self, ip_address):
|
|
"""Validate IP address format."""
|
|
if ip_address.data:
|
|
try:
|
|
ipaddress.ip_address(ip_address.data)
|
|
except ValueError:
|
|
raise ValidationError("Invalid IP address format")
|
|
|
|
|
|
class FirewallRuleForm(FlaskForm):
|
|
"""Form for creating and editing firewall rules."""
|
|
name = StringField("Rule Name", validators=[
|
|
DataRequired(),
|
|
Length(min=3, max=128)
|
|
])
|
|
source = StringField("Source", validators=[DataRequired()])
|
|
destination = StringField("Destination", validators=[DataRequired()])
|
|
protocol = SelectField("Protocol", choices=[
|
|
("any", "Any"),
|
|
("tcp", "TCP"),
|
|
("udp", "UDP"),
|
|
("icmp", "ICMP")
|
|
])
|
|
port_range = StringField("Port Range", validators=[Optional()])
|
|
action = SelectField("Action", choices=[
|
|
("allow", "Allow"),
|
|
("deny", "Deny")
|
|
])
|
|
description = TextAreaField("Description", validators=[Optional()])
|
|
submit = SubmitField("Save Rule") |