2015-05-18 15:21:42 +00:00
|
|
|
from collections import defaultdict
|
|
|
|
|
2015-02-25 17:29:39 +00:00
|
|
|
from django import forms
|
|
|
|
from django.core.exceptions import ValidationError
|
2015-03-10 21:51:10 +00:00
|
|
|
|
2023-08-08 14:20:46 +00:00
|
|
|
from orchestra.contrib.webapps.models import WebApp
|
|
|
|
|
2015-05-19 13:27:04 +00:00
|
|
|
from .utils import normurlpath
|
2015-07-07 10:41:34 +00:00
|
|
|
from .validators import validate_domain_protocol, validate_server_name
|
2015-02-25 17:29:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class WebsiteAdminForm(forms.ModelForm):
|
|
|
|
def clean(self):
|
2015-03-10 21:51:10 +00:00
|
|
|
""" Prevent multiples domains on the same protocol """
|
2015-04-09 14:32:10 +00:00
|
|
|
super(WebsiteAdminForm, self).clean()
|
2015-02-25 17:29:39 +00:00
|
|
|
domains = self.cleaned_data.get('domains')
|
2015-03-10 21:51:10 +00:00
|
|
|
if not domains:
|
|
|
|
return self.cleaned_data
|
|
|
|
protocol = self.cleaned_data.get('protocol')
|
2015-07-07 10:41:34 +00:00
|
|
|
domains = domains.all()
|
|
|
|
for domain in domains:
|
2015-03-10 22:27:32 +00:00
|
|
|
try:
|
|
|
|
validate_domain_protocol(self.instance, domain, protocol)
|
2015-07-07 10:41:34 +00:00
|
|
|
except ValidationError as err:
|
|
|
|
self.add_error(None, err)
|
|
|
|
try:
|
|
|
|
validate_server_name(domains)
|
|
|
|
except ValidationError as err:
|
|
|
|
self.add_error('domains', err)
|
2015-02-25 17:29:39 +00:00
|
|
|
return self.cleaned_data
|
2023-08-08 14:20:46 +00:00
|
|
|
|
|
|
|
def clean_target_server(self):
|
|
|
|
# valida que el webapp pertenezca al server indicado
|
|
|
|
try:
|
|
|
|
server = self.cleaned_data['target_server']
|
|
|
|
except:
|
|
|
|
server = self.instance.target_server
|
|
|
|
|
|
|
|
diferentServer = False
|
|
|
|
for i in range(int(self.data['content_set-TOTAL_FORMS']) + 1):
|
|
|
|
if f"content_set-{i}-webapp" in self.data.keys() and f"content_set-{i}-DELETE" not in self.data.keys():
|
|
|
|
if self.data[f"content_set-{i}-webapp"]:
|
|
|
|
idWebapp = self.data[f"content_set-{i}-webapp"]
|
|
|
|
webapp = WebApp.objects.get(id=idWebapp)
|
|
|
|
if webapp.target_server.id != server.id :
|
|
|
|
diferentServer = True
|
|
|
|
if diferentServer:
|
|
|
|
self.add_error("target_server", f"Some Webapp does not belong to the {server.name} server")
|
|
|
|
return server
|
2015-02-25 17:29:39 +00:00
|
|
|
|
2015-03-25 15:45:04 +00:00
|
|
|
|
|
|
|
class WebsiteDirectiveInlineFormSet(forms.models.BaseInlineFormSet):
|
|
|
|
def clean(self):
|
2015-05-18 15:21:42 +00:00
|
|
|
# directives formset cross-validation with contents for unique locations
|
|
|
|
locations = set()
|
|
|
|
for form in self.content_formset.forms:
|
|
|
|
location = form.cleaned_data.get('path')
|
2016-07-21 11:28:43 +00:00
|
|
|
delete = form.cleaned_data.get('DELETE')
|
|
|
|
if not delete and location is not None:
|
2015-05-19 13:27:04 +00:00
|
|
|
locations.add(normurlpath(location))
|
2015-05-18 15:21:42 +00:00
|
|
|
|
|
|
|
values = defaultdict(list)
|
2015-03-25 15:45:04 +00:00
|
|
|
for form in self.forms:
|
2015-10-01 16:02:26 +00:00
|
|
|
wdirective = form.instance
|
2015-05-18 15:21:42 +00:00
|
|
|
directive = form.cleaned_data
|
|
|
|
if directive.get('name') is not None:
|
2015-03-25 15:45:04 +00:00
|
|
|
try:
|
2015-10-01 16:02:26 +00:00
|
|
|
wdirective.directive_instance.validate_uniqueness(directive, values, locations)
|
2015-05-18 15:21:42 +00:00
|
|
|
except ValidationError as err:
|
|
|
|
for k,v in err.error_dict.items():
|
|
|
|
form.add_error(k, v)
|