Manage DNS Settings

This commit is contained in:
Eduardo Silva 2024-04-26 16:16:02 -03:00
parent 13702c664e
commit 414bc8076a
3 changed files with 79 additions and 3 deletions

View file

@ -11,6 +11,45 @@ from django.core.exceptions import ValidationError
import re
class DNSSettingsForm(forms.ModelForm):
class Meta:
model = DNSSettings
fields = ['dns_primary', 'dns_secondary']
def __init__(self, *args, **kwargs):
super(DNSSettingsForm, self).__init__(*args, **kwargs)
self.helper = FormHelper()
self.fields['dns_primary'].label = 'Primary Resolver'
self.fields['dns_secondary'].label = 'Secondary Resolver'
self.helper.form_method = 'post'
self.helper.layout = Layout(
Fieldset(
'Resolver Settings',
Div(
Field('dns_primary', css_class='form-control'),
Field('dns_secondary', css_class='form-control'),
css_class='col-md-6'
),
),
FormActions(
Submit('save', 'Save', css_class='btn btn-primary'),
HTML('<a class="btn btn-outline-primary" href="/dns/">Back</a>'),
)
)
def clean(self):
cleaned_data = super().clean()
dns_primary = cleaned_data.get('dns_primary')
dns_secondary = cleaned_data.get('dns_secondary')
if dns_secondary and not dns_primary:
dns_primary = dns_secondary
dns_secondary = ''
cleaned_data['dns_primary'] = dns_primary
cleaned_data['dns_secondary'] = dns_secondary
if dns_primary and dns_primary == dns_secondary:
raise ValidationError('Primary and secondary DNS cannot be the same')
return
class StaticHostForm(forms.ModelForm):
class Meta:
@ -42,7 +81,8 @@ class StaticHostForm(forms.ModelForm):
)
def clean(self):
hostname = self.cleaned_data.get('hostname')
cleaned_data = super().clean()
hostname = cleaned_data.get('hostname')
if hostname:
regex = r'^[a-zA-Z][a-zA-Z0-9-\.]*[a-zA-Z0-9]$'
if not re.match(regex, hostname):