views.py 2.07 KB
Newer Older
Agate's avatar
Agate committed
1
from rest_framework import mixins, response, viewsets
2 3
from rest_framework.decorators import list_route

Agate's avatar
Agate committed
4
from funkwhale_api.common import preferences
5
from funkwhale_api.music import models as music_models
Agate's avatar
Agate committed
6
from funkwhale_api.users import models as users_models
7 8
from funkwhale_api.users.permissions import HasUserPermission

Agate's avatar
Agate committed
9
from . import filters, serializers
10 11 12


class ManageTrackFileViewSet(
Agate's avatar
Agate committed
13 14 15 16 17
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
    mixins.DestroyModelMixin,
    viewsets.GenericViewSet,
):
18 19
    queryset = (
        music_models.TrackFile.objects.all()
Agate's avatar
Agate committed
20 21
        .select_related("track__artist", "track__album__artist", "library_track")
        .order_by("-id")
22 23 24 25
    )
    serializer_class = serializers.ManageTrackFileSerializer
    filter_class = filters.ManageTrackFileFilterSet
    permission_classes = (HasUserPermission,)
Agate's avatar
Agate committed
26
    required_permissions = ["library"]
27
    ordering_fields = [
Agate's avatar
Agate committed
28 29 30 31 32 33 34
        "accessed_date",
        "modification_date",
        "creation_date",
        "track__artist__name",
        "bitrate",
        "size",
        "duration",
35 36
    ]

Agate's avatar
Agate committed
37
    @list_route(methods=["post"])
38 39 40
    def action(self, request, *args, **kwargs):
        queryset = self.get_queryset()
        serializer = serializers.ManageTrackFileActionSerializer(
Agate's avatar
Agate committed
41
            request.data, queryset=queryset
42 43 44 45
        )
        serializer.is_valid(raise_exception=True)
        result = serializer.save()
        return response.Response(result, status=200)
Agate's avatar
Agate committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64


class ManageUserViewSet(
    mixins.ListModelMixin,
    mixins.RetrieveModelMixin,
    mixins.UpdateModelMixin,
    viewsets.GenericViewSet,
):
    queryset = users_models.User.objects.all().order_by("-id")
    serializer_class = serializers.ManageUserSerializer
    filter_class = filters.ManageUserFilterSet
    permission_classes = (HasUserPermission,)
    required_permissions = ["settings"]
    ordering_fields = ["date_joined", "last_activity", "username"]

    def get_serializer_context(self):
        context = super().get_serializer_context()
        context["default_permissions"] = preferences.get("users__default_permissions")
        return context