gva/gnuviechadmin/managemails/forms.py

82 lines
2.6 KiB
Python
Raw Normal View History

"""
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.utils.translation import ugettext as _
from crispy_forms.helper import FormHelper
from crispy_forms.layout import Submit
from .models import Mailbox
from gvawebcore.forms import PasswordModelFormMixin
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)