diff --git a/plinth/modules/dynamicdns/forms.py b/plinth/modules/dynamicdns/forms.py index a7165661b..a870ed28a 100644 --- a/plinth/modules/dynamicdns/forms.py +++ b/plinth/modules/dynamicdns/forms.py @@ -5,10 +5,12 @@ Forms for the dynamicsdns module. from django import forms from django.core import validators +from django.core.exceptions import ValidationError from django.utils.translation import gettext as _ from django.utils.translation import gettext_lazy from plinth import cfg +from plinth.modules.dynamicdns import get_config from plinth.utils import format_lazy @@ -94,10 +96,6 @@ class DomainForm(forms.Form): """Further validate and transform field data.""" cleaned_data = super().clean() - # Domain name is not case sensitive, but Let's Encrypt - # certificate paths use lower-case domain name. - cleaned_data['domain'] = cleaned_data['domain'].lower() - update_url = cleaned_data.get('update_url') password = cleaned_data.get('password') service_type = cleaned_data.get('service_type') @@ -129,3 +127,14 @@ class DomainForm(forms.Form): del cleaned_data['show_password'] return cleaned_data + + def clean_domain(self): + """Validate the domain field.""" + # Domain name is not case sensitive, but Let's Encrypt + # certificate paths use lower-case domain name. + domain = self.cleaned_data['domain'].lower() + initial_domain = self.initial.get('domain') + if initial_domain != domain and domain in get_config()['domains']: + raise ValidationError(_('Domain already exists.')) + + return domain