SHARE
TWEET

Untitled

a guest Aug 18th, 2018 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ## Serializer ##
  2. class AdminLoginSerializer(serializers.Serializer):
  3.     username = serializers.CharField()
  4.     password = serializers.CharField()
  5.  
  6.     def validate(self, data):
  7.         username = data.get("username", "")
  8.         password = data.get("password", "")
  9.  
  10.         if username and password:
  11.             user = authenticate(username=username, password=password)
  12.             if user:
  13.                 if user.is_active:
  14.                     data["user"] = user
  15.                 else:
  16.                     msg = 'User is deactivated'
  17.                     raise exceptions.ValidationError(msg)
  18.             else:
  19.                 msg = "Unable to login with given credentials"
  20.                 raise exceptions.ValidationError(msg)
  21.         else:
  22.             msg = 'Must Provide Username and password'
  23.             raise exceptions.ValidationError(msg)
  24.         return data
  25.  
  26. ## Viewsets ##
  27.  
  28. class AdminLoginView(APIView):
  29.     def post(self, request):
  30.         serializer = AdminLoginSerializer(data=request.data)
  31.         serializer.is_valid(raise_exception=True)
  32.         user = serializer.validated_data['user']
  33.         # django_login(request, user)
  34.         token, created = Token.objects.get_or_create(user=user)
  35.         return Response({"token": token.key, 'id': token.user.id}, status=200)
  36.  
  37.  
  38. class AdminLogoutView(APIView):
  39.     authentication_classes = [TokenAuthentication]
  40.  
  41.     def post(self, request):
  42.         # django_logout(request)
  43.         **request.user.auth_token.delete()**
  44.         return Response(status=204)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top