implement mail address target editing
- extract common code into managemails.forms.MailAddressFieldMixin - move code from forms into managemails.models.MailAddress - implement managemails.models.MailboxManager.unused and unused_or_own - implement managemails.forms.EditMailAddressForm - add managemails.views.EditMailAddress - add URL pattern 'edit_mailaddress' to managemails.urls - add template managemails/mailaddress_edit.html - add changelog entry
This commit is contained in:
parent
bebcad8c86
commit
5429055f0d
6 changed files with 269 additions and 26 deletions
|
@ -23,9 +23,12 @@ from .forms import (
|
|||
AddMailAddressForm,
|
||||
ChangeMailboxPasswordForm,
|
||||
CreateMailboxForm,
|
||||
EditMailAddressForm,
|
||||
MAILBOX_OR_FORWARDS,
|
||||
)
|
||||
from .models import (
|
||||
MailAddress,
|
||||
MailAddressMailbox,
|
||||
Mailbox,
|
||||
)
|
||||
|
||||
|
@ -188,3 +191,55 @@ class DeleteMailAddress(
|
|||
|
||||
def get_success_url(self):
|
||||
return self.get_hosting_package().get_absolute_url()
|
||||
|
||||
|
||||
class EditMailAddress(
|
||||
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, UpdateView
|
||||
):
|
||||
"""
|
||||
This view is used to edit a mail address' target mailbox or forwarding
|
||||
addresses.
|
||||
|
||||
"""
|
||||
context_object_name = 'mailaddress'
|
||||
form_class = EditMailAddressForm
|
||||
model = MailAddress
|
||||
template_name_suffix = '_edit'
|
||||
|
||||
def get_maildomain(self):
|
||||
return get_object_or_404(MailDomain, domain=self.kwargs['domain'])
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super(EditMailAddress, self).get_context_data(**kwargs)
|
||||
context['customer'] = self.get_customer_object()
|
||||
return context
|
||||
|
||||
def get_form_kwargs(self):
|
||||
kwargs = super(EditMailAddress, self).get_form_kwargs()
|
||||
kwargs.update({
|
||||
'hostingpackage': self.get_hosting_package(),
|
||||
'maildomain': self.get_maildomain(),
|
||||
})
|
||||
return kwargs
|
||||
|
||||
def get_initial(self):
|
||||
initial = super(EditMailAddress, self).get_initial()
|
||||
mailaddress = self.get_object()
|
||||
if MailAddressMailbox.objects.filter(mailaddress=mailaddress).exists():
|
||||
initial['mailbox'] = mailaddress.mailaddressmailbox.mailbox
|
||||
initial['mailbox_or_forwards'] = MAILBOX_OR_FORWARDS.mailbox
|
||||
elif mailaddress.mailaddressforward_set.exists():
|
||||
initial['forwards'] = ", ".join(
|
||||
fwd.target for fwd in mailaddress.mailaddressforward_set.all()
|
||||
)
|
||||
initial['mailbox_or_forwards'] = MAILBOX_OR_FORWARDS.forwards
|
||||
return initial
|
||||
|
||||
def form_valid(self, form):
|
||||
mailaddress = form.save()
|
||||
messages.success(
|
||||
self.request,
|
||||
_('Successfully updated mail address {mailaddress} '
|
||||
'targets.').format(mailaddress=mailaddress)
|
||||
)
|
||||
return redirect(self.get_hosting_package())
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue