funkwhale/api/funkwhale_api/users/views.py

100 wiersze
3.8 KiB
Python
Czysty Zwykły widok Historia

from allauth.account.adapter import get_adapter
from rest_auth import views as rest_auth_views
from rest_auth.registration import views as registration_views
2018-06-10 08:55:16 +00:00
from rest_framework import mixins, viewsets
2019-01-11 12:33:35 +00:00
from rest_framework.decorators import action
2018-06-10 08:55:16 +00:00
from rest_framework.response import Response
from funkwhale_api.common import preferences
from . import models, serializers, tasks
class RegisterView(registration_views.RegisterView):
serializer_class = serializers.RegisterSerializer
permission_classes = []
action = "signup"
throttling_scopes = {"signup": {"authenticated": "signup", "anonymous": "signup"}}
def create(self, request, *args, **kwargs):
invitation_code = request.data.get("invitation")
if not invitation_code and not self.is_open_for_signup(request):
2018-06-09 13:36:16 +00:00
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)
class VerifyEmailView(registration_views.VerifyEmailView):
action = "verify-email"
class PasswordChangeView(rest_auth_views.PasswordChangeView):
action = "password-change"
class PasswordResetView(rest_auth_views.PasswordResetView):
action = "password-reset"
class PasswordResetConfirmView(rest_auth_views.PasswordResetConfirmView):
action = "password-reset-confirm"
2018-06-09 13:36:16 +00:00
class UserViewSet(mixins.UpdateModelMixin, viewsets.GenericViewSet):
queryset = models.User.objects.all()
2018-03-03 10:20:21 +00:00
serializer_class = serializers.UserWriteSerializer
2018-06-09 13:36:16 +00:00
lookup_field = "username"
lookup_value_regex = r"[a-zA-Z0-9-_.]+"
required_scope = "profile"
@action(methods=["get", "delete"], detail=False)
def me(self, request, *args, **kwargs):
"""Return information about the current user or delete it"""
if request.method.lower() == "delete":
serializer = serializers.UserDeleteSerializer(
request.user, data=request.data
)
serializer.is_valid(raise_exception=True)
tasks.delete_account.delay(user_id=request.user.pk)
# at this point, password is valid, we launch deletion
return Response(status=204)
serializer = serializers.MeSerializer(request.user)
return Response(serializer.data)
2018-03-03 10:20:21 +00:00
@action(
methods=["get", "post", "delete"],
required_scope="security",
url_path="subsonic-token",
detail=True,
)
def subsonic_token(self, request, *args, **kwargs):
2018-06-09 13:36:16 +00:00
if not self.request.user.username == kwargs.get("username"):
return Response(status=403)
2018-06-09 13:36:16 +00:00
if not preferences.get("subsonic__enabled"):
return Response(status=405)
2018-06-09 13:36:16 +00:00
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
2018-06-09 13:36:16 +00:00
self.request.user.save(update_fields=["subsonic_api_token"])
return Response(status=204)
self.request.user.update_subsonic_api_token()
2018-06-09 13:36:16 +00:00
self.request.user.save(update_fields=["subsonic_api_token"])
data = {"subsonic_api_token": self.request.user.subsonic_api_token}
return Response(data)
2018-03-03 10:20:21 +00:00
def update(self, request, *args, **kwargs):
2018-06-09 13:36:16 +00:00
if not self.request.user.username == kwargs.get("username"):
2018-03-03 10:20:21 +00:00
return Response(status=403)
return super().update(request, *args, **kwargs)
def partial_update(self, request, *args, **kwargs):
2018-06-09 13:36:16 +00:00
if not self.request.user.username == kwargs.get("username"):
2018-03-03 10:20:21 +00:00
return Response(status=403)
return super().partial_update(request, *args, **kwargs)