views.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. from rest_framework.views import APIView
  2. from rest_framework.generics import GenericAPIView
  3. from rest_framework.response import Response
  4. from rest_framework import status
  5. from django.contrib.auth import authenticate
  6. from utils.JWTAuthentication_diy import get_token, MyJWTAuthentication
  7. from utils.permissions import RegisterViewPermission
  8. from utils.MyRateThrottle import LoginRateThrottle
  9. from .serializers import RegisterViewSerializer
  10. # Create your views here.
  11. class LoginView(APIView):
  12. throttle_classes = [LoginRateThrottle,]
  13. def post(self,request):
  14. username = request.data.get('username')
  15. password = request.data.get('password')
  16. user = authenticate(username = username,password = password)
  17. if user is not None and user.is_active:
  18. token = get_token(user)
  19. return Response(token)
  20. else:
  21. return Response(data = {"msg":"登录验证失败","data":""},status=status.HTTP_401_UNAUTHORIZED)
  22. class RegisterView(GenericAPIView):
  23. authentication_classes = [MyJWTAuthentication]
  24. serializer_class = RegisterViewSerializer
  25. permission_classes = [RegisterViewPermission]
  26. def post(self,request):
  27. serializer = self.get_serializer(data=request.data)
  28. serializer.is_valid()
  29. serializer.save()
  30. return Response(serializer.data)
  31. # class UserShowView(APIView):
  32. # #局部配置
  33. # authentication_classes = [MyJWTAuthentication]
  34. # def post(self,request):
  35. # token = request.auth
  36. # user = request.user
  37. # print(token)
  38. # print(user)
  39. # # print(user["user_modules"])
  40. # return Response("认证成功")