Source code for web.views.account_views.AccountDetailWithDeleteView
# SPDX-License-Identifier: AGPL-3.0-or-later
#
# Eonvelope - a open-source self-hostable email archiving server
# Copyright (C) 2024 David Aderbauer & The Eonvelope Contributors
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""Module with the :class:`web.views.AccountDetailWithDeleteView` view."""
from __future__ import annotations
from typing import TYPE_CHECKING, Any, override
from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic.edit import DeletionMixin
from core.models import Account, Daemon, Email
from core.utils.fetchers.exceptions import MailAccountError
from web.mixins.CustomActionMixin import CustomActionMixin
from web.mixins.TestActionMixin import TestActionMixin
from web.views.base import DetailWithDeleteView
from .AccountFilterView import AccountFilterView
if TYPE_CHECKING:
from django.db.models import QuerySet
from django.http import HttpRequest, HttpResponse
[docs]
class AccountDetailWithDeleteView(
LoginRequiredMixin,
DetailWithDeleteView,
CustomActionMixin,
TestActionMixin,
):
"""View for a single :class:`core.models.Account` instance."""
URL_NAME = Account.get_detail_web_url_name()
model = Account
template_name = "web/account/account_detail.html"
success_url = reverse_lazy("web:" + AccountFilterView.URL_NAME)
[docs]
@override
def get_queryset(self) -> QuerySet[Account]:
"""Restricts the queryset to objects owned by the requesting user."""
return (
super()
.get_queryset()
.filter(user=self.request.user)
.prefetch_related("mailboxes")
)
[docs]
@override
def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
"""Extended to add the accounts latest emails to the context."""
context = super().get_context_data(**kwargs)
context["latest_emails"] = (
Email.objects.filter(mailbox__account=self.object)
.order_by("-created")
.select_related("mailbox", "mailbox__account")[:25]
)
context["account_daemons"] = Daemon.objects.filter(
mailbox__in=self.object.mailboxes.all()
).select_related("mailbox", "mailbox__account", "interval")
return context
[docs]
@override
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
if "delete" in request.POST:
return DeletionMixin.post(self, request)
return CustomActionMixin.post(self, request)
[docs]
def handle_update_mailboxes(self, request: HttpRequest) -> HttpResponse:
"""Handler function for the `update-mailboxes` action.
Args:
request: The action request to handle.
Returns:
A template response with the updated view after the action.
"""
self.object = self.get_object()
try:
self.object.update_mailboxes()
except MailAccountError as error:
messages.error(
request,
_("Updating mailboxes failed: %(error)s") % {"error": str(error)},
)
else:
messages.success(request, _("Updating mailboxes successful"))
self.object.refresh_from_db()
return self.get(request)
[docs]
def handle_add_daemons(self, request: HttpRequest) -> HttpResponse:
"""Handler function for the `add-daemons` action.
Args:
request: The action request to handle.
Returns:
A template response with the updated view after the action.
"""
self.object = self.get_object()
self.object.add_daemons()
messages.success(request, _("Adding routines successful"))
return self.get(request)