from rest_framework.views import APIView from rest_framework.generics import GenericAPIView from rest_framework.response import Response from rest_framework import status from django.contrib.auth import authenticate from utils.JWTAuthentication_diy import get_token, MyJWTAuthentication from utils.permissions import RegisterViewPermission from .serializers import RegisterViewSerializer # Create your views here. class LoginView(APIView): def post(self, request): username = request.data.get('username') password = request.data.get('password') user = authenticate(username=username, password=password) if user is not None and user.is_active: token = get_token(user) return Response(token) else: return Response(data={"msg": "登录验证失败", "data": ""}, status=status.HTTP_401_UNAUTHORIZED) class RegisterView(GenericAPIView): authentication_classes = [MyJWTAuthentication] serializer_class = RegisterViewSerializer permission_classes = [RegisterViewPermission] def post(self, request): serializer = self.get_serializer(data=request.data) serializer.is_valid() serializer.save() return Response(serializer.data)