1 # -*- coding: utf-8 -*-
2 # Copyright (C) 2012-2021 by the Free Software Foundation, Inc.
4 # This file is part of Postorius.
6 # Postorius is free software: you can redistribute it and/or modify it under
7 # the terms of the GNU General Public License as published by the Free
8 # Software Foundation, either version 3 of the License, or (at your option)
11 # Postorius is distributed in the hope that it will be useful, but WITHOUT
12 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 # FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
16 # You should have received a copy of the GNU General Public License along with
17 # Postorius. If not, see <http://www.gnu.org/licenses/>.
20 from django
import forms
21 from django
.contrib
.sites
.models
import Site
22 from django
.core
.exceptions
import ValidationError
23 from django
.core
.validators
import validate_email
24 from django
.urls
import reverse
25 from django
.utils
.translation
import gettext_lazy
as _
27 from postorius
.forms
.fields
import SiteModelChoiceField
30 def _get_web_host_help():
31 # Using a function is necessary, otherwise reverse() will be called before
32 # URLConfs are loaded.
34 'The domain from which you want the web UI to be served from. '
35 'This can be same or different from the Mail Host. '
36 'You can edit the list of available web hosts <a href="%s">here</a>.'
37 ) % reverse("admin:sites_site_changelist"))
40 class DomainForm(forms
.Form
):
44 mail_host
= forms
.CharField(
46 error_messages
={'required': _('Please enter a domain name'),
47 'invalid': _('Please enter a valid domain name.')},
50 'The domain for your mailing lists. For example when you want '
51 'lists like testing@example.com, enter example.com here.'),
53 description
= forms
.CharField(
54 label
=_('Description'),
56 alias_domain
= forms
.CharField(
57 label
=_('Alias Domain'),
59 'invalid': _('Please enter a valid domain name or nothing.')},
61 help_text
=_('Normally empty. Used only for unusual Postfix configs.'),
63 site
= SiteModelChoiceField(
65 error_messages
={'required': _('Please enter a domain name'),
66 'invalid': _('Please enter a valid domain name.')},
68 queryset
=Site
.objects
.order_by("name").all(),
69 initial
=lambda: Site
.objects
.get_current(),
70 help_text
=_get_web_host_help
,
73 def clean_mail_host(self
):
74 mail_host
= self
.cleaned_data
['mail_host']
76 validate_email('mail@' + mail_host
)
77 except ValidationError
:
78 raise forms
.ValidationError(_("Please enter a valid domain name"))
81 def clean_alias_domain(self
):
82 alias_domain
= self
.cleaned_data
['alias_domain']
83 if alias_domain
!= '':
85 validate_email('mail@' + alias_domain
)
86 except ValidationError
:
87 raise forms
.ValidationError(
88 _("Please enter a valid domain name or nothing."))
92 class DomainEditForm(DomainForm
):
95 separte from the DomainForm, so that the mail_host can't be changed.
100 class DomainOwnerForm(forms
.Form
):
101 """Form to add a owner for a domain."""
102 email
= forms
.EmailField(
103 label
=_("Owner's Email"),