settings: avatar & username (& little stuff too)
This commit is contained in:
@ -5,17 +5,18 @@ from django.contrib.auth import logout
|
||||
from django.http import HttpRequest
|
||||
from rest_framework.authentication import SessionAuthentication
|
||||
|
||||
|
||||
class DeleteView(APIView):
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def delete(self, request: HttpRequest):
|
||||
data: dict = request.data
|
||||
|
||||
password: str = data["password"]
|
||||
if (password is None):
|
||||
return Response({"password": ["This field may not be blank."]})
|
||||
if (request.user.check_password(password) == False):
|
||||
return Response({"password": ["Password wrong."]})
|
||||
if (request.user.check_password(password) is False):
|
||||
return Response({"password": ["Password incorrect."]},
|
||||
status.HTTP_401_UNAUTHORIZED)
|
||||
request.user.delete()
|
||||
logout(request)
|
||||
return Response("user deleted", status=status.HTTP_200_OK)
|
||||
return Response(status=status.HTTP_200_OK)
|
||||
|
@ -1,45 +0,0 @@
|
||||
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.contrib.auth.models import User
|
||||
import re
|
||||
|
||||
class EditView(APIView):
|
||||
|
||||
permission_classes = (permissions.IsAuthenticated,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def get(self, request: HttpRequest):
|
||||
return Response({"username": request.user.username, "id": request.user.pk})
|
||||
|
||||
def patch(self, request: HttpRequest):
|
||||
data: dict = request.data
|
||||
|
||||
current_password: str = data.get("current_password")
|
||||
if (current_password is None):
|
||||
return Response({"current_password": ["This field may not be blank."]})
|
||||
|
||||
user_object = request.user
|
||||
|
||||
if (user_object.check_password(current_password) == False):
|
||||
return Response({"current_password": ["Password is wrong."]})
|
||||
|
||||
new_username = data.get("username", user_object.username)
|
||||
if (new_username != user_object.username):
|
||||
if (User.objects.filter(username=new_username).exists()):
|
||||
return Response({"username": ["A user with that username already exists."]})
|
||||
if (set(new_username) == {' '}):
|
||||
return Response({"username": ["This field may not be blank."]})
|
||||
if (re.search('^([a-z]||\@||\+||\-||\_)+$', new_username) is None):
|
||||
return Response({"username":["Enter a valid username. This value may contain only letters, numbers, and @/./+/-/_ characters."]})
|
||||
|
||||
new_password: str = data.get("password")
|
||||
if (new_password is not None):
|
||||
user_object.set_password(new_password)
|
||||
|
||||
user_object.save()
|
||||
|
||||
return Response("data has been alterate")
|
@ -2,15 +2,13 @@ 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 ..serializers.login import LoginSerializer
|
||||
|
||||
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))
|
||||
return Response(status=status.HTTP_200_OK if request.user.is_authenticated else status.HTTP_400_BAD_REQUEST)
|
||||
|
@ -8,8 +8,9 @@ from django.utils.translation import gettext as _
|
||||
|
||||
from ..serializers.login import LoginSerializer
|
||||
|
||||
|
||||
class LoginView(APIView):
|
||||
|
||||
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
|
@ -5,9 +5,11 @@ 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 unlogged", status=status.HTTP_200_OK)
|
||||
return Response("user logged out", status.HTTP_200_OK)
|
||||
|
@ -5,8 +5,10 @@ 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)
|
||||
|
14
accounts/views/update_profile.py
Normal file
14
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