django-orchestra-test/orchestra/contrib/websites/forms.py

49 lines
1.8 KiB
Python
Raw Normal View History

2015-05-18 15:21:42 +00:00
from collections import defaultdict
from django import forms
from django.core.exceptions import ValidationError
2015-04-02 16:14:55 +00:00
from django.utils.encoding import force_text
2015-04-04 17:44:07 +00:00
from django.utils.translation import ugettext_lazy as _
2015-03-10 21:51:10 +00:00
2015-05-18 15:21:42 +00:00
from .directives import SiteDirective
2015-03-10 22:27:32 +00:00
from .validators import validate_domain_protocol
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()
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')
for domain in domains.all():
2015-03-10 22:27:32 +00:00
try:
validate_domain_protocol(self.instance, domain, protocol)
except ValidationError as e:
# TODO not sure about this one
self.add_error(None, e)
return self.cleaned_data
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')
if location is not None:
locations.add(location)
directives = []
values = defaultdict(list)
for form in self.forms:
2015-05-18 15:21:42 +00:00
website = form.instance
directive = form.cleaned_data
if directive.get('name') is not None:
try:
2015-05-18 15:21:42 +00:00
website.directive_instance.validate_uniqueness(directive, values, locations)
except ValidationError as err:
for k,v in err.error_dict.items():
form.add_error(k, v)