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 utils.MyRateThrottle import LoginRateThrottle from .serializers import RegisterViewSerializer # Create your views here. class LoginView(APIView): throttle_classes = [LoginRateThrottle, ] 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) # class UserShowView(APIView): # #局部配置 # authentication_classes = [MyJWTAuthentication] # def post(self,request): # token = request.auth # user = request.user # print(token) # print(user) # # print(user["user_modules"]) # return Response("认证成功")