mirror of
https://code.eliotberriot.com/funkwhale/funkwhale.git
synced 2025-10-05 03:49:24 +02:00
See #432: added admin API endpoints to retrieve and delete tags
This commit is contained in:
parent
c885c10be1
commit
13f36beec3
6 changed files with 127 additions and 1 deletions
|
@ -2,7 +2,7 @@ from rest_framework import mixins, response, viewsets
|
|||
from rest_framework import decorators as rest_decorators
|
||||
|
||||
from django.db.models import Count, Prefetch, Q, Sum, OuterRef, Subquery
|
||||
from django.db.models.functions import Coalesce
|
||||
from django.db.models.functions import Coalesce, Length
|
||||
from django.shortcuts import get_object_or_404
|
||||
|
||||
from funkwhale_api.common import models as common_models
|
||||
|
@ -14,6 +14,7 @@ from funkwhale_api.history import models as history_models
|
|||
from funkwhale_api.music import models as music_models
|
||||
from funkwhale_api.moderation import models as moderation_models
|
||||
from funkwhale_api.playlists import models as playlists_models
|
||||
from funkwhale_api.tags import models as tags_models
|
||||
from funkwhale_api.users import models as users_models
|
||||
|
||||
|
||||
|
@ -452,3 +453,43 @@ class ManageInstancePolicyViewSet(
|
|||
|
||||
def perform_create(self, serializer):
|
||||
serializer.save(actor=self.request.user.actor)
|
||||
|
||||
|
||||
class ManageTagViewSet(
|
||||
mixins.ListModelMixin,
|
||||
mixins.RetrieveModelMixin,
|
||||
mixins.DestroyModelMixin,
|
||||
mixins.CreateModelMixin,
|
||||
viewsets.GenericViewSet,
|
||||
):
|
||||
lookup_field = "name"
|
||||
queryset = (
|
||||
tags_models.Tag.objects.all()
|
||||
.order_by("-creation_date")
|
||||
.annotate(items_count=Count("tagged_items"))
|
||||
.annotate(length=Length("name"))
|
||||
)
|
||||
serializer_class = serializers.ManageTagSerializer
|
||||
filterset_class = filters.ManageTagFilterSet
|
||||
required_scope = "instance:libraries"
|
||||
ordering_fields = ["id", "creation_date", "name", "items_count", "length"]
|
||||
|
||||
def get_queryset(self):
|
||||
queryset = super().get_queryset()
|
||||
from django.contrib.contenttypes.models import ContentType
|
||||
|
||||
album_ct = ContentType.objects.get_for_model(music_models.Album)
|
||||
track_ct = ContentType.objects.get_for_model(music_models.Track)
|
||||
artist_ct = ContentType.objects.get_for_model(music_models.Artist)
|
||||
queryset = queryset.annotate(
|
||||
_albums_count=Count(
|
||||
"tagged_items", filter=Q(tagged_items__content_type=album_ct)
|
||||
),
|
||||
_tracks_count=Count(
|
||||
"tagged_items", filter=Q(tagged_items__content_type=track_ct)
|
||||
),
|
||||
_artists_count=Count(
|
||||
"tagged_items", filter=Q(tagged_items__content_type=artist_ct)
|
||||
),
|
||||
)
|
||||
return queryset
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue