2013-09-28 20:01:09 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2013-12-15 17:09:22 +01:00
|
|
|
"""
|
|
|
|
form definitions (which fields are available, order, autofocus, ...)
|
|
|
|
"""
|
|
|
|
|
2015-12-29 13:12:22 +01:00
|
|
|
import binascii
|
|
|
|
|
2013-09-28 20:01:09 +02:00
|
|
|
from django import forms
|
2015-12-29 13:30:58 +01:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2013-10-03 19:26:39 +02:00
|
|
|
|
2014-09-25 20:15:18 +02:00
|
|
|
from .models import Host, RelatedHost, Domain, ServiceUpdaterHostConfig
|
2015-12-29 13:30:58 +01:00
|
|
|
from .dnstools import check_domain, NameServerNotAvailable
|
2013-09-28 20:01:09 +02:00
|
|
|
|
2013-09-29 01:21:44 +02:00
|
|
|
|
2013-09-29 19:58:08 +02:00
|
|
|
class CreateHostForm(forms.ModelForm):
|
2013-10-03 17:21:18 +02:00
|
|
|
class Meta(object):
|
2013-09-28 20:01:09 +02:00
|
|
|
model = Host
|
2014-09-21 22:31:26 +02:00
|
|
|
fields = ['name', 'domain', 'comment']
|
2013-11-01 21:11:57 +01:00
|
|
|
widgets = {
|
2014-09-21 22:31:26 +02:00
|
|
|
'name': forms.widgets.TextInput(attrs=dict(autofocus=None)),
|
2013-11-01 21:11:57 +01:00
|
|
|
}
|
2013-09-29 19:58:08 +02:00
|
|
|
|
|
|
|
|
|
|
|
class EditHostForm(forms.ModelForm):
|
2013-10-03 17:21:18 +02:00
|
|
|
class Meta(object):
|
2013-09-29 19:58:08 +02:00
|
|
|
model = Host
|
2014-09-25 23:40:52 +02:00
|
|
|
fields = ['comment', 'available', 'abuse', 'netmask_ipv4', 'netmask_ipv6']
|
|
|
|
|
|
|
|
netmask_ipv4 = forms.IntegerField(min_value=0, max_value=32)
|
|
|
|
netmask_ipv6 = forms.IntegerField(min_value=0, max_value=64)
|
2013-10-18 15:30:17 -07:00
|
|
|
|
|
|
|
|
2014-09-25 20:15:18 +02:00
|
|
|
class CreateRelatedHostForm(forms.ModelForm):
|
|
|
|
class Meta(object):
|
|
|
|
model = RelatedHost
|
2014-09-25 22:05:14 +02:00
|
|
|
fields = ['name', 'comment', 'available', 'interface_id_ipv4', 'interface_id_ipv6']
|
2014-09-25 20:15:18 +02:00
|
|
|
widgets = {
|
|
|
|
'name': forms.widgets.TextInput(attrs=dict(autofocus=None)),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class EditRelatedHostForm(forms.ModelForm):
|
|
|
|
class Meta(object):
|
|
|
|
model = RelatedHost
|
2014-09-25 22:05:14 +02:00
|
|
|
fields = ['name', 'comment', 'available', 'interface_id_ipv4', 'interface_id_ipv6']
|
2014-09-25 20:15:18 +02:00
|
|
|
|
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
class CreateDomainForm(forms.ModelForm):
|
2015-12-29 13:12:22 +01:00
|
|
|
def clean_nameserver_update_secret(self):
|
|
|
|
secret = self.cleaned_data['nameserver_update_secret']
|
|
|
|
try:
|
|
|
|
binascii.a2b_base64(secret.encode(encoding="ascii", errors="strict"))
|
|
|
|
except (binascii.Error, UnicodeEncodeError):
|
|
|
|
raise forms.ValidationError(_("Enter a valid secret in base64 format."), code='invalid')
|
|
|
|
return secret
|
|
|
|
|
2013-10-18 15:30:17 -07:00
|
|
|
class Meta(object):
|
|
|
|
model = Domain
|
2015-12-29 13:30:58 +01:00
|
|
|
fields = ['name', 'nameserver_ip', 'nameserver2_ip', 'nameserver_update_algorithm', 'comment']
|
2013-11-01 21:11:57 +01:00
|
|
|
widgets = {
|
2014-09-21 22:31:26 +02:00
|
|
|
'name': forms.widgets.TextInput(attrs=dict(autofocus=None)),
|
2013-11-01 21:11:57 +01:00
|
|
|
}
|
2013-11-02 11:29:06 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EditDomainForm(forms.ModelForm):
|
2018-09-29 00:10:01 +02:00
|
|
|
def clean_nameserver_update_secret(self):
|
|
|
|
secret = self.cleaned_data['nameserver_update_secret']
|
|
|
|
try:
|
|
|
|
binascii.a2b_base64(secret.encode(encoding="ascii", errors="strict"))
|
|
|
|
except (binascii.Error, UnicodeEncodeError):
|
|
|
|
raise forms.ValidationError(_("Enter a valid secret in base64 format."), code='invalid')
|
|
|
|
return secret
|
|
|
|
|
2015-12-29 13:30:58 +01:00
|
|
|
def clean(self):
|
|
|
|
cleaned_data = super(EditDomainForm, self).clean()
|
|
|
|
|
|
|
|
if self.cleaned_data['available']:
|
|
|
|
try:
|
|
|
|
check_domain(self.instance.name)
|
|
|
|
|
|
|
|
except (NameServerNotAvailable, ):
|
|
|
|
raise forms.ValidationError(
|
2016-08-17 13:16:14 +02:00
|
|
|
_("Failed to add/delete host connectivity-test.%(domain)s, check your DNS server configuration. "
|
2015-12-29 13:30:58 +01:00
|
|
|
"This is a requirement for setting the available flag."),
|
|
|
|
code='invalid',
|
|
|
|
params={'domain': self.instance.name}
|
|
|
|
)
|
|
|
|
|
|
|
|
if cleaned_data['public'] and not cleaned_data['available']:
|
|
|
|
raise forms.ValidationError(
|
|
|
|
_("Domain must be available to be public"),
|
|
|
|
code='invalid')
|
|
|
|
|
2013-11-02 11:29:06 +01:00
|
|
|
class Meta(object):
|
|
|
|
model = Domain
|
2014-11-15 22:42:13 +01:00
|
|
|
fields = ['comment', 'nameserver_ip', 'nameserver2_ip', 'public', 'available',
|
2013-11-24 05:04:07 +01:00
|
|
|
'nameserver_update_algorithm', 'nameserver_update_secret']
|
2013-11-29 10:43:18 +01:00
|
|
|
|
|
|
|
|
|
|
|
class CreateUpdaterHostConfigForm(forms.ModelForm):
|
|
|
|
class Meta(object):
|
|
|
|
model = ServiceUpdaterHostConfig
|
|
|
|
fields = ['service', 'hostname', 'name', 'password',
|
|
|
|
'give_ipv4', 'give_ipv6', 'comment']
|
|
|
|
widgets = {
|
|
|
|
'hostname': forms.widgets.TextInput(attrs=dict(autofocus=None)),
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class EditUpdaterHostConfigForm(forms.ModelForm):
|
|
|
|
class Meta(object):
|
|
|
|
model = ServiceUpdaterHostConfig
|
|
|
|
fields = ['hostname', 'comment', 'name', 'password',
|
|
|
|
'give_ipv4', 'give_ipv6']
|