1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-27 16:45:22 +00:00
webvirtcloud/interfaces/forms.py

109 lines
3.9 KiB
Python
Raw Normal View History

2015-02-27 09:28:22 +00:00
import re
2015-02-27 09:28:22 +00:00
from django import forms
from django.utils.translation import gettext_lazy as _
2015-02-27 09:28:22 +00:00
class AddInterface(forms.Form):
name = forms.CharField(max_length=10, required=True)
2022-11-02 13:05:41 +00:00
itype = forms.ChoiceField(
required=True, choices=(("bridge", "bridge"), ("ethernet", "ethernet"))
)
2022-11-02 08:32:44 +00:00
start_mode = forms.ChoiceField(
required=True,
2022-11-02 13:05:41 +00:00
choices=(("none", "none"), ("onboot", "onboot"), ("hotplug", "hotplug")),
2022-11-02 08:32:44 +00:00
)
2015-02-27 09:28:22 +00:00
netdev = forms.CharField(max_length=15, required=True)
2022-11-02 08:32:44 +00:00
ipv4_type = forms.ChoiceField(
required=True,
2022-11-02 13:05:41 +00:00
choices=(("dhcp", "dhcp"), ("static", "static"), ("none", "none")),
2022-11-02 08:32:44 +00:00
)
2015-02-27 09:28:22 +00:00
ipv4_addr = forms.CharField(max_length=18, required=False)
ipv4_gw = forms.CharField(max_length=15, required=False)
2022-11-02 08:32:44 +00:00
ipv6_type = forms.ChoiceField(
required=True,
2022-11-02 13:05:41 +00:00
choices=(("dhcp", "dhcp"), ("static", "static"), ("none", "none")),
2022-11-02 08:32:44 +00:00
)
2015-02-27 09:28:22 +00:00
ipv6_addr = forms.CharField(max_length=100, required=False)
ipv6_gw = forms.CharField(max_length=100, required=False)
2022-11-02 13:05:41 +00:00
stp = forms.ChoiceField(required=False, choices=(("on", "on"), ("off", "off")))
2015-02-27 09:28:22 +00:00
delay = forms.IntegerField(required=False)
def clean_ipv4_addr(self):
2022-11-02 13:05:41 +00:00
ipv4_addr = self.cleaned_data["ipv4_addr"]
have_symbol = re.match("^[0-9./]+$", ipv4_addr)
2015-02-27 09:28:22 +00:00
if not have_symbol:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The IPv4 address must not contain any special characters")
)
2015-02-27 09:28:22 +00:00
elif len(ipv4_addr) > 20:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The IPv4 address must not exceed 20 characters")
)
2015-02-27 09:28:22 +00:00
return ipv4_addr
def clean_ipv4_gw(self):
2022-11-02 13:05:41 +00:00
ipv4_gw = self.cleaned_data["ipv4_gw"]
have_symbol = re.match("^[0-9.]+$", ipv4_gw)
2015-02-27 09:28:22 +00:00
if not have_symbol:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The IPv4 gateway must not contain any special characters")
)
2015-02-27 09:28:22 +00:00
elif len(ipv4_gw) > 20:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The IPv4 gateway must not exceed 20 characters")
)
2015-02-27 09:28:22 +00:00
return ipv4_gw
def clean_ipv6_addr(self):
2022-11-02 13:05:41 +00:00
ipv6_addr = self.cleaned_data["ipv6_addr"]
have_symbol = re.match("^[0-9a-f./:]+|^$", ipv6_addr)
2015-02-27 09:28:22 +00:00
if not have_symbol:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The IPv6 address must not contain any special characters")
)
2015-02-27 09:28:22 +00:00
elif len(ipv6_addr) > 100:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The IPv6 address must not exceed 100 characters")
)
2015-02-27 09:28:22 +00:00
return ipv6_addr
def clean_ipv6_gw(self):
2022-11-02 13:05:41 +00:00
ipv6_gw = self.cleaned_data["ipv6_gw"]
have_symbol = re.match("^[0-9a-f./:]+|^$", ipv6_gw)
2015-02-27 09:28:22 +00:00
if not have_symbol:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The IPv6 gateway must not contain any special characters")
)
2015-02-27 09:28:22 +00:00
elif len(ipv6_gw) > 100:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The IPv6 gateway must not exceed 100 characters")
)
2015-02-27 09:28:22 +00:00
return ipv6_gw
def clean_name(self):
2022-11-02 13:05:41 +00:00
name = self.cleaned_data["name"]
have_symbol = re.match("^[a-z0-9.]+$", name)
2015-02-27 09:28:22 +00:00
if not have_symbol:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The interface must not contain any special characters")
)
2015-02-27 09:28:22 +00:00
elif len(name) > 10:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The interface must not exceed 10 characters")
)
2015-02-27 09:28:22 +00:00
return name
def clean_netdev(self):
2022-11-02 13:05:41 +00:00
netdev = self.cleaned_data["netdev"]
have_symbol = re.match("^[a-z0-9.:]+$", netdev)
2015-02-27 09:28:22 +00:00
if not have_symbol:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The interface must not contain any special characters")
)
2015-02-27 09:28:22 +00:00
elif len(netdev) > 10:
2022-11-02 13:05:41 +00:00
raise forms.ValidationError(
_("The interface must not exceed 10 characters")
)
2015-02-27 09:28:22 +00:00
return netdev