(backend) add max_role field to the document access API endpoint

The frontend needs to know what to display on an access. The maximum
role between the access role and the role equivalent to all accesses
on the document's ancestors should be computed on the backend.
This commit is contained in:
Samuel Paccoud - DINUM
2025-05-13 17:58:45 +02:00
committed by Anthony LC
parent d232654c55
commit 04b8400766
3 changed files with 49 additions and 22 deletions

View File

@@ -306,6 +306,7 @@ class DocumentAccessSerializer(serializers.ModelSerializer):
team = serializers.CharField(required=False, allow_blank=True)
abilities = serializers.SerializerMethodField(read_only=True)
max_ancestors_role = serializers.SerializerMethodField(read_only=True)
max_role = serializers.SerializerMethodField(read_only=True)
class Meta:
model = models.DocumentAccess
@@ -319,8 +320,15 @@ class DocumentAccessSerializer(serializers.ModelSerializer):
"role",
"abilities",
"max_ancestors_role",
"max_role",
]
read_only_fields = [
"id",
"document",
"abilities",
"max_ancestors_role",
"max_role",
]
read_only_fields = ["id", "document", "abilities", "max_ancestors_role"]
def get_abilities(self, instance) -> dict:
"""Return abilities of the logged-in user on the instance."""
@@ -333,6 +341,13 @@ class DocumentAccessSerializer(serializers.ModelSerializer):
"""Return max_ancestors_role if annotated; else None."""
return getattr(instance, "max_ancestors_role", None)
def get_max_role(self, instance):
"""Return max_ancestors_role if annotated; else None."""
return choices.RoleChoices.max(
getattr(instance, "max_ancestors_role", None),
instance.role,
)
def update(self, instance, validated_data):
"""Make "user" field readonly but only on update."""
validated_data.pop("team", None)
@@ -356,6 +371,7 @@ class DocumentAccessLightSerializer(DocumentAccessSerializer):
"role",
"abilities",
"max_ancestors_role",
"max_role",
]
read_only_fields = [
"id",
@@ -364,6 +380,7 @@ class DocumentAccessLightSerializer(DocumentAccessSerializer):
"role",
"abilities",
"max_ancestors_role",
"max_role",
]