dockered
This commit is contained in:
23
django/accounts/views/delete.py
Normal file
23
django/accounts/views/delete.py
Normal file
@ -0,0 +1,23 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework import permissions, status
|
||||
from rest_framework.response import Response
|
||||
from django.contrib.auth import logout
|
||||
from django.http import HttpRequest
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
|
||||
class DeleteView(APIView):
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def delete(self, request: HttpRequest):
|
||||
data: dict = request.data
|
||||
|
||||
password: str = data["password"]
|
||||
if (request.user.check_password(password) is False):
|
||||
return Response({"password": _("Password incorrect.")},
|
||||
status.HTTP_401_UNAUTHORIZED)
|
||||
request.user.delete()
|
||||
logout(request)
|
||||
return Response(status=status.HTTP_200_OK)
|
14
django/accounts/views/logged.py
Normal file
14
django/accounts/views/logged.py
Normal file
@ -0,0 +1,14 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import permissions, status
|
||||
from django.http import HttpRequest
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
||||
|
||||
class LoggedView(APIView):
|
||||
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def get(self, request: HttpRequest):
|
||||
return Response(status=status.HTTP_200_OK if request.user.is_authenticated else status.HTTP_400_BAD_REQUEST)
|
25
django/accounts/views/login.py
Normal file
25
django/accounts/views/login.py
Normal file
@ -0,0 +1,25 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import permissions, status
|
||||
from django.http import HttpRequest
|
||||
from django.contrib.auth import login
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from ..serializers.login import LoginSerializer
|
||||
|
||||
|
||||
class LoginView(APIView):
|
||||
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def post(self, request: HttpRequest):
|
||||
data = request.data
|
||||
serializer = LoginSerializer(data=data)
|
||||
serializer.is_valid(raise_exception=True)
|
||||
user = serializer.get_user(data)
|
||||
if user is None:
|
||||
return Response({'login': [_('Invalid username or password.')]}, status.HTTP_401_UNAUTHORIZED)
|
||||
login(request, user)
|
||||
return Response({'id': user.pk}, status=status.HTTP_200_OK)
|
15
django/accounts/views/logout.py
Normal file
15
django/accounts/views/logout.py
Normal file
@ -0,0 +1,15 @@
|
||||
from rest_framework.views import APIView
|
||||
from django.contrib.auth import logout
|
||||
from rest_framework import permissions, status
|
||||
from rest_framework.response import Response
|
||||
from django.http import HttpRequest
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
||||
|
||||
class LogoutView(APIView):
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def get(self, request: HttpRequest):
|
||||
logout(request)
|
||||
return Response("user logged out", status.HTTP_200_OK)
|
20
django/accounts/views/register.py
Normal file
20
django/accounts/views/register.py
Normal file
@ -0,0 +1,20 @@
|
||||
from rest_framework import permissions, status
|
||||
from ..serializers.register import RegisterSerialiser
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from django.http import HttpRequest
|
||||
from django.contrib.auth import login
|
||||
|
||||
|
||||
class RegisterView(APIView):
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
|
||||
def post(self, request: HttpRequest):
|
||||
data = request.data
|
||||
serializer = RegisterSerialiser(data=data)
|
||||
if serializer.is_valid(raise_exception=True):
|
||||
user = serializer.create(data)
|
||||
if user:
|
||||
login(request, user)
|
||||
return Response("user created", status=status.HTTP_201_CREATED)
|
||||
return Response(status=status.HTTP_400_BAD_REQUEST)
|
13
django/accounts/views/update_password.py
Normal file
13
django/accounts/views/update_password.py
Normal file
@ -0,0 +1,13 @@
|
||||
from ..serializers.update_password import UpdatePasswordSerializer
|
||||
from rest_framework.generics import UpdateAPIView
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
||||
class UpdatePasswordView(UpdateAPIView):
|
||||
queryset = User.objects.all()
|
||||
permission_classes = (IsAuthenticated,)
|
||||
serializer_class = UpdatePasswordSerializer
|
||||
|
||||
def get_object(self):
|
||||
return self.queryset.get(pk=self.request.user.pk)
|
14
django/accounts/views/update_profile.py
Normal file
14
django/accounts/views/update_profile.py
Normal file
@ -0,0 +1,14 @@
|
||||
from ..serializers.update_user import UpdateUserSerializer
|
||||
from rest_framework.generics import UpdateAPIView
|
||||
from rest_framework.permissions import IsAuthenticated
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
|
||||
class UpdateProfileView(UpdateAPIView):
|
||||
|
||||
queryset = User.objects.all()
|
||||
permission_classes = (IsAuthenticated,)
|
||||
serializer_class = UpdateUserSerializer
|
||||
|
||||
def get_object(self):
|
||||
return self.queryset.get(pk=self.request.user.pk)
|
Reference in New Issue
Block a user