Verified Commit 7a7cc551 authored by Eliot Berriot's avatar Eliot Berriot
Browse files

See #890: tweaks / perf improvements

parent b363d111
......@@ -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
......
......@@ -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),
},
)
)
......
......@@ -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:
......
......@@ -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
......
......@@ -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
)
......
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,
},
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment