Jan Dittberner
1d69bb22dc
- implement managemails.forms.multiple_email_validator - implement managemails.forms.AddMailAddressForm - implement managemails.views.AddMailAddress - add URL pattern 'add_mailaddress' to managemails.urls - add template managemails/mailaddress_create.html - add changelog entry
225 lines
7.1 KiB
Python
225 lines
7.1 KiB
Python
"""
|
|
This module defines form classes for mailbox and mail address editing.
|
|
|
|
"""
|
|
from __future__ import absolute_import, unicode_literals
|
|
|
|
from django import forms
|
|
from django.core.urlresolvers import reverse
|
|
from django.core.validators import validate_email
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from crispy_forms.helper import FormHelper
|
|
from crispy_forms.bootstrap import AppendedText
|
|
from crispy_forms.layout import (
|
|
Div,
|
|
Layout,
|
|
Submit,
|
|
)
|
|
|
|
from .models import (
|
|
MailAddress,
|
|
MailAddressForward,
|
|
MailAddressMailbox,
|
|
Mailbox,
|
|
)
|
|
from gvawebcore.forms import PasswordModelFormMixin
|
|
|
|
from model_utils import Choices
|
|
|
|
MAILBOX_OR_FORWARDS = Choices(
|
|
(0, 'mailbox', _('Mailbox')),
|
|
(1, 'forwards', _('Forwards')),
|
|
)
|
|
|
|
|
|
class CreateMailboxForm(PasswordModelFormMixin, forms.ModelForm):
|
|
"""
|
|
This form is used to create new Mailbox instances.
|
|
|
|
"""
|
|
class Meta:
|
|
model = Mailbox
|
|
fields = []
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.hosting_package = kwargs.pop('hostingpackage')
|
|
super(CreateMailboxForm, self).__init__(*args, **kwargs)
|
|
self.helper = FormHelper()
|
|
self.helper.form_action = reverse(
|
|
'create_mailbox', kwargs={'package': self.hosting_package.id})
|
|
self.helper.add_input(Submit('submit', _('Create mailbox')))
|
|
|
|
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)
|
|
self.instance.set_password(self.cleaned_data['password1'])
|
|
return super(CreateMailboxForm, self).save(commit=commit)
|
|
|
|
|
|
class ChangeMailboxPasswordForm(PasswordModelFormMixin, forms.ModelForm):
|
|
"""
|
|
This form is used to set a new password for an existing mailbox.
|
|
|
|
"""
|
|
class Meta:
|
|
model = Mailbox
|
|
fields = []
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.hosting_package = kwargs.pop('hostingpackage')
|
|
super(ChangeMailboxPasswordForm, self).__init__(*args, **kwargs)
|
|
self.helper = FormHelper()
|
|
self.helper.form_action = reverse(
|
|
'change_mailbox_password', kwargs={
|
|
'package': self.hosting_package.id,
|
|
'slug': self.instance.username,
|
|
})
|
|
self.helper.add_input(Submit('submit', _('Set password')))
|
|
|
|
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`
|
|
|
|
"""
|
|
self.instance.set_password(self.cleaned_data['password1'])
|
|
return super(ChangeMailboxPasswordForm, self).save(commit=commit)
|
|
|
|
|
|
def multiple_email_validator(value):
|
|
if value:
|
|
for email in [part.strip() for part in value.split(',')]:
|
|
validate_email(email)
|
|
return value
|
|
|
|
|
|
class AddMailAddressForm(forms.ModelForm):
|
|
"""
|
|
This form is used to add a new mail address.
|
|
|
|
"""
|
|
mailbox_or_forwards = forms.TypedChoiceField(
|
|
label=_('Mailbox or Forwards'),
|
|
choices=MAILBOX_OR_FORWARDS,
|
|
widget=forms.RadioSelect,
|
|
coerce=int,
|
|
)
|
|
mailbox = forms.ModelChoiceField(
|
|
Mailbox.objects,
|
|
label=_('Mailbox'),
|
|
required=False,
|
|
)
|
|
forwards = forms.CharField(
|
|
label=_('Forwards'),
|
|
required=False,
|
|
error_messages={
|
|
'invalid': _(
|
|
'Please enter one or more email addresses separated by '
|
|
'commas.'),
|
|
},
|
|
validators=[multiple_email_validator],
|
|
)
|
|
|
|
class Meta:
|
|
model = MailAddress
|
|
fields = ['localpart']
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
self.maildomain = kwargs.pop('maildomain')
|
|
self.hosting_package = kwargs.pop('hostingpackage')
|
|
super(AddMailAddressForm, self).__init__(*args, **kwargs)
|
|
self.fields['mailbox'].queryset = Mailbox.objects.filter(
|
|
active=True, osuser=self.hosting_package.osuser,
|
|
)
|
|
|
|
self.helper = FormHelper()
|
|
self.helper.form_action = reverse(
|
|
'add_mailaddress', kwargs={
|
|
'package': self.hosting_package.id,
|
|
'domain': self.maildomain.domain,
|
|
})
|
|
self.helper.layout = Layout(
|
|
Div(
|
|
Div(
|
|
AppendedText('localpart', '@' + self.maildomain.domain),
|
|
css_class='col-lg-4 col-md-4 col-xs-12',
|
|
),
|
|
Div(
|
|
'mailbox_or_forwards',
|
|
css_class='col-lg-2 col-md-2 col-xs-12',
|
|
),
|
|
Div(
|
|
'mailbox',
|
|
'forwards',
|
|
css_class='col-lg-6 col-md-6 col-xs-12',
|
|
),
|
|
css_class='row',
|
|
),
|
|
Submit('submit', _('Add mailaddress')),
|
|
)
|
|
|
|
def clean_localpart(self):
|
|
localpart = self.cleaned_data['localpart']
|
|
if MailAddress.objects.filter(
|
|
domain=self.maildomain,
|
|
localpart=localpart,
|
|
).exists():
|
|
raise forms.ValidationError(
|
|
_('This mail address is already in use.'))
|
|
validate_email('{0}@{1}'.format(localpart, self.maildomain.domain))
|
|
return localpart
|
|
|
|
def clean(self):
|
|
data = self.cleaned_data
|
|
if data['mailbox_or_forwards'] == MAILBOX_OR_FORWARDS.mailbox:
|
|
if not data['mailbox']:
|
|
raise forms.ValidationError(_('No mailbox selected'))
|
|
elif data['mailbox_or_forwards'] == MAILBOX_OR_FORWARDS.forwards:
|
|
if 'forwards' not in data:
|
|
raise forms.ValidationError(
|
|
_('No forward addresses selected'))
|
|
else:
|
|
raise forms.ValidationError(
|
|
_('Illegal choice for target of the mail address'))
|
|
|
|
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
|
|
target_choice = self.cleaned_data['mailbox_or_forwards']
|
|
mailaddress = super(AddMailAddressForm, self).save(commit)
|
|
if target_choice == MAILBOX_OR_FORWARDS.mailbox:
|
|
mailbox = MailAddressMailbox(
|
|
mailaddress=mailaddress,
|
|
mailbox=self.cleaned_data['mailbox'])
|
|
if commit:
|
|
mailbox.save()
|
|
elif target_choice == MAILBOX_OR_FORWARDS.forwards:
|
|
for address in [
|
|
part.strip() for part in
|
|
self.cleaned_data['forwards'].split(',')
|
|
]:
|
|
forward = MailAddressForward(
|
|
mailaddress=mailaddress, target=part)
|
|
if commit:
|
|
forward.save()
|
|
return mailaddress
|