2015-01-25 12:10:17 +01:00
|
|
|
"""
|
|
|
|
This module defines views for mailbox and mail address handling.
|
|
|
|
|
|
|
|
"""
|
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 django.contrib import messages
|
2015-01-25 12:10:17 +01:00
|
|
|
from django.http import HttpResponseForbidden
|
|
|
|
from django.shortcuts import get_object_or_404, redirect
|
2023-02-18 22:46:48 +01:00
|
|
|
from django.utils.translation import gettext as _
|
|
|
|
from django.views.generic.edit import CreateView, DeleteView, UpdateView
|
2015-01-25 12:10:17 +01:00
|
|
|
from gvacommon.viewmixins import StaffOrSelfLoginRequiredMixin
|
|
|
|
|
2015-01-25 18:20:51 +01:00
|
|
|
from domains.models import MailDomain
|
2023-02-18 22:46:48 +01:00
|
|
|
from gvawebcore.views import HostingPackageAndCustomerMixin
|
|
|
|
|
2015-01-25 12:49:31 +01:00
|
|
|
from .forms import (
|
2023-02-18 22:46:48 +01:00
|
|
|
MAILBOX_OR_FORWARDS,
|
2015-01-25 18:20:51 +01:00
|
|
|
AddMailAddressForm,
|
2015-01-25 12:49:31 +01:00
|
|
|
ChangeMailboxPasswordForm,
|
2015-01-25 18:20:51 +01:00
|
|
|
CreateMailboxForm,
|
2015-01-25 22:07:54 +01:00
|
|
|
EditMailAddressForm,
|
2015-01-25 12:49:31 +01:00
|
|
|
)
|
2023-02-18 22:46:48 +01:00
|
|
|
from .models import MailAddress, MailAddressMailbox, Mailbox
|
2015-01-25 12:10:17 +01:00
|
|
|
|
2015-01-25 12:49:31 +01:00
|
|
|
|
|
|
|
class CreateMailbox(
|
|
|
|
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, CreateView
|
|
|
|
):
|
2015-01-25 12:10:17 +01:00
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
This view is used to set up new mailboxes for a customer hosting package.
|
2015-01-25 12:10:17 +01:00
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
2015-01-25 12:10:17 +01:00
|
|
|
model = Mailbox
|
2023-02-18 22:46:48 +01:00
|
|
|
context_object_name = "mailbox"
|
|
|
|
template_name_suffix = "_create"
|
2015-01-25 12:10:17 +01:00
|
|
|
form_class = CreateMailboxForm
|
|
|
|
|
|
|
|
def dispatch(self, request, *args, **kwargs):
|
|
|
|
resp = super(CreateMailbox, self).dispatch(request, *args, **kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
if request.method != "POST":
|
2015-11-28 20:01:52 +01:00
|
|
|
if not self.get_hosting_package().may_add_mailbox():
|
|
|
|
resp = HttpResponseForbidden(
|
2023-02-18 22:46:48 +01:00
|
|
|
_(
|
|
|
|
"You are not allowed to add more mailboxes to this"
|
|
|
|
" hosting package"
|
|
|
|
)
|
|
|
|
)
|
2015-01-25 12:10:17 +01:00
|
|
|
return resp
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(CreateMailbox, self).get_context_data(**kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
context["hostingpackage"] = self.get_hosting_package()
|
|
|
|
context["customer"] = self.get_customer_object()
|
2015-01-25 12:10:17 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(CreateMailbox, self).get_form_kwargs()
|
2023-02-18 22:46:48 +01:00
|
|
|
kwargs["hostingpackage"] = self.get_hosting_package()
|
2015-01-25 12:10:17 +01:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
mailbox = form.save()
|
|
|
|
messages.success(
|
|
|
|
self.request,
|
2023-02-18 22:46:48 +01:00
|
|
|
_("Mailbox {mailbox} created successfully.").format(
|
2015-01-25 12:10:17 +01:00
|
|
|
mailbox=mailbox.username
|
2023-02-18 22:46:48 +01:00
|
|
|
),
|
2015-01-25 12:10:17 +01:00
|
|
|
)
|
2015-01-25 12:49:31 +01:00
|
|
|
return redirect(self.get_hosting_package())
|
|
|
|
|
|
|
|
|
|
|
|
class ChangeMailboxPassword(
|
|
|
|
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, UpdateView
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
This view is used to set a new password for an existing mailbox.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
context_object_name = "mailbox"
|
2015-01-25 12:49:31 +01:00
|
|
|
form_class = ChangeMailboxPasswordForm
|
|
|
|
model = Mailbox
|
2023-02-18 22:46:48 +01:00
|
|
|
slug_field = "username"
|
|
|
|
template_name_suffix = "_setpassword"
|
2015-01-25 12:49:31 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(ChangeMailboxPassword, self).get_context_data(**kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
context["hostingpackage"] = self.get_hosting_package()
|
|
|
|
context["customer"] = self.get_customer_object()
|
2015-01-25 12:49:31 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(ChangeMailboxPassword, self).get_form_kwargs()
|
2023-02-18 22:46:48 +01:00
|
|
|
kwargs["hostingpackage"] = self.get_hosting_package()
|
2015-01-25 12:49:31 +01:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
mailbox = form.save()
|
|
|
|
messages.success(
|
|
|
|
self.request,
|
2023-02-18 22:46:48 +01:00
|
|
|
_("Successfully set new password for mailbox {mailbox}.").format(
|
2015-01-25 12:49:31 +01:00
|
|
|
mailbox=mailbox.username
|
2023-02-18 22:46:48 +01:00
|
|
|
),
|
2015-01-25 12:49:31 +01:00
|
|
|
)
|
|
|
|
return redirect(self.get_hosting_package())
|
2015-01-25 18:20:51 +01:00
|
|
|
|
|
|
|
|
|
|
|
class AddMailAddress(
|
|
|
|
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, CreateView
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
This view is used to add a new mail address to a domain.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
context_object_name = "mailaddress"
|
2015-01-25 18:20:51 +01:00
|
|
|
form_class = AddMailAddressForm
|
|
|
|
model = MailAddress
|
2023-02-18 22:46:48 +01:00
|
|
|
template_name_suffix = "_create"
|
2015-01-25 18:20:51 +01:00
|
|
|
|
2015-01-25 19:03:58 +01:00
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(AddMailAddress, self).get_context_data(**kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
context["customer"] = self.get_customer_object()
|
2015-01-25 19:03:58 +01:00
|
|
|
return context
|
|
|
|
|
2015-01-25 18:20:51 +01:00
|
|
|
def get_maildomain(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
return get_object_or_404(MailDomain, domain=self.kwargs["domain"])
|
2015-01-25 18:20:51 +01:00
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(AddMailAddress, self).get_form_kwargs()
|
2023-02-18 22:46:48 +01:00
|
|
|
kwargs.update(
|
|
|
|
{
|
|
|
|
"hostingpackage": self.get_hosting_package(),
|
|
|
|
"maildomain": self.get_maildomain(),
|
|
|
|
}
|
|
|
|
)
|
2015-01-25 18:20:51 +01:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
address = form.save()
|
|
|
|
messages.success(
|
|
|
|
self.request,
|
2023-02-18 22:46:48 +01:00
|
|
|
_("Successfully added mail address {mailaddress}").format(
|
|
|
|
mailaddress=address
|
|
|
|
),
|
2015-01-25 18:20:51 +01:00
|
|
|
)
|
|
|
|
return redirect(self.get_hosting_package())
|
2015-01-25 19:03:58 +01:00
|
|
|
|
|
|
|
|
|
|
|
class DeleteMailAddress(
|
|
|
|
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, DeleteView
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
This view is used to delete a mail address.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
context_object_name = "mailaddress"
|
2015-01-25 19:03:58 +01:00
|
|
|
model = MailAddress
|
|
|
|
|
|
|
|
def get_maildomain(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
return get_object_or_404(MailDomain, domain=self.kwargs["domain"])
|
2015-01-25 19:03:58 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(DeleteMailAddress, self).get_context_data(**kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
context.update(
|
|
|
|
{
|
|
|
|
"customer": self.get_customer_object(),
|
|
|
|
"hostingpackage": self.get_hosting_package(),
|
|
|
|
"maildomain": self.get_maildomain(),
|
|
|
|
}
|
|
|
|
)
|
2015-01-25 19:03:58 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_success_url(self):
|
|
|
|
return self.get_hosting_package().get_absolute_url()
|
2015-01-25 22:07:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
class EditMailAddress(
|
|
|
|
HostingPackageAndCustomerMixin, StaffOrSelfLoginRequiredMixin, UpdateView
|
|
|
|
):
|
|
|
|
"""
|
|
|
|
This view is used to edit a mail address' target mailbox or forwarding
|
|
|
|
addresses.
|
|
|
|
|
|
|
|
"""
|
2023-02-18 22:46:48 +01:00
|
|
|
|
|
|
|
context_object_name = "mailaddress"
|
2015-01-25 22:07:54 +01:00
|
|
|
form_class = EditMailAddressForm
|
|
|
|
model = MailAddress
|
2023-02-18 22:46:48 +01:00
|
|
|
template_name_suffix = "_edit"
|
2015-01-25 22:07:54 +01:00
|
|
|
|
|
|
|
def get_maildomain(self):
|
2023-02-18 22:46:48 +01:00
|
|
|
return get_object_or_404(MailDomain, domain=self.kwargs["domain"])
|
2015-01-25 22:07:54 +01:00
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(EditMailAddress, self).get_context_data(**kwargs)
|
2023-02-18 22:46:48 +01:00
|
|
|
context["customer"] = self.get_customer_object()
|
2015-01-25 22:07:54 +01:00
|
|
|
return context
|
|
|
|
|
|
|
|
def get_form_kwargs(self):
|
|
|
|
kwargs = super(EditMailAddress, self).get_form_kwargs()
|
2023-02-18 22:46:48 +01:00
|
|
|
kwargs.update(
|
|
|
|
{
|
|
|
|
"hostingpackage": self.get_hosting_package(),
|
|
|
|
"maildomain": self.get_maildomain(),
|
|
|
|
}
|
|
|
|
)
|
2015-01-25 22:07:54 +01:00
|
|
|
return kwargs
|
|
|
|
|
|
|
|
def get_initial(self):
|
|
|
|
initial = super(EditMailAddress, self).get_initial()
|
|
|
|
mailaddress = self.get_object()
|
|
|
|
if MailAddressMailbox.objects.filter(mailaddress=mailaddress).exists():
|
2023-02-18 22:46:48 +01:00
|
|
|
initial["mailbox"] = mailaddress.mailaddressmailbox.mailbox
|
|
|
|
initial["mailbox_or_forwards"] = MAILBOX_OR_FORWARDS.mailbox
|
2015-01-25 22:07:54 +01:00
|
|
|
elif mailaddress.mailaddressforward_set.exists():
|
2023-02-18 22:46:48 +01:00
|
|
|
initial["forwards"] = ", ".join(
|
2015-01-25 22:07:54 +01:00
|
|
|
fwd.target for fwd in mailaddress.mailaddressforward_set.all()
|
|
|
|
)
|
2023-02-18 22:46:48 +01:00
|
|
|
initial["mailbox_or_forwards"] = MAILBOX_OR_FORWARDS.forwards
|
2015-01-25 22:07:54 +01:00
|
|
|
return initial
|
|
|
|
|
|
|
|
def form_valid(self, form):
|
|
|
|
mailaddress = form.save()
|
|
|
|
messages.success(
|
|
|
|
self.request,
|
2023-02-18 22:46:48 +01:00
|
|
|
_("Successfully updated mail address {mailaddress} " "targets.").format(
|
|
|
|
mailaddress=mailaddress
|
|
|
|
),
|
2015-01-25 22:07:54 +01:00
|
|
|
)
|
|
|
|
return redirect(self.get_hosting_package())
|