From eb5b63c008b6308623e6d0e7f2067fc7bdc83e0d Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Thu, 29 Aug 2019 14:12:26 +0200 Subject: [PATCH] =?UTF-8?q?See=20#890:=20include=20notes=20in=20reports=20?= =?UTF-8?q?API=C2=A0representations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- api/funkwhale_api/manage/serializers.py | 35 +++++++++++++++++-------- api/funkwhale_api/manage/views.py | 3 +++ api/funkwhale_api/moderation/models.py | 9 +++++-- 3 files changed, 34 insertions(+), 13 deletions(-) diff --git a/api/funkwhale_api/manage/serializers.py b/api/funkwhale_api/manage/serializers.py index 5b74c584d..134cfefcd 100644 --- a/api/funkwhale_api/manage/serializers.py +++ b/api/funkwhale_api/manage/serializers.py @@ -641,11 +641,30 @@ class ManageTagActionSerializer(common_serializers.ActionSerializer): return objects.delete() +class ManageBaseNoteSerializer(serializers.ModelSerializer): + author = ManageBaseActorSerializer(required=False, read_only=True) + + class Meta: + model = moderation_models.Note + fields = ["id", "uuid", "creation_date", "summary", "author"] + read_only_fields = ["uuid", "creation_date", "author"] + + +class ManageNoteSerializer(ManageBaseNoteSerializer): + target = common_fields.GenericRelation( + moderation_utils.NOTE_TARGET_FIELDS + ) + + class Meta(ManageBaseNoteSerializer.Meta): + fields = ManageBaseNoteSerializer.Meta.fields + ['target'] + + class ManageReportSerializer(serializers.ModelSerializer): assigned_to = ManageBaseActorSerializer() target_owner = ManageBaseActorSerializer() submitter = ManageBaseActorSerializer() target = moderation_serializers.TARGET_FIELD + notes = serializers.SerializerMethodField() class Meta: model = moderation_models.Report @@ -664,6 +683,7 @@ class ManageReportSerializer(serializers.ModelSerializer): "target_owner", "submitter", "submitter_email", + "notes", ] read_only_fields = [ "id", @@ -677,16 +697,9 @@ class ManageReportSerializer(serializers.ModelSerializer): "target_state", "target_owner", "summary", + # "notes", ] - -class ManageNoteSerializer(serializers.ModelSerializer): - author = ManageBaseActorSerializer(required=False, read_only=True) - target = common_fields.GenericRelation( - moderation_utils.NOTE_TARGET_FIELDS - ) - - class Meta: - model = moderation_models.Note - fields = ["id", "uuid", "creation_date", "summary", "author", "target"] - read_only_fields = ["uuid", "creation_date", "author"] + def get_notes(self, o): + notes = getattr(o, '_prefetched_notes', []) + return ManageBaseNoteSerializer(notes, many=True).data diff --git a/api/funkwhale_api/manage/views.py b/api/funkwhale_api/manage/views.py index 51ff23a64..38cfef396 100644 --- a/api/funkwhale_api/manage/views.py +++ b/api/funkwhale_api/manage/views.py @@ -472,6 +472,9 @@ class ManageReportViewSet( .order_by("-creation_date") .select_related('submitter', 'target_owner', 'assigned_to', 'target_content_type') .prefetch_related('target') + .prefetch_related( + Prefetch('notes', queryset=moderation_models.Note.objects.order_by('creation_date').select_related('author'), to_attr="_prefetched_notes") + ) ) serializer_class = serializers.ManageReportSerializer filterset_class = filters.ManageReportFilterSet diff --git a/api/funkwhale_api/moderation/models.py b/api/funkwhale_api/moderation/models.py index 1aebfddf6..dc20ae32f 100644 --- a/api/funkwhale_api/moderation/models.py +++ b/api/funkwhale_api/moderation/models.py @@ -1,8 +1,7 @@ import urllib.parse import uuid - -from django.contrib.contenttypes.fields import GenericForeignKey +from django.contrib.contenttypes.fields import GenericForeignKey, GenericRelation from django.contrib.contenttypes.models import ContentType from django.contrib.postgres.fields import JSONField from django.db import models @@ -149,6 +148,12 @@ class Report(federation_models.FederationMixin): # delete target_state = JSONField(null=True) + notes = GenericRelation( + "Note", + content_type_field="target_content_type", + object_id_field="target_id", + ) + def get_federation_id(self): if self.fid: return self.fid