Skip to content
Snippets Groups Projects
views.py 2.81 KiB
Newer Older
  • Learn to ignore specific revisions
  • from allauth.account.adapter import get_adapter
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    from rest_auth.registration.views import RegisterView as BaseRegisterView
    from rest_framework import mixins, viewsets
    
    from rest_framework.decorators import action
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    from rest_framework.response import Response
    
    from funkwhale_api.common import preferences
    
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    from . import models, serializers
    
        serializer_class = serializers.RegisterSerializer
    
        permission_classes = []
    
        def create(self, request, *args, **kwargs):
    
            invitation_code = request.data.get("invitation")
            if not invitation_code and not self.is_open_for_signup(request):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                r = {"detail": "Registration has been disabled"}
    
                return Response(r, status=403)
            return super().create(request, *args, **kwargs)
    
        def is_open_for_signup(self, request):
            return get_adapter().is_open_for_signup(request)
    
    
    
    Eliot Berriot's avatar
    Eliot Berriot committed
    class UserViewSet(mixins.UpdateModelMixin, viewsets.GenericViewSet):
    
        serializer_class = serializers.UserWriteSerializer
    
    Eliot Berriot's avatar
    Eliot Berriot committed
        lookup_field = "username"
    
        required_scope = "profile"
    
        @action(methods=["get"], detail=False)
    
        def me(self, request, *args, **kwargs):
            """Return information about the current user"""
    
            serializer = serializers.MeSerializer(request.user)
    
        @action(
            methods=["get", "post", "delete"],
            required_scope="security",
            url_path="subsonic-token",
            detail=True,
        )
    
        def subsonic_token(self, request, *args, **kwargs):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            if not self.request.user.username == kwargs.get("username"):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            if not preferences.get("subsonic__enabled"):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            if request.method.lower() == "get":
                return Response(
                    {"subsonic_api_token": self.request.user.subsonic_api_token}
                )
            if request.method.lower() == "delete":
    
                self.request.user.subsonic_api_token = None
    
    Eliot Berriot's avatar
    Eliot Berriot committed
                self.request.user.save(update_fields=["subsonic_api_token"])
    
                return Response(status=204)
            self.request.user.update_subsonic_api_token()
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            self.request.user.save(update_fields=["subsonic_api_token"])
            data = {"subsonic_api_token": self.request.user.subsonic_api_token}
    
        def update(self, request, *args, **kwargs):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            if not self.request.user.username == kwargs.get("username"):
    
                return Response(status=403)
            return super().update(request, *args, **kwargs)
    
        def partial_update(self, request, *args, **kwargs):
    
    Eliot Berriot's avatar
    Eliot Berriot committed
            if not self.request.user.username == kwargs.get("username"):
    
                return Response(status=403)
            return super().partial_update(request, *args, **kwargs)