2024-04-17 11:19:22 +02:00
|
|
|
"""Client serializers for People's mailbox manager app."""
|
2024-04-16 17:06:43 +02:00
|
|
|
|
|
|
|
|
from rest_framework import serializers
|
|
|
|
|
|
2024-09-14 00:59:38 +02:00
|
|
|
from core.api.serializers import UserSerializer
|
|
|
|
|
|
|
|
|
|
from mailbox_manager import enums, models
|
2024-09-19 18:47:08 +02:00
|
|
|
from mailbox_manager.utils.dimail import DimailAPIClient
|
2024-04-16 17:06:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailboxSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""Serialize mailbox."""
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = models.Mailbox
|
2024-08-07 12:29:40 +02:00
|
|
|
fields = ["id", "first_name", "last_name", "local_part", "secondary_email"]
|
2024-08-05 12:20:44 +02:00
|
|
|
# everything is actually read-only as we do not allow update for now
|
|
|
|
|
read_only_fields = ["id"]
|
2024-04-16 17:06:43 +02:00
|
|
|
|
2024-09-19 18:47:08 +02:00
|
|
|
def create(self, validated_data):
|
|
|
|
|
"""
|
|
|
|
|
Override create function to fire a request on mailbox creation.
|
|
|
|
|
"""
|
|
|
|
|
client = DimailAPIClient()
|
|
|
|
|
client.send_mailbox_request(validated_data)
|
|
|
|
|
return models.Mailbox.objects.create(**validated_data)
|
|
|
|
|
|
2024-04-16 17:06:43 +02:00
|
|
|
|
|
|
|
|
class MailDomainSerializer(serializers.ModelSerializer):
|
|
|
|
|
"""Serialize mail domain."""
|
|
|
|
|
|
2024-08-06 00:04:51 +02:00
|
|
|
abilities = serializers.SerializerMethodField(read_only=True)
|
|
|
|
|
|
2024-04-16 17:06:43 +02:00
|
|
|
class Meta:
|
|
|
|
|
model = models.MailDomain
|
2024-06-03 16:59:55 +02:00
|
|
|
lookup_field = "slug"
|
2024-04-17 11:19:22 +02:00
|
|
|
fields = [
|
|
|
|
|
"id",
|
|
|
|
|
"name",
|
2024-06-18 15:10:15 +02:00
|
|
|
"slug",
|
2024-08-08 15:55:16 +02:00
|
|
|
"status",
|
2024-08-06 00:04:51 +02:00
|
|
|
"abilities",
|
2024-04-17 11:19:22 +02:00
|
|
|
"created_at",
|
|
|
|
|
"updated_at",
|
|
|
|
|
]
|
2024-06-19 16:48:07 +02:00
|
|
|
read_only_fields = [
|
|
|
|
|
"id",
|
|
|
|
|
"slug",
|
2024-08-08 15:55:16 +02:00
|
|
|
"status",
|
2024-08-06 00:04:51 +02:00
|
|
|
"abilities",
|
2024-06-19 16:48:07 +02:00
|
|
|
"created_at",
|
|
|
|
|
"updated_at",
|
|
|
|
|
]
|
2024-04-17 11:19:22 +02:00
|
|
|
|
2024-08-06 00:04:51 +02:00
|
|
|
def get_abilities(self, domain) -> dict:
|
|
|
|
|
"""Return abilities of the logged-in user on the instance."""
|
|
|
|
|
request = self.context.get("request")
|
|
|
|
|
if request:
|
|
|
|
|
return domain.get_abilities(request.user)
|
|
|
|
|
return {}
|
|
|
|
|
|
2024-04-17 11:19:22 +02:00
|
|
|
|
|
|
|
|
class MailDomainAccessSerializer(serializers.ModelSerializer):
|
2024-09-14 00:59:38 +02:00
|
|
|
"""Serialize mail domain access."""
|
|
|
|
|
|
|
|
|
|
user = UserSerializer(read_only=True, fields=["id", "name", "email"])
|
|
|
|
|
can_set_role_to = serializers.SerializerMethodField(read_only=True)
|
2024-04-17 11:19:22 +02:00
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = models.MailDomainAccess
|
|
|
|
|
fields = [
|
|
|
|
|
"id",
|
|
|
|
|
"user",
|
|
|
|
|
"role",
|
2024-09-14 00:59:38 +02:00
|
|
|
"can_set_role_to",
|
|
|
|
|
]
|
|
|
|
|
read_only_fields = ["id", "user", "can_set_role_to"]
|
|
|
|
|
|
|
|
|
|
def get_can_set_role_to(self, access):
|
|
|
|
|
"""Return roles available to set"""
|
|
|
|
|
roles = list(enums.MailDomainRoleChoices)
|
|
|
|
|
# get role of authenticated user
|
|
|
|
|
authenticated_user_role = access.user_role
|
|
|
|
|
if authenticated_user_role != enums.MailDomainRoleChoices.OWNER:
|
|
|
|
|
roles.remove(enums.MailDomainRoleChoices.OWNER)
|
|
|
|
|
# if the user authenticated is a viewer, they can't modify role
|
|
|
|
|
# and only an owner can change role of an owner
|
|
|
|
|
if authenticated_user_role == enums.MailDomainRoleChoices.VIEWER or (
|
|
|
|
|
authenticated_user_role != enums.MailDomainRoleChoices.OWNER
|
|
|
|
|
and access.role == enums.MailDomainRoleChoices.OWNER
|
|
|
|
|
):
|
|
|
|
|
return []
|
|
|
|
|
# we only want to return other roles available to change,
|
|
|
|
|
# so we remove the current role of current access.
|
|
|
|
|
roles.remove(access.role)
|
|
|
|
|
return sorted(roles)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MailDomainAccessReadOnlySerializer(MailDomainAccessSerializer):
|
|
|
|
|
"""Serialize mail domain access for list and retrieve actions."""
|
|
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
|
model = models.MailDomainAccess
|
|
|
|
|
fields = [
|
|
|
|
|
"id",
|
|
|
|
|
"user",
|
|
|
|
|
"role",
|
|
|
|
|
"can_set_role_to",
|
|
|
|
|
]
|
|
|
|
|
read_only_fields = [
|
|
|
|
|
"id",
|
|
|
|
|
"user",
|
|
|
|
|
"role",
|
|
|
|
|
"can_set_role_to",
|
2024-04-17 11:19:22 +02:00
|
|
|
]
|