2015-01-26 22:47:49 +01:00
|
|
|
"""
|
|
|
|
This module defines form classes for website editing.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
from __future__ import absolute_import
|
2015-01-26 22:47:49 +01:00
|
|
|
|
|
|
|
from crispy_forms.bootstrap import AppendedText
|
|
|
|
from crispy_forms.helper import FormHelper
|
2023-02-18 22:46:48 +01:00
|
|
|
from crispy_forms.layout import Layout, Submit
|
|
|
|
from django import forms
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils.translation import gettext as _
|
2015-01-26 22:47:49 +01:00
|
|
|
|
2015-01-27 16:41:44 +01:00
|
|
|
from domains.forms import relative_domain_validator
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-26 22:47:49 +01:00
|
|
|
from .models import Website
|
|
|
|
|
|
|
|
|
|
|
|
class AddWebsiteForm(forms.ModelForm):
|
|
|
|
"""
|
|
|
|
This form is used to create new Website instances.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-26 22:47:49 +01:00
|
|
|
class Meta:
|
|
|
|
model = Website
|
2023-02-18 22:46:48 +01:00
|
|
|
fields = ["subdomain", "wildcard"]
|
2015-01-26 22:47:49 +01:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.hosting_package = kwargs.pop("hostingpackage")
|
|
|
|
self.hosting_domain = kwargs.pop("domain")
|
2015-01-26 22:47:49 +01:00
|
|
|
super(AddWebsiteForm, self).__init__(*args, **kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.fields["subdomain"].validators.append(relative_domain_validator)
|
|
|
|
if Website.objects.filter(wildcard=True, domain=self.hosting_domain).exists():
|
|
|
|
self.fields["wildcard"].widget = forms.HiddenInput()
|
2015-01-26 22:47:49 +01:00
|
|
|
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_action = reverse(
|
2023-02-18 22:46:48 +01:00
|
|
|
"add_website",
|
|
|
|
kwargs={
|
|
|
|
"package": self.hosting_package.id,
|
|
|
|
"domain": self.hosting_domain.domain,
|
|
|
|
},
|
2015-01-26 22:47:49 +01:00
|
|
|
)
|
|
|
|
self.helper.layout = Layout(
|
2023-02-18 22:46:48 +01:00
|
|
|
AppendedText("subdomain", "." + self.hosting_domain.domain),
|
|
|
|
"wildcard",
|
|
|
|
Submit("submit", _("Add website")),
|
2015-01-26 22:47:49 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def clean_subdomain(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
data = self.cleaned_data["subdomain"]
|
|
|
|
if Website.objects.filter(domain=self.hosting_domain, subdomain=data).exists():
|
2015-01-26 22:47:49 +01:00
|
|
|
raise forms.ValidationError(
|
2023-02-18 22:46:48 +01:00
|
|
|
_("There is already a website for this subdomain")
|
|
|
|
)
|
|
|
|
relative_domain_validator("{0}.{1}".format(data, self.hosting_domain.domain))
|
2015-01-26 22:47:49 +01:00
|
|
|
return data
|
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
self.instance.domain = self.hosting_domain
|
|
|
|
self.instance.osuser = self.hosting_package.osuser
|
|
|
|
return super(AddWebsiteForm, self).save(commit)
|