2018-06-10 08:55:16 +00:00
|
|
|
from rest_framework import mixins, response, viewsets
|
2018-05-28 22:07:20 +00:00
|
|
|
from rest_framework.decorators import list_route
|
|
|
|
|
2018-06-19 16:48:43 +00:00
|
|
|
from funkwhale_api.common import preferences
|
2018-05-28 22:07:20 +00:00
|
|
|
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
|
2018-05-28 22:07:20 +00:00
|
|
|
from funkwhale_api.users.permissions import HasUserPermission
|
|
|
|
|
2018-06-10 08:55:16 +00:00
|
|
|
from . import filters, serializers
|
2018-05-28 22:07:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageTrackFileViewSet(
|
2018-06-09 13:36:16 +00:00
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
):
|
2018-05-28 22:07:20 +00:00
|
|
|
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")
|
2018-05-28 22:07:20 +00:00
|
|
|
)
|
|
|
|
serializer_class = serializers.ManageTrackFileSerializer
|
|
|
|
filter_class = filters.ManageTrackFileFilterSet
|
|
|
|
permission_classes = (HasUserPermission,)
|
2018-06-09 13:36:16 +00:00
|
|
|
required_permissions = ["library"]
|
2018-05-28 22:07:20 +00:00
|
|
|
ordering_fields = [
|
2018-06-09 13:36:16 +00:00
|
|
|
"accessed_date",
|
|
|
|
"modification_date",
|
|
|
|
"creation_date",
|
|
|
|
"track__artist__name",
|
|
|
|
"bitrate",
|
|
|
|
"size",
|
|
|
|
"duration",
|
2018-05-28 22:07:20 +00:00
|
|
|
]
|
|
|
|
|
2018-06-09 13:36:16 +00:00
|
|
|
@list_route(methods=["post"])
|
2018-05-28 22:07:20 +00:00
|
|
|
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
|
2018-05-28 22:07:20 +00:00
|
|
|
)
|
|
|
|
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
|
2018-06-19 21:27:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
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)
|
2018-06-21 17:22:51 +00:00
|
|
|
|
|
|
|
@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)
|