Source code for web.views.email_views.EmailDetailWithDeleteView

# 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.EmailDetailWithDeleteView` 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.db.models import Prefetch, QuerySet
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 Email, EmailCorrespondent
from core.utils.fetchers.exceptions import FetcherError
from web.mixins import CustomActionMixin
from web.views.base import DetailWithDeleteView

from .EmailFilterView import EmailFilterView

if TYPE_CHECKING:
    from django.http import HttpRequest, HttpResponse


[docs] class EmailDetailWithDeleteView( LoginRequiredMixin, DetailWithDeleteView, CustomActionMixin ): """View for a single :class:`core.models.Email` instance.""" URL_NAME = Email.get_detail_web_url_name() model = Email template_name = "web/email/email_detail.html" success_url = reverse_lazy("web:" + EmailFilterView.URL_NAME)
[docs] @override def get_queryset(self) -> QuerySet[Email]: """Restricts the queryset to objects owned by the requesting user.""" return ( super() .get_queryset() .filter(mailbox__account__user=self.request.user) .select_related("mailbox", "mailbox__account") .prefetch_related( "attachments", "in_reply_to", "replies", "references", "referenced_by" ) .prefetch_related( Prefetch( "emailcorrespondents", queryset=EmailCorrespondent.objects.select_related("correspondent"), ) ) )
[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_reprocess(self, request: HttpRequest) -> HttpResponse: """Handler function for the `reprocess` 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.reprocess() messages.success(request, _("Email successfully reprocessed.")) return self.get(request)
[docs] def handle_restore(self, request: HttpRequest) -> HttpResponse: """Handler function for the `restore` 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.restore_to_mailbox() except NotImplementedError: messages.error( request, _("POP accounts do not support restoring of emails to mailbox."), ) except FileNotFoundError: messages.error( request, _("Restoring of email failed: %(error)s") % {"error": _("eml file not found.")}, ) except FetcherError as error: messages.error( request, _("Restoring of email to mailbox failed: %(error)s") % {"error": str(error)}, ) else: messages.success(request, _("Email successfully restored to mailbox.")) return self.get(request)