2015-01-25 00:38:42 +01:00
|
|
|
"""
|
|
|
|
This module defines form classes for domain editing.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
from __future__ import absolute_import
|
2015-01-25 00:38:42 +01:00
|
|
|
|
2015-01-27 16:41:44 +01:00
|
|
|
import re
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from crispy_forms.helper import FormHelper
|
|
|
|
from crispy_forms.layout import Layout, Submit
|
2015-01-25 00:38:42 +01:00
|
|
|
from django import forms
|
2018-11-19 23:28:40 +01:00
|
|
|
from django.urls import reverse
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext as _
|
2015-01-25 00:38:42 +01:00
|
|
|
|
|
|
|
from .models import HostingDomain
|
|
|
|
|
|
|
|
|
2015-01-27 16:41:44 +01:00
|
|
|
def relative_domain_validator(value):
|
|
|
|
"""
|
2015-10-15 21:50:18 +02:00
|
|
|
This validator ensures that the given value is a valid lowercase domain
|
|
|
|
name.
|
|
|
|
|
2015-01-27 16:41:44 +01:00
|
|
|
"""
|
|
|
|
if len(value) > 254:
|
2023-02-18 22:46:48 +01:00
|
|
|
raise forms.ValidationError(_("host name too long"), code="too-long")
|
2015-01-27 16:41:44 +01:00
|
|
|
allowed = re.compile(r"(?!-)[a-z\d-]{1,63}(?<!-)$")
|
2023-02-18 22:46:48 +01:00
|
|
|
if not all(allowed.match(x) for x in value.split(".")):
|
|
|
|
raise forms.ValidationError(_("invalid domain name"))
|
2015-01-27 16:41:44 +01:00
|
|
|
|
|
|
|
|
2015-01-25 00:38:42 +01:00
|
|
|
class CreateHostingDomainForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
This form is used to create new HostingDomain instances.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-25 00:38:42 +01:00
|
|
|
class Meta:
|
|
|
|
model = HostingDomain
|
2023-02-18 22:46:48 +01:00
|
|
|
fields = ["domain"]
|
2015-01-25 00:38:42 +01:00
|
|
|
|
|
|
|
def __init__(self, instance, *args, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.hosting_package = kwargs.pop("hostingpackage")
|
2015-01-25 00:38:42 +01:00
|
|
|
super(CreateHostingDomainForm, self).__init__(*args, **kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.fields["domain"].validators.append(relative_domain_validator)
|
2015-01-25 00:38:42 +01:00
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_action = reverse(
|
2023-02-18 22:46:48 +01:00
|
|
|
"create_hosting_domain", kwargs={"package": self.hosting_package.id}
|
|
|
|
)
|
2015-01-25 00:38:42 +01:00
|
|
|
self.helper.layout = Layout(
|
2023-02-18 22:46:48 +01:00
|
|
|
"domain",
|
|
|
|
Submit("submit", _("Add Hosting Domain")),
|
2015-01-25 00:38:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
self.cleaned_data = super(CreateHostingDomainForm, self).clean()
|
2023-02-18 22:46:48 +01:00
|
|
|
self.cleaned_data["hosting_package"] = self.hosting_package
|
2015-01-25 00:38:42 +01:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
return HostingDomain.objects.create_for_hosting_package(
|
2023-02-18 22:46:48 +01:00
|
|
|
commit=commit, **self.cleaned_data
|
|
|
|
)
|
2015-01-25 00:38:42 +01:00
|
|
|
|
|
|
|
def save_m2m(self):
|
|
|
|
pass
|