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-11-13 03:16:53 +04:00
|
|
|
class AuthTokenView(APIView):
|
|
|
|
throttle_classes = ()
|
|
|
|
permission_classes = ()
|
|
|
|
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
|
|
|
|
renderer_classes = (renderers.JSONRenderer,)
|
2012-11-11 04:17:50 +04:00
|
|
|
model = Token
|
|
|
|
|
|
|
|
def post(self, request):
|
2012-11-13 03:16:53 +04:00
|
|
|
serializer = AuthTokenSerializer(data=request.DATA)
|
|
|
|
if serializer.is_valid():
|
|
|
|
token, created = Token.objects.get_or_create(user=serializer.object['user'])
|
|
|
|
return Response({'token': token.key})
|
|
|
|
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
|
|
|
|