2018-06-10 08:55:16 +00:00
|
|
|
from rest_framework import mixins, response, viewsets
|
2018-12-27 16:42:43 +00:00
|
|
|
from rest_framework.decorators import detail_route, list_route
|
2019-01-03 10:47:29 +00:00
|
|
|
from django.shortcuts import get_object_or_404
|
2018-05-28 22:07:20 +00:00
|
|
|
|
2019-01-09 13:18:32 +00:00
|
|
|
from funkwhale_api.common import preferences, decorators
|
2018-12-26 22:03:27 +00:00
|
|
|
from funkwhale_api.federation import models as federation_models
|
2018-12-27 16:42:43 +00:00
|
|
|
from funkwhale_api.federation import tasks as federation_tasks
|
2018-05-28 22:07:20 +00:00
|
|
|
from funkwhale_api.music import models as music_models
|
2019-01-07 08:45:53 +00:00
|
|
|
from funkwhale_api.moderation import models as moderation_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
|
2019-01-11 11:03:06 +00:00
|
|
|
filterset_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
|
2019-01-11 11:03:06 +00:00
|
|
|
filterset_class = filters.ManageUserFilterSet
|
2018-06-19 16:48:43 +00:00
|
|
|
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
|
2019-01-11 11:03:06 +00:00
|
|
|
filterset_class = filters.ManageInvitationFilterSet
|
2018-06-19 21:27:21 +00:00
|
|
|
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(
|
2019-01-05 17:21:35 +00:00
|
|
|
mixins.CreateModelMixin,
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
viewsets.GenericViewSet,
|
2018-12-26 22:03:27 +00:00
|
|
|
):
|
2018-12-27 19:39:03 +00:00
|
|
|
lookup_value_regex = r"[a-zA-Z0-9\-\.]+"
|
2018-12-26 22:03:27 +00:00
|
|
|
queryset = (
|
|
|
|
federation_models.Domain.objects.external()
|
|
|
|
.with_actors_count()
|
|
|
|
.with_outbox_activities_count()
|
2019-01-09 16:50:15 +00:00
|
|
|
.prefetch_related("instance_policy")
|
2018-12-26 22:03:27 +00:00
|
|
|
.order_by("name")
|
|
|
|
)
|
|
|
|
serializer_class = serializers.ManageDomainSerializer
|
2019-01-11 11:03:06 +00:00
|
|
|
filterset_class = filters.ManageDomainFilterSet
|
2018-12-26 22:03:27 +00:00
|
|
|
permission_classes = (HasUserPermission,)
|
|
|
|
required_permissions = ["moderation"]
|
|
|
|
ordering_fields = [
|
|
|
|
"name",
|
|
|
|
"creation_date",
|
2018-12-27 21:45:43 +00:00
|
|
|
"nodeinfo_fetch_date",
|
2018-12-26 22:03:27 +00:00
|
|
|
"actors_count",
|
|
|
|
"outbox_activities_count",
|
2019-01-09 16:50:15 +00:00
|
|
|
"instance_policy",
|
2018-12-26 22:03:27 +00:00
|
|
|
]
|
2018-12-27 16:42:43 +00:00
|
|
|
|
|
|
|
@detail_route(methods=["get"])
|
|
|
|
def nodeinfo(self, request, *args, **kwargs):
|
|
|
|
domain = self.get_object()
|
|
|
|
federation_tasks.update_domain_nodeinfo(domain_name=domain.name)
|
|
|
|
domain.refresh_from_db()
|
|
|
|
return response.Response(domain.nodeinfo, status=200)
|
2018-12-27 18:58:34 +00:00
|
|
|
|
|
|
|
@detail_route(methods=["get"])
|
|
|
|
def stats(self, request, *args, **kwargs):
|
|
|
|
domain = self.get_object()
|
|
|
|
return response.Response(domain.get_stats(), status=200)
|
2019-01-03 10:47:29 +00:00
|
|
|
|
2019-01-09 13:18:32 +00:00
|
|
|
action = decorators.action_route(serializers.ManageDomainActionSerializer)
|
|
|
|
|
2019-01-03 10:47:29 +00:00
|
|
|
|
|
|
|
class ManageActorViewSet(
|
|
|
|
mixins.ListModelMixin, mixins.RetrieveModelMixin, viewsets.GenericViewSet
|
|
|
|
):
|
|
|
|
lookup_value_regex = r"([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)"
|
|
|
|
queryset = (
|
|
|
|
federation_models.Actor.objects.all()
|
|
|
|
.with_uploads_count()
|
|
|
|
.order_by("-creation_date")
|
2019-01-03 16:10:02 +00:00
|
|
|
.select_related("user")
|
2019-01-09 16:50:15 +00:00
|
|
|
.prefetch_related("instance_policy")
|
2019-01-03 10:47:29 +00:00
|
|
|
)
|
|
|
|
serializer_class = serializers.ManageActorSerializer
|
2019-01-11 11:03:06 +00:00
|
|
|
filterset_class = filters.ManageActorFilterSet
|
2019-01-03 10:47:29 +00:00
|
|
|
permission_classes = (HasUserPermission,)
|
|
|
|
required_permissions = ["moderation"]
|
|
|
|
ordering_fields = [
|
|
|
|
"name",
|
|
|
|
"preferred_username",
|
|
|
|
"domain",
|
|
|
|
"fid",
|
|
|
|
"creation_date",
|
|
|
|
"last_fetch_date",
|
|
|
|
"uploads_count",
|
|
|
|
"outbox_activities_count",
|
2019-01-09 16:50:15 +00:00
|
|
|
"instance_policy",
|
2019-01-03 10:47:29 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
def get_object(self):
|
|
|
|
queryset = self.filter_queryset(self.get_queryset())
|
|
|
|
username, domain = self.kwargs["pk"].split("@")
|
|
|
|
filter_kwargs = {"domain_id": domain, "preferred_username": username}
|
|
|
|
obj = get_object_or_404(queryset, **filter_kwargs)
|
|
|
|
self.check_object_permissions(self.request, obj)
|
|
|
|
|
|
|
|
return obj
|
|
|
|
|
|
|
|
@detail_route(methods=["get"])
|
|
|
|
def stats(self, request, *args, **kwargs):
|
|
|
|
domain = self.get_object()
|
|
|
|
return response.Response(domain.get_stats(), status=200)
|
2019-01-07 08:45:53 +00:00
|
|
|
|
2019-01-09 13:18:32 +00:00
|
|
|
action = decorators.action_route(serializers.ManageActorActionSerializer)
|
|
|
|
|
2019-01-07 08:45:53 +00:00
|
|
|
|
|
|
|
class ManageInstancePolicyViewSet(
|
|
|
|
mixins.ListModelMixin,
|
|
|
|
mixins.RetrieveModelMixin,
|
|
|
|
mixins.DestroyModelMixin,
|
|
|
|
mixins.CreateModelMixin,
|
|
|
|
mixins.UpdateModelMixin,
|
|
|
|
viewsets.GenericViewSet,
|
|
|
|
):
|
|
|
|
queryset = (
|
|
|
|
moderation_models.InstancePolicy.objects.all()
|
|
|
|
.order_by("-creation_date")
|
|
|
|
.select_related()
|
|
|
|
)
|
|
|
|
serializer_class = serializers.ManageInstancePolicySerializer
|
2019-01-11 11:03:06 +00:00
|
|
|
filterset_class = filters.ManageInstancePolicyFilterSet
|
2019-01-07 08:45:53 +00:00
|
|
|
permission_classes = (HasUserPermission,)
|
|
|
|
required_permissions = ["moderation"]
|
|
|
|
ordering_fields = ["id", "creation_date"]
|
|
|
|
|
|
|
|
def perform_create(self, serializer):
|
|
|
|
serializer.save(actor=self.request.user.actor)
|