2015-01-25 12:10:17 +01:00
|
|
|
"""
|
|
|
|
This module defines form classes for mailbox and mail address editing.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
from __future__ import absolute_import
|
2015-01-25 12:10:17 +01:00
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from crispy_forms.bootstrap import AppendedText
|
|
|
|
from crispy_forms.helper import FormHelper
|
|
|
|
from crispy_forms.layout import Div, Layout, Submit
|
2015-01-25 12:10:17 +01:00
|
|
|
from django import forms
|
2015-01-25 18:20:51 +01:00
|
|
|
from django.core.validators import validate_email
|
2018-11-19 23:28:40 +01:00
|
|
|
from django.urls import reverse
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
from model_utils import Choices
|
2015-01-25 18:20:51 +01:00
|
|
|
|
2015-01-25 12:10:17 +01:00
|
|
|
from gvawebcore.forms import PasswordModelFormMixin
|
|
|
|
|
2023-02-18 22:46:48 +01:00
|
|
|
from .models import MailAddress, Mailbox
|
2015-01-25 18:20:51 +01:00
|
|
|
|
|
|
|
MAILBOX_OR_FORWARDS = Choices(
|
2023-02-18 22:46:48 +01:00
|
|
|
(0, "mailbox", _("Mailbox")),
|
|
|
|
(1, "forwards", _("Forwards")),
|
2015-01-25 18:20:51 +01:00
|
|
|
)
|
|
|
|
|
2015-01-25 12:10:17 +01:00
|
|
|
|
|
|
|
class CreateMailboxForm(PasswordModelFormMixin, forms.ModelForm):
|
|
|
|
"""
|
|
|
|
This form is used to create new Mailbox instances.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-25 12:10:17 +01:00
|
|
|
class Meta:
|
|
|
|
model = Mailbox
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.hosting_package = kwargs.pop("hostingpackage")
|
2015-01-25 12:10:17 +01:00
|
|
|
super(CreateMailboxForm, self).__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_action = reverse(
|
2023-02-18 22:46:48 +01:00
|
|
|
"create_mailbox", kwargs={"package": self.hosting_package.id}
|
|
|
|
)
|
|
|
|
self.helper.add_input(Submit("submit", _("Create mailbox")))
|
2015-01-25 12:10:17 +01:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Set the new mailbox's password and osuser.
|
|
|
|
|
|
|
|
:param boolean commit: whether to save the created mailbox
|
|
|
|
:return: mailbox instance
|
|
|
|
:rtype: :py:class:`managemails.models.Mailbox`
|
|
|
|
|
|
|
|
"""
|
|
|
|
osuser = self.hosting_package.osuser
|
|
|
|
self.instance.osuser = osuser
|
|
|
|
self.instance.username = Mailbox.objects.get_next_mailbox_name(osuser)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.instance.set_password(self.cleaned_data["password1"])
|
2015-01-25 12:10:17 +01:00
|
|
|
return super(CreateMailboxForm, self).save(commit=commit)
|
2015-01-25 12:49:31 +01:00
|
|
|
|
|
|
|
|
|
|
|
class ChangeMailboxPasswordForm(PasswordModelFormMixin, forms.ModelForm):
|
|
|
|
"""
|
|
|
|
This form is used to set a new password for an existing mailbox.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-25 12:49:31 +01:00
|
|
|
class Meta:
|
|
|
|
model = Mailbox
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.hosting_package = kwargs.pop("hostingpackage")
|
2015-01-25 12:49:31 +01:00
|
|
|
super(ChangeMailboxPasswordForm, self).__init__(*args, **kwargs)
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_action = reverse(
|
2023-02-18 22:46:48 +01:00
|
|
|
"change_mailbox_password",
|
|
|
|
kwargs={
|
|
|
|
"package": self.hosting_package.id,
|
|
|
|
"slug": self.instance.username,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
self.helper.add_input(Submit("submit", _("Set password")))
|
2015-01-25 12:49:31 +01:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Set the mailbox password.
|
|
|
|
|
|
|
|
:param boolean commit: whether to save the mailbox instance
|
|
|
|
:return: mailbox instance
|
|
|
|
:rtype: :py:class:`managemails.models.Mailbox`
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
self.instance.set_password(self.cleaned_data["password1"])
|
2015-01-25 12:49:31 +01:00
|
|
|
return super(ChangeMailboxPasswordForm, self).save(commit=commit)
|
2015-01-25 18:20:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
def multiple_email_validator(value):
|
|
|
|
if value:
|
2023-02-18 22:46:48 +01:00
|
|
|
for email in [part.strip() for part in value.split(",")]:
|
2015-01-25 18:20:51 +01:00
|
|
|
validate_email(email)
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
2015-01-25 22:07:54 +01:00
|
|
|
class MailAddressFieldMixin(forms.Form):
|
2015-01-25 18:20:51 +01:00
|
|
|
"""
|
2015-01-25 22:07:54 +01:00
|
|
|
This mixin defines form fields common to mail address forms.
|
2015-01-25 18:20:51 +01:00
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-25 18:20:51 +01:00
|
|
|
mailbox_or_forwards = forms.TypedChoiceField(
|
2023-02-18 22:46:48 +01:00
|
|
|
label=_("Mailbox or Forwards"),
|
2015-01-25 18:20:51 +01:00
|
|
|
choices=MAILBOX_OR_FORWARDS,
|
|
|
|
widget=forms.RadioSelect,
|
|
|
|
coerce=int,
|
|
|
|
)
|
|
|
|
mailbox = forms.ModelChoiceField(
|
|
|
|
Mailbox.objects,
|
2023-02-18 22:46:48 +01:00
|
|
|
label=_("Mailbox"),
|
2015-01-25 18:20:51 +01:00
|
|
|
required=False,
|
|
|
|
)
|
2015-11-22 19:41:45 +01:00
|
|
|
# TODO: refactor as separate field class returning a list
|
2015-01-25 18:20:51 +01:00
|
|
|
forwards = forms.CharField(
|
2023-02-18 22:46:48 +01:00
|
|
|
label=_("Forwards"),
|
2015-01-25 18:20:51 +01:00
|
|
|
required=False,
|
|
|
|
error_messages={
|
2023-02-18 22:46:48 +01:00
|
|
|
"invalid": _(
|
|
|
|
"Please enter one or more email addresses separated by " "commas."
|
|
|
|
),
|
2015-01-25 18:20:51 +01:00
|
|
|
},
|
|
|
|
validators=[multiple_email_validator],
|
|
|
|
)
|
|
|
|
|
2015-01-25 22:07:54 +01:00
|
|
|
|
|
|
|
class AddMailAddressForm(forms.ModelForm, MailAddressFieldMixin):
|
|
|
|
"""
|
|
|
|
This form is used to add a new mail address.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-25 18:20:51 +01:00
|
|
|
class Meta:
|
|
|
|
model = MailAddress
|
2023-02-18 22:46:48 +01:00
|
|
|
fields = ["localpart"]
|
2015-01-25 18:20:51 +01:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.maildomain = kwargs.pop("maildomain")
|
|
|
|
self.hosting_package = kwargs.pop("hostingpackage")
|
2015-01-25 18:20:51 +01:00
|
|
|
super(AddMailAddressForm, self).__init__(*args, **kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.fields["mailbox"].queryset = Mailbox.objects.unused(
|
2015-01-25 22:07:54 +01:00
|
|
|
osuser=self.hosting_package.osuser,
|
2015-01-25 18:20:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_action = reverse(
|
2023-02-18 22:46:48 +01:00
|
|
|
"add_mailaddress",
|
|
|
|
kwargs={
|
|
|
|
"package": self.hosting_package.id,
|
|
|
|
"domain": self.maildomain.domain,
|
|
|
|
},
|
|
|
|
)
|
2015-01-25 18:20:51 +01:00
|
|
|
self.helper.layout = Layout(
|
|
|
|
Div(
|
|
|
|
Div(
|
2023-02-18 22:46:48 +01:00
|
|
|
AppendedText("localpart", "@" + self.maildomain.domain),
|
|
|
|
css_class="col-lg-4 col-md-4 col-xs-12",
|
2015-01-25 18:20:51 +01:00
|
|
|
),
|
|
|
|
Div(
|
2023-02-18 22:46:48 +01:00
|
|
|
"mailbox_or_forwards",
|
|
|
|
css_class="col-lg-2 col-md-2 col-xs-12",
|
2015-01-25 18:20:51 +01:00
|
|
|
),
|
|
|
|
Div(
|
2023-02-18 22:46:48 +01:00
|
|
|
"mailbox",
|
|
|
|
"forwards",
|
|
|
|
css_class="col-lg-6 col-md-6 col-xs-12",
|
2015-01-25 18:20:51 +01:00
|
|
|
),
|
2023-02-18 22:46:48 +01:00
|
|
|
css_class="row",
|
2015-01-25 18:20:51 +01:00
|
|
|
),
|
2023-02-18 22:46:48 +01:00
|
|
|
Submit("submit", _("Add mail address")),
|
2015-01-25 18:20:51 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def clean_localpart(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
localpart = self.cleaned_data["localpart"]
|
2015-01-25 18:20:51 +01:00
|
|
|
if MailAddress.objects.filter(
|
|
|
|
domain=self.maildomain,
|
|
|
|
localpart=localpart,
|
|
|
|
).exists():
|
2023-02-18 22:46:48 +01:00
|
|
|
raise forms.ValidationError(_("This mail address is already in use."))
|
|
|
|
validate_email("{0}@{1}".format(localpart, self.maildomain.domain))
|
2015-01-25 18:20:51 +01:00
|
|
|
return localpart
|
|
|
|
|
|
|
|
def clean(self):
|
2015-11-22 19:41:45 +01:00
|
|
|
super(AddMailAddressForm, self).clean()
|
2015-01-25 18:20:51 +01:00
|
|
|
data = self.cleaned_data
|
2023-02-18 22:46:48 +01:00
|
|
|
if data["mailbox_or_forwards"] == MAILBOX_OR_FORWARDS.mailbox:
|
|
|
|
if "mailbox" not in data or not data["mailbox"]:
|
|
|
|
self.add_error("mailbox", _("No mailbox selected"))
|
|
|
|
elif data["mailbox_or_forwards"] == MAILBOX_OR_FORWARDS.forwards:
|
|
|
|
if "forwards" not in data or not data["forwards"]:
|
|
|
|
self.add_error("forwards", _("No forward addresses selected"))
|
2015-11-22 19:41:45 +01:00
|
|
|
else: # pragma: no cover
|
|
|
|
# should not happen because of the field's validation
|
2015-01-25 18:20:51 +01:00
|
|
|
raise forms.ValidationError(
|
2023-02-18 22:46:48 +01:00
|
|
|
_("Illegal choice for target of the mail address")
|
|
|
|
)
|
2015-01-25 18:20:51 +01:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Save the mail address.
|
|
|
|
|
|
|
|
:param boolean commit: whether to save the mail address instance
|
|
|
|
:return: mail address instance
|
|
|
|
:rtype: :py:class:`managemails.models.MailAddress`
|
|
|
|
|
|
|
|
"""
|
|
|
|
self.instance.domain = self.maildomain
|
2015-01-25 22:07:54 +01:00
|
|
|
data = self.cleaned_data
|
2023-02-18 22:46:48 +01:00
|
|
|
target_choice = data["mailbox_or_forwards"]
|
2015-01-25 18:20:51 +01:00
|
|
|
if target_choice == MAILBOX_OR_FORWARDS.mailbox:
|
2023-02-18 22:46:48 +01:00
|
|
|
mabox = self.instance.set_mailbox(data["mailbox"], commit=False)
|
2015-01-25 18:20:51 +01:00
|
|
|
elif target_choice == MAILBOX_OR_FORWARDS.forwards:
|
2023-02-18 22:46:48 +01:00
|
|
|
targets = [part.strip() for part in data["forwards"].split(",")]
|
2015-01-25 22:07:54 +01:00
|
|
|
fwds = self.instance.set_forward_addresses(targets, commit=False)
|
|
|
|
mailaddress = super(AddMailAddressForm, self).save(commit)
|
|
|
|
if commit:
|
|
|
|
if target_choice == MAILBOX_OR_FORWARDS.mailbox:
|
|
|
|
mabox.mailaddress = mailaddress
|
|
|
|
mabox.save()
|
|
|
|
elif target_choice == MAILBOX_OR_FORWARDS.forwards:
|
|
|
|
for fwd in fwds:
|
|
|
|
fwd.mailaddress = mailaddress
|
|
|
|
fwd.save()
|
2015-01-25 18:20:51 +01:00
|
|
|
return mailaddress
|
2015-01-25 22:07:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EditMailAddressForm(forms.ModelForm, MailAddressFieldMixin):
|
|
|
|
"""
|
|
|
|
This form is used to edit the targets for a mail address.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-25 22:07:54 +01:00
|
|
|
class Meta:
|
|
|
|
model = MailAddress
|
|
|
|
fields = []
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
2023-02-18 22:46:48 +01:00
|
|
|
self.hosting_package = kwargs.pop("hostingpackage")
|
|
|
|
self.maildomain = kwargs.pop("maildomain")
|
2015-01-25 22:07:54 +01:00
|
|
|
super(EditMailAddressForm, self).__init__(*args, **kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
self.fields["mailbox"].queryset = Mailbox.objects.unused_or_own(
|
2015-01-25 22:07:54 +01:00
|
|
|
self.instance, self.hosting_package.osuser
|
|
|
|
)
|
|
|
|
|
|
|
|
self.helper = FormHelper()
|
|
|
|
self.helper.form_action = reverse(
|
2023-02-18 22:46:48 +01:00
|
|
|
"edit_mailaddress",
|
|
|
|
kwargs={
|
|
|
|
"package": self.hosting_package.id,
|
|
|
|
"domain": self.maildomain.domain,
|
|
|
|
"pk": self.instance.id,
|
|
|
|
},
|
|
|
|
)
|
2015-01-25 22:07:54 +01:00
|
|
|
self.helper.layout = Layout(
|
|
|
|
Div(
|
|
|
|
Div(
|
2023-02-18 22:46:48 +01:00
|
|
|
"mailbox_or_forwards",
|
|
|
|
css_class="col-log-2 col-md-2 col-xs-12",
|
2015-01-25 22:07:54 +01:00
|
|
|
),
|
|
|
|
Div(
|
2023-02-18 22:46:48 +01:00
|
|
|
"mailbox",
|
|
|
|
"forwards",
|
|
|
|
css_class="col-lg-10 col-md-10 col-xs-12",
|
2015-01-25 22:07:54 +01:00
|
|
|
),
|
2023-02-18 22:46:48 +01:00
|
|
|
css_class="row",
|
2015-01-25 22:07:54 +01:00
|
|
|
),
|
2023-02-18 22:46:48 +01:00
|
|
|
Submit("submit", _("Change mail address targets")),
|
2015-01-25 22:07:54 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def clean(self):
|
|
|
|
data = self.cleaned_data
|
2023-02-18 22:46:48 +01:00
|
|
|
if data["mailbox_or_forwards"] == MAILBOX_OR_FORWARDS.mailbox:
|
|
|
|
if not data["mailbox"]:
|
|
|
|
self.add_error("mailbox", _("No mailbox selected"))
|
|
|
|
elif data["mailbox_or_forwards"] == MAILBOX_OR_FORWARDS.forwards:
|
|
|
|
if "forwards" not in data or not data["forwards"]:
|
|
|
|
self.add_error("forwards", _("No forward addresses selected"))
|
2015-11-22 19:41:45 +01:00
|
|
|
else: # pragma: no cover
|
|
|
|
# should not happen because of the field's validation
|
2015-01-25 22:07:54 +01:00
|
|
|
raise forms.ValidationError(
|
2023-02-18 22:46:48 +01:00
|
|
|
_("Illegal choice for target of the mail address")
|
|
|
|
)
|
2015-01-25 22:07:54 +01:00
|
|
|
|
|
|
|
def save(self, commit=True):
|
|
|
|
"""
|
|
|
|
Save the mail address.
|
|
|
|
|
|
|
|
:param boolean commit:
|
|
|
|
"""
|
|
|
|
data = self.cleaned_data
|
2023-02-18 22:46:48 +01:00
|
|
|
if data["mailbox_or_forwards"] == MAILBOX_OR_FORWARDS.mailbox:
|
|
|
|
self.instance.set_mailbox(data["mailbox"], commit)
|
|
|
|
elif data["mailbox_or_forwards"] == MAILBOX_OR_FORWARDS.forwards:
|
|
|
|
targets = [part.strip() for part in data["forwards"].split(",")]
|
2015-02-06 11:17:03 +01:00
|
|
|
self.instance.set_forward_addresses(targets, commit)
|
2015-11-22 19:41:45 +01:00
|
|
|
return super(EditMailAddressForm, self).save(commit)
|