funkwhale/api/funkwhale_api/manage/views.py

99 wiersze
3.2 KiB
Python
Czysty Zwykły widok Historia

2018-06-10 08:55:16 +00:00
from rest_framework import mixins, response, viewsets
from rest_framework.decorators import list_route
2018-06-19 16:48:43 +00:00
from funkwhale_api.common import preferences
from funkwhale_api.music import models as music_models
2018-06-19 16:48:43 +00:00
from funkwhale_api.users import models as users_models
from funkwhale_api.users.permissions import HasUserPermission
2018-06-10 08:55:16 +00:00
from . import filters, serializers
class ManageTrackFileViewSet(
2018-06-09 13:36:16 +00:00
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.DestroyModelMixin,
viewsets.GenericViewSet,
):
queryset = (
music_models.TrackFile.objects.all()
2018-06-09 13:36:16 +00:00
.select_related("track__artist", "track__album__artist", "library_track")
.order_by("-id")
)
serializer_class = serializers.ManageTrackFileSerializer
filter_class = filters.ManageTrackFileFilterSet
permission_classes = (HasUserPermission,)
2018-06-09 13:36:16 +00:00
required_permissions = ["library"]
ordering_fields = [
2018-06-09 13:36:16 +00:00
"accessed_date",
"modification_date",
"creation_date",
"track__artist__name",
"bitrate",
"size",
"duration",
]
2018-06-09 13:36:16 +00:00
@list_route(methods=["post"])
def action(self, request, *args, **kwargs):
queryset = self.get_queryset()
serializer = serializers.ManageTrackFileActionSerializer(
2018-06-09 13:36:16 +00:00
request.data, queryset=queryset
)
serializer.is_valid(raise_exception=True)
result = serializer.save()
return response.Response(result, status=200)
2018-06-19 16:48:43 +00:00
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
class ManageInvitationViewSet(
mixins.CreateModelMixin,
mixins.ListModelMixin,
mixins.RetrieveModelMixin,
mixins.UpdateModelMixin,
mixins.DestroyModelMixin,
viewsets.GenericViewSet,
):
queryset = (
users_models.Invitation.objects.all()
.order_by("-id")
.prefetch_related("users")
.select_related("owner")
)
serializer_class = serializers.ManageInvitationSerializer
filter_class = filters.ManageInvitationFilterSet
permission_classes = (HasUserPermission,)
required_permissions = ["settings"]
ordering_fields = ["creation_date", "expiration_date"]
def perform_create(self, serializer):
serializer.save(owner=self.request.user)
@list_route(methods=["post"])
def action(self, request, *args, **kwargs):
queryset = self.get_queryset()
serializer = serializers.ManageInvitationActionSerializer(
request.data, queryset=queryset
)
serializer.is_valid(raise_exception=True)
result = serializer.save()
return response.Response(result, status=200)