From 7a7cc55143d1ed7f532b123307e43e5ce427ea5d Mon Sep 17 00:00:00 2001 From: Eliot Berriot Date: Thu, 29 Aug 2019 14:00:10 +0200 Subject: [PATCH] See #890: tweaks / perf improvements --- api/funkwhale_api/common/filters.py | 4 +++ api/funkwhale_api/manage/filters.py | 4 +++ api/funkwhale_api/manage/serializers.py | 17 ++-------- api/funkwhale_api/manage/views.py | 7 ++-- api/funkwhale_api/moderation/serializers.py | 36 ++++++++++----------- api/funkwhale_api/moderation/utils.py | 22 +++++++++++++ 6 files changed, 56 insertions(+), 34 deletions(-) create mode 100644 api/funkwhale_api/moderation/utils.py diff --git a/api/funkwhale_api/common/filters.py b/api/funkwhale_api/common/filters.py index 58244b67f..2d0f2b8b1 100644 --- a/api/funkwhale_api/common/filters.py +++ b/api/funkwhale_api/common/filters.py @@ -90,6 +90,10 @@ def get_boolean_filter(name): return {"handler": lambda v: Q(**{name: clean_boolean_filter(v)})} +def get_generic_relation_filter(relation_name, choices): + return {"handler": lambda v: fields.get_generic_filter_query(v, relation_name=relation_name, choices=choices)} + + class DummyTypedMultipleChoiceField(forms.TypedMultipleChoiceField): def valid_value(self, value): return True diff --git a/api/funkwhale_api/manage/filters.py b/api/funkwhale_api/manage/filters.py index 6cc69ea58..263af9635 100644 --- a/api/funkwhale_api/manage/filters.py +++ b/api/funkwhale_api/manage/filters.py @@ -11,6 +11,8 @@ from funkwhale_api.common import search from funkwhale_api.federation import models as federation_models from funkwhale_api.federation import utils as federation_utils from funkwhale_api.moderation import models as moderation_models +from funkwhale_api.moderation import serializers as moderation_serializers +from funkwhale_api.moderation import utils as moderation_utils from funkwhale_api.music import models as music_models from funkwhale_api.users import models as users_models from funkwhale_api.tags import models as tags_models @@ -356,6 +358,7 @@ class ManageReportFilterSet(filters.FilterSet): "assigned_to": get_actor_filter("assigned_to"), "target_owner": get_actor_filter("target_owner"), "submitter_email": {"to": "submitter_email"}, + "target": common_filters.get_generic_relation_filter("target", moderation_serializers.TARGET_CONFIG), }, ) ) @@ -372,6 +375,7 @@ class ManageNoteFilterSet(filters.FilterSet): filter_fields={ "uuid": {"to": "uuid"}, "author": get_actor_filter("author"), + "target": common_filters.get_generic_relation_filter("target", moderation_utils.NOTE_TARGET_FIELDS), }, ) ) diff --git a/api/funkwhale_api/manage/serializers.py b/api/funkwhale_api/manage/serializers.py index 0bd364183..5b74c584d 100644 --- a/api/funkwhale_api/manage/serializers.py +++ b/api/funkwhale_api/manage/serializers.py @@ -11,6 +11,7 @@ from funkwhale_api.federation import fields as federation_fields from funkwhale_api.federation import tasks as federation_tasks from funkwhale_api.moderation import models as moderation_models from funkwhale_api.moderation import serializers as moderation_serializers +from funkwhale_api.moderation import utils as moderation_utils from funkwhale_api.music import models as music_models from funkwhale_api.music import serializers as music_serializers from funkwhale_api.tags import models as tags_models @@ -680,21 +681,9 @@ class ManageReportSerializer(serializers.ModelSerializer): class ManageNoteSerializer(serializers.ModelSerializer): - author = ManageBaseActorSerializer(required=False) + author = ManageBaseActorSerializer(required=False, read_only=True) target = common_fields.GenericRelation( - { - "report": { - "queryset": moderation_models.Report.objects.all(), - "id_attr": "uuid", - "id_field": serializers.UUIDField(), - }, - "account": { - "queryset": federation_models.Actor.objects.all(), - "id_attr": "full_username", - "id_field": serializers.EmailField(), - "get_query": moderation_serializers.get_actor_query, - }, - } + moderation_utils.NOTE_TARGET_FIELDS ) class Meta: diff --git a/api/funkwhale_api/manage/views.py b/api/funkwhale_api/manage/views.py index 4c8f6655e..51ff23a64 100644 --- a/api/funkwhale_api/manage/views.py +++ b/api/funkwhale_api/manage/views.py @@ -470,7 +470,8 @@ class ManageReportViewSet( queryset = ( moderation_models.Report.objects.all() .order_by("-creation_date") - .select_related() + .select_related('submitter', 'target_owner', 'assigned_to', 'target_content_type') + .prefetch_related('target') ) serializer_class = serializers.ManageReportSerializer filterset_class = filters.ManageReportFilterSet @@ -487,7 +488,9 @@ class ManageNoteViewSet( ): lookup_field = "uuid" queryset = ( - moderation_models.Note.objects.all().order_by("-creation_date").select_related() + moderation_models.Note.objects.all().order_by("-creation_date") + .select_related('author', 'target_content_type') + .prefetch_related('target') ) serializer_class = serializers.ManageNoteSerializer filterset_class = filters.ManageNoteFilterSet diff --git a/api/funkwhale_api/moderation/serializers.py b/api/funkwhale_api/moderation/serializers.py index d6a954a15..f1b6b36b7 100644 --- a/api/funkwhale_api/moderation/serializers.py +++ b/api/funkwhale_api/moderation/serializers.py @@ -170,25 +170,25 @@ def get_target_owner(target): return mapping[target.__class__](target) - +TARGET_CONFIG = { + "artist": {"queryset": music_models.Artist.objects.all()}, + "album": {"queryset": music_models.Album.objects.all()}, + "track": {"queryset": music_models.Track.objects.all()}, + "library": { + "queryset": music_models.Library.objects.all(), + "id_attr": "uuid", + "id_field": serializers.UUIDField(), + }, + "playlist": {"queryset": playlists_models.Playlist.objects.all()}, + "account": { + "queryset": federation_models.Actor.objects.all(), + "id_attr": "full_username", + "id_field": serializers.EmailField(), + "get_query": get_actor_query, + }, +} TARGET_FIELD = common_fields.GenericRelation( - { - "artist": {"queryset": music_models.Artist.objects.all()}, - "album": {"queryset": music_models.Album.objects.all()}, - "track": {"queryset": music_models.Track.objects.all()}, - "library": { - "queryset": music_models.Library.objects.all(), - "id_attr": "uuid", - "id_field": serializers.UUIDField(), - }, - "playlist": {"queryset": playlists_models.Playlist.objects.all()}, - "account": { - "queryset": federation_models.Actor.objects.all(), - "id_attr": "full_username", - "id_field": serializers.EmailField(), - "get_query": get_actor_query, - }, - } + TARGET_CONFIG ) diff --git a/api/funkwhale_api/moderation/utils.py b/api/funkwhale_api/moderation/utils.py new file mode 100644 index 000000000..0b2e5bb04 --- /dev/null +++ b/api/funkwhale_api/moderation/utils.py @@ -0,0 +1,22 @@ + +from rest_framework import serializers + +from funkwhale_api.federation import models as federation_models + +from . import models +from . import serializers as moderation_serializers + + +NOTE_TARGET_FIELDS = { + "report": { + "queryset": models.Report.objects.all(), + "id_attr": "uuid", + "id_field": serializers.UUIDField(), + }, + "account": { + "queryset": federation_models.Actor.objects.all(), + "id_attr": "full_username", + "id_field": serializers.EmailField(), + "get_query": moderation_serializers.get_actor_query, + }, +}