gva/gnuviechadmin/managemails/views.py
Jan Dittberner 449af174ec implement create_mailbox functionality
- implement managemails.forms.CreateMailboxForm
- implement managemails.views.CreateMailbox
- add url pattern 'create_mailbox' to managemails.urls
- add templates managemails/base.html and managemails/mailbox_create.html
- add german translation
- add generated code documentation
2015-01-25 12:10:17 +01:00

66 lines
2.1 KiB
Python

"""
This module defines views for mailbox and mail address handling.
"""
from __future__ import absolute_import, unicode_literals
from django.http import HttpResponseForbidden
from django.shortcuts import get_object_or_404, redirect
from django.utils.translation import ugettext as _
from django.views.generic.edit import CreateView
from django.contrib import messages
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
from hostingpackages.models import CustomerHostingPackage
from .forms import CreateMailboxForm
from .models import Mailbox
class CreateMailbox(StaffOrSelfLoginRequiredMixin, CreateView):
"""
This view is used to setup new mailboxes for a customer hosting package.
"""
model = Mailbox
context_object_name = 'mailbox'
template_name_suffix = '_create'
form_class = CreateMailboxForm
def dispatch(self, request, *args, **kwargs):
resp = super(CreateMailbox, self).dispatch(request, *args, **kwargs)
if not self._get_hosting_package().may_add_mailbox():
resp = HttpResponseForbidden(
_('You are not allowed to add more mailboxes to this'
' hosting package'))
return resp
def _get_hosting_package(self):
return get_object_or_404(
CustomerHostingPackage, pk=int(self.kwargs['package']))
def get_customer_object(self):
return self._get_hosting_package().customer
def get_context_data(self, **kwargs):
context = super(CreateMailbox, self).get_context_data(**kwargs)
context['hostingpackage'] = self._get_hosting_package()
context['customer'] = self.get_customer_object()
return context
def get_form_kwargs(self):
kwargs = super(CreateMailbox, self).get_form_kwargs()
kwargs['hostingpackage'] = self._get_hosting_package()
return kwargs
def form_valid(self, form):
mailbox = form.save()
messages.success(
self.request,
_('Mailbox {mailbox} created successfully.').format(
mailbox=mailbox.username
)
)
return redirect(self._get_hosting_package().get_absolute_url())