2015-02-27 09:28:22 +00:00
|
|
|
import re
|
2020-11-05 09:34:31 +00:00
|
|
|
|
2015-02-27 09:28:22 +00:00
|
|
|
from django import forms
|
2020-10-14 12:27:57 +00:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
2015-02-27 09:28:22 +00:00
|
|
|
|
|
|
|
|
|
|
|
class AddNetPool(forms.Form):
|
2020-11-05 09:34:31 +00:00
|
|
|
name = forms.CharField(error_messages={"required": _("No pool name has been entered")}, max_length=20)
|
|
|
|
subnet = forms.CharField(
|
|
|
|
error_messages={"required": _("No IPv4 subnet has been entered")},
|
|
|
|
max_length=20,
|
|
|
|
required=False,
|
|
|
|
)
|
|
|
|
subnet6 = forms.CharField(
|
|
|
|
error_messages={"required": _("No IPv6 subnet has been entered")},
|
|
|
|
max_length=42,
|
|
|
|
required=False,
|
|
|
|
)
|
2015-02-27 09:28:22 +00:00
|
|
|
forward = forms.CharField(max_length=100)
|
2019-11-07 11:07:57 +00:00
|
|
|
dhcp4 = forms.BooleanField(required=False)
|
2019-11-08 15:26:47 +00:00
|
|
|
dhcp6 = forms.BooleanField(required=False)
|
2015-02-27 09:28:22 +00:00
|
|
|
fixed = forms.BooleanField(required=False)
|
|
|
|
bridge_name = forms.CharField(max_length=20, required=False)
|
|
|
|
openvswitch = forms.BooleanField(required=False)
|
|
|
|
|
|
|
|
def clean_name(self):
|
2020-11-05 09:34:31 +00:00
|
|
|
name = self.cleaned_data["name"]
|
|
|
|
have_symbol = re.match(r"^[a-zA-Z0-9\.\_\-]+$", name)
|
2015-02-27 09:28:22 +00:00
|
|
|
if not have_symbol:
|
2020-11-05 09:34:31 +00:00
|
|
|
raise forms.ValidationError(_("The pool name must not contain any special characters"))
|
2015-02-27 09:28:22 +00:00
|
|
|
elif len(name) > 20:
|
2020-11-05 09:34:31 +00:00
|
|
|
raise forms.ValidationError(_("The pool name must not exceed 20 characters"))
|
2015-02-27 09:28:22 +00:00
|
|
|
return name
|
|
|
|
|
|
|
|
def clean_subnet(self):
|
2020-11-05 09:34:31 +00:00
|
|
|
subnet = self.cleaned_data["subnet"]
|
|
|
|
have_symbol = re.match("^[0-9./]+$", subnet if subnet else ".")
|
2015-02-27 09:28:22 +00:00
|
|
|
if not have_symbol:
|
2020-11-05 09:34:31 +00:00
|
|
|
raise forms.ValidationError(_("The IPv4 subnet must not contain any special characters"))
|
2015-02-27 09:28:22 +00:00
|
|
|
elif len(subnet) > 20:
|
2020-11-05 09:34:31 +00:00
|
|
|
raise forms.ValidationError(_("The IPv4 subnet must not exceed 20 characters"))
|
2015-02-27 09:28:22 +00:00
|
|
|
return subnet
|
|
|
|
|
2019-11-08 15:26:47 +00:00
|
|
|
def clean_subnet6(self):
|
2020-11-05 09:34:31 +00:00
|
|
|
subnet = self.cleaned_data["subnet6"]
|
|
|
|
have_symbol = re.match("^[0-9a-fA-F:/]+$", subnet if subnet else ":")
|
2019-11-08 15:26:47 +00:00
|
|
|
if not have_symbol:
|
2020-11-05 09:34:31 +00:00
|
|
|
raise forms.ValidationError(_("The IPv6 subnet must not contain any special characters"))
|
2019-11-08 15:26:47 +00:00
|
|
|
elif len(subnet) > 42:
|
2020-11-05 09:34:31 +00:00
|
|
|
raise forms.ValidationError(_("The IPv6 subnet must not exceed 42 characters"))
|
2019-11-08 15:26:47 +00:00
|
|
|
return subnet
|
|
|
|
|
2015-02-27 09:28:22 +00:00
|
|
|
def clean_bridge_name(self):
|
2020-11-05 09:34:31 +00:00
|
|
|
bridge_name = self.cleaned_data["bridge_name"]
|
|
|
|
if self.cleaned_data["forward"] in ["bridge", "macvtap"]:
|
|
|
|
have_symbol = re.match(r"^[a-zA-Z0-9\.\_\:\-]+$", bridge_name)
|
2015-02-27 09:28:22 +00:00
|
|
|
if not have_symbol:
|
2020-11-05 09:34:31 +00:00
|
|
|
raise forms.ValidationError(_("The pool bridge name must not contain any special characters"))
|
2015-02-27 09:28:22 +00:00
|
|
|
elif len(bridge_name) > 20:
|
2020-11-05 09:34:31 +00:00
|
|
|
raise forms.ValidationError(_("The pool bridge name must not exceed 20 characters"))
|
2015-02-27 09:28:22 +00:00
|
|
|
return bridge_name
|