Source code for api.v1.serializers.correspondent_serializers.CorrespondentSerializer

# 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:`CorrespondentSerializer` serializer class."""

# ruff: noqa: TC001 TC002
# TYPE_CHECKING guard doesn't work with drf-spectacular: https://github.com/tfranzel/drf-spectacular/issues/390

from __future__ import annotations

from typing import Any

from drf_spectacular.utils import extend_schema_field
from rest_framework import serializers
from rest_framework.utils.serializer_helpers import ReturnDict

from api.v1.serializers.emailcorrespondent_serializers import (
    CorrespondentEmailSerializer,
)
from core.models import Correspondent

from .BaseCorrespondentSerializer import BaseCorrespondentSerializer


[docs] class CorrespondentSerializer(BaseCorrespondentSerializer): """The standard serializer for a :class:`core.models.Correspondent.Correspondent`. Includes a nested serializer for the :attr:`core.models.Correspondent.Correspondent.emails` field. """ emails = serializers.SerializerMethodField(read_only=True) """The emails are set from the :class:`core.models.EmailCorrespondent.EmailCorrespondent` via :fu`core.models.EmailCorrespondent` """
[docs] @extend_schema_field(CorrespondentEmailSerializer(many=True)) def get_emails(self, instance: Correspondent) -> ReturnDict[str, Any]: """Serializes the emails connected to the instance to be serialized. Args: instance: The instance being serialized. Returns: The serialized emails connected to the instance to be serialized. An empty list if the the user is not authenticated. """ request = self.context.get("request") user = getattr(request, "user", None) if user is not None: correspondentemails = instance.correspondentemails.filter( email__mailbox__account__user=user ).distinct() else: correspondentemails = instance.correspondentemails.none() return CorrespondentEmailSerializer( correspondentemails, many=True, read_only=True ).data