See #890: include notes in reports API representations

environments/review-docs-rate-jr6phc/deployments/2479
Eliot Berriot 2019-08-29 14:12:26 +02:00
rodzic 7a7cc55143
commit eb5b63c008
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: DD6965E2476E5C27
3 zmienionych plików z 34 dodań i 13 usunięć

Wyświetl plik

@ -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

Wyświetl plik

@ -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

Wyświetl plik

@ -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