Mosqkiller-API/apps/accounts/api/mobile/views.py

75 lines
2.6 KiB
Python

from django.contrib.auth import get_user_model
from django.db.models import Q
from rest_framework import serializers
from rest_framework.response import Response
from rest_framework.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_400_BAD_REQUEST
from rest_framework.views import APIView
from rest_framework.generics import RetrieveAPIView
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework_jwt.serializers import VerifyJSONWebTokenSerializer
from rest_framework.views import exception_handler
from .serializers import UserLoginSerializer, UserDetailSerializer
from django.contrib.auth.backends import ModelBackend
User = get_user_model()
class CustomBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user = User.objects.get(Q(username=username)|Q(email=username))
if user.check_password(password):
return user
except Exception as e:
return None
class UserLoginAPIView(APIView):
permission_classes = [AllowAny]
serializer_class = UserLoginSerializer
def post(self, request, *args, **kwargs):
data = request.data
serializer = UserLoginSerializer(data=data)
if serializer.is_valid(raise_exception=True):
login_data = serializer.data
response_data = {
"data": {
"username": login_data['username'],
"token": login_data['token']
},
"code": 0,
"message": "success"
}
return Response(response_data, HTTP_200_OK)
else:
return Response(serializer.errors, HTTP_400_BAD_REQUEST)
class UserLogoutAPIView(APIView):
permission_classes = [IsAuthenticated]
serializer_class = UserLoginSerializer
def get(self, request, *args, **kwargs):
logout_data = {'msg': 'logout successfully'}
return Response(logout_data, HTTP_200_OK)
class UserDetailAPIView(RetrieveAPIView):
serializer_class = UserDetailSerializer
permission_classes = [AllowAny]
queryset = User.objects.all()
def get(self, request, *args, **kwargs):
token = request.GET.get('token', None)
data = {'token': token}
try:
valid_data = VerifyJSONWebTokenSerializer().validate(data)
user = valid_data['user']
serializer = self.get_serializer(user)
return Response(serializer.data, HTTP_200_OK)
except serializers.ValidationError as exc:
exc.detail = exc.detail[0]
response = exception_handler(exc, context=None)
return response