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-12-26 22:03:27 +00:00
|
|
|
from funkwhale_api.federation import models as federation_models
|
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
|
|
|
|
|
|
|
|
2018-09-22 12:29:30 +00:00
|
|
|
class ManageUploadViewSet(
|
2018-06-21 21:31:12 +00:00
|
|
|
mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
|
2018-06-09 13:36:16 +00:00
|
|
|
):
|
2018-05-28 22:07:20 +00:00
|
|
|
queryset = (
|
2018-09-22 12:29:30 +00:00
|
|
|
music_models.Upload.objects.all()
|
2018-09-06 18:35:02 +00:00
|
|
|
.select_related("track__artist", "track__album__artist")
|
2018-06-09 13:36:16 +00:00
|
|
|
.order_by("-id")
|
2018-05-28 22:07:20 +00:00
|
|
|
)
|
2018-09-22 12:29:30 +00:00
|
|
|
serializer_class = serializers.ManageUploadSerializer
|
|
|
|
filter_class = filters.ManageUploadFilterSet
|
2018-05-28 22:07:20 +00:00
|
|
|
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()
|
2018-09-22 12:29:30 +00:00
|
|
|
serializer = serializers.ManageUploadActionSerializer(
|
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,
|
|
|
|
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)
|
2018-12-26 22:03:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
class ManageDomainViewSet(
|
|
|
|
mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
|
|
|
|
):
|
|
|
|
queryset = (
|
|
|
|
federation_models.Domain.objects.external()
|
|
|
|
.with_last_activity_date()
|
|
|
|
.with_actors_count()
|
|
|
|
.with_outbox_activities_count()
|
|
|
|
.order_by("name")
|
|
|
|
)
|
|
|
|
serializer_class = serializers.ManageDomainSerializer
|
|
|
|
filter_class = filters.ManageDomainFilterSet
|
|
|
|
permission_classes = (HasUserPermission,)
|
|
|
|
required_permissions = ["moderation"]
|
|
|
|
ordering_fields = [
|
|
|
|
"name",
|
|
|
|
"creation_date",
|
|
|
|
"last_activity_date",
|
|
|
|
"actors_count",
|
|
|
|
"outbox_activities_count",
|
|
|
|
]
|