django-orchestra/orchestra/contrib/domains/forms.py

100 lines
3.8 KiB
Python
Raw Normal View History

2014-05-08 16:59:35 +00:00
from django import forms
from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from orchestra.admin.forms import AdminFormSet
2014-05-08 16:59:35 +00:00
from . import validators
from .helpers import domain_for_validation
from .models import Domain
2015-03-11 20:01:08 +00:00
class BatchDomainCreationAdminForm(forms.ModelForm):
name = forms.CharField(label=_("Names"), widget=forms.Textarea(attrs={'rows': 5, 'cols': 50}),
help_text=_("Fully qualified domain name per line. "
"All domains will have the provided account and records."))
2015-03-11 20:01:08 +00:00
def clean_name(self):
self.extra_names = []
target = None
2015-05-26 12:59:16 +00:00
existing = set(Domain.objects.values_list('name', flat=True))
errors = []
2015-03-11 20:01:08 +00:00
for name in self.cleaned_data['name'].strip().splitlines():
name = name.strip()
if not name:
continue
2015-05-26 12:59:16 +00:00
if name in existing:
errors.append(ValidationError(_("%s domain name already exists.") % name))
existing.add(name)
2015-03-11 20:01:08 +00:00
if target is None:
target = name
else:
domain = Domain(name=name)
try:
domain.full_clean(exclude=['top'])
except ValidationError as e:
raise ValidationError(e.error_dict['name'])
self.extra_names.append(name)
2015-05-26 12:59:16 +00:00
if errors:
raise ValidationError(errors)
2015-03-11 20:01:08 +00:00
return target
2014-05-08 16:59:35 +00:00
def clean(self):
2015-03-29 16:10:07 +00:00
""" inherit related parent domain account, when exists """
2015-03-11 20:01:08 +00:00
cleaned_data = super(BatchDomainCreationAdminForm, self).clean()
2014-05-08 16:59:35 +00:00
if not cleaned_data['account']:
2015-03-11 20:01:08 +00:00
account = None
for name in [cleaned_data['name']] + self.extra_names:
2015-05-15 14:19:24 +00:00
parent = Domain.get_parent_domain(name)
2015-03-29 16:10:07 +00:00
if not parent:
2015-03-11 20:01:08 +00:00
# Fake an account to make django validation happy
account_model = self.fields['account']._queryset.model
cleaned_data['account'] = account_model()
raise ValidationError({
'account': _("An account should be provided for top domain names."),
})
2015-03-29 16:10:07 +00:00
elif account and parent.account != account:
2015-03-11 20:01:08 +00:00
# Fake an account to make django validation happy
account_model = self.fields['account']._queryset.model
cleaned_data['account'] = account_model()
raise ValidationError({
'account': _("Provided domain names belong to different accounts."),
})
2015-03-29 16:10:07 +00:00
account = parent.account
2015-03-11 20:01:08 +00:00
cleaned_data['account'] = account
2014-05-08 16:59:35 +00:00
return cleaned_data
class RecordForm(forms.ModelForm):
class Meta:
fields = ('ttl', 'type', 'value')
def __init__(self, *args, **kwargs):
super(RecordForm, self).__init__(*args, **kwargs)
self.fields['ttl'].widget = forms.TextInput(attrs={'size':'10'})
self.fields['value'].widget = forms.TextInput(attrs={'size':'100'})
class ValidateZoneMixin(object):
2014-05-08 16:59:35 +00:00
def clean(self):
""" Checks if everything is consistent """
super(ValidateZoneMixin, self).clean()
if any(formset.errors):
2014-05-08 16:59:35 +00:00
return
if formset.instance.name:
2014-05-08 16:59:35 +00:00
records = []
for form in formset.forms:
2014-05-08 16:59:35 +00:00
data = form.cleaned_data
if data and not data['DELETE']:
records.append(data)
domain = domain_for_validation(formset.instance, records)
2014-05-08 16:59:35 +00:00
validators.validate_zone(domain.render_zone())
class RecordEditFormSet(ValidateZoneMixin, AdminFormSet):
pass
class RecordInlineFormSet(ValidateZoneMixin, forms.models.BaseInlineFormSet):
pass