2012-11-11 04:17:50 +04:00
|
|
|
from rest_framework.views import APIView
|
2012-11-13 03:16:53 +04:00
|
|
|
from rest_framework import status
|
|
|
|
from rest_framework import parsers
|
|
|
|
from rest_framework import renderers
|
|
|
|
from rest_framework.response import Response
|
2012-11-11 04:17:50 +04:00
|
|
|
from rest_framework.authtoken.models import Token
|
|
|
|
from rest_framework.authtoken.serializers import AuthTokenSerializer
|
|
|
|
|
2012-12-08 02:25:16 +04:00
|
|
|
|
2012-11-14 04:49:13 +04:00
|
|
|
class ObtainAuthToken(APIView):
|
2012-11-13 03:16:53 +04:00
|
|
|
throttle_classes = ()
|
|
|
|
permission_classes = ()
|
|
|
|
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
|
2012-12-08 02:25:16 +04:00
|
|
|
renderer_classes = (renderers.JSONRenderer,)
|
2013-01-08 16:20:01 +04:00
|
|
|
serializer_class = AuthTokenSerializer
|
2012-11-11 04:17:50 +04:00
|
|
|
model = Token
|
|
|
|
|
|
|
|
def post(self, request):
|
2014-09-26 13:46:52 +04:00
|
|
|
serializer = self.serializer_class(data=request.data)
|
2012-11-13 03:16:53 +04:00
|
|
|
if serializer.is_valid():
|
2014-09-02 20:41:23 +04:00
|
|
|
user = serializer.validated_data['user']
|
|
|
|
token, created = Token.objects.get_or_create(user=user)
|
2012-11-13 03:16:53 +04:00
|
|
|
return Response({'token': token.key})
|
2012-12-08 02:25:16 +04:00
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
2012-11-13 03:16:53 +04:00
|
|
|
|
2012-11-14 04:49:13 +04:00
|
|
|
|
|
|
|
obtain_auth_token = ObtainAuthToken.as_view()
|