From 24943ebe15afc8ebc398ba979feaa98a963c8f45 Mon Sep 17 00:00:00 2001 From: James Valleroy Date: Fri, 6 Jan 2017 19:03:53 -0500 Subject: [PATCH] bind: Use django's form validation --- CHANGELOG.md | 1 + plinth/modules/bind/__init__.py | 12 ------------ plinth/modules/bind/forms.py | 8 ++++++++ plinth/modules/bind/views.py | 16 ++++++---------- 4 files changed, 15 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 34f306cb1..697b93c7d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file. list those modules. - Added JS license web labels for LibreJS. - Added basic configuration form for Minetest server. +- Added Domain Name Server (BIND) module. ### Changed - frontpage: Show app logos instead of generic icons. diff --git a/plinth/modules/bind/__init__.py b/plinth/modules/bind/__init__.py index 10e20e917..0c3089f9a 100644 --- a/plinth/modules/bind/__init__.py +++ b/plinth/modules/bind/__init__.py @@ -22,8 +22,6 @@ Plinth module to configure BIND server import re from django.utils.translation import ugettext_lazy as _ -from django.core.exceptions import ValidationError -from django.core.validators import validate_ipv46_address from plinth import actions from plinth import action_utils @@ -141,13 +139,3 @@ def get_default(): 'forwarders': forwarders } return conf - - -def validate(ips): - """Validate that ips is a list of IP addresses, separated by space.""" - for ip_addr in ips.split(): - try: - validate_ipv46_address(ip_addr) - except ValidationError: - return False - return True diff --git a/plinth/modules/bind/forms.py b/plinth/modules/bind/forms.py index aa04d209e..c3fc27752 100644 --- a/plinth/modules/bind/forms.py +++ b/plinth/modules/bind/forms.py @@ -20,11 +20,18 @@ Forms for BIND module. """ from django import forms +from django.core.validators import validate_ipv46_address from django.utils.translation import ugettext_lazy as _ from plinth.forms import ServiceForm +def validate_ips(ips): + """Validate that ips is a list of IP addresses, separated by space.""" + for ip_addr in ips.split(): + validate_ipv46_address(ip_addr) + + class BindForm(ServiceForm): """BIND configuration form""" set_forwarding = forms.BooleanField( @@ -39,4 +46,5 @@ class BindForm(ServiceForm): forwarders = forms.CharField( required=False, + validators=[validate_ips], help_text=_('A list of IP addresses, separated by space')) diff --git a/plinth/modules/bind/views.py b/plinth/modules/bind/views.py index 52b4ff169..f7cdc90c6 100644 --- a/plinth/modules/bind/views.py +++ b/plinth/modules/bind/views.py @@ -26,7 +26,7 @@ from plinth import actions from plinth.views import ServiceView -from . import description, managed_services, get_default, validate +from . import description, managed_services, get_default from .forms import BindForm @@ -67,15 +67,11 @@ class BindServiceView(ServiceView): # pylint: disable=too-many-ancestors if old_config['forwarders'] != data['forwarders'] \ and old_config['forwarders'] is not '': - if validate(data['forwarders']) is True: - actions.superuser_run( - 'bind', - ['dns', '--set', data['forwarders']]) - messages.success(self.request, - _('DNS server configuration updated')) - else: - messages.error(self.request, - _('Enter a valid IPv4 or IPv6 address.')) + actions.superuser_run( + 'bind', + ['dns', '--set', data['forwarders']]) + messages.success(self.request, + _('DNS server configuration updated')) elif old_config['forwarders'] is '' \ and old_config['forwarders'] != data['forwarders']: messages.error(