mirror of
https://github.com/encode/django-rest-framework.git
synced 2024-11-23 01:57:00 +03:00
24 lines
885 B
Python
24 lines
885 B
Python
from rest_framework import parsers, renderers
|
|
from rest_framework.authtoken.models import Token
|
|
from rest_framework.authtoken.serializers import AuthTokenSerializer
|
|
from rest_framework.response import Response
|
|
from rest_framework.views import APIView
|
|
|
|
|
|
class ObtainAuthToken(APIView):
|
|
throttle_classes = ()
|
|
permission_classes = ()
|
|
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
|
|
renderer_classes = (renderers.JSONRenderer,)
|
|
serializer_class = AuthTokenSerializer
|
|
|
|
def post(self, request, *args, **kwargs):
|
|
serializer = self.serializer_class(data=request.data)
|
|
serializer.is_valid(raise_exception=True)
|
|
user = serializer.validated_data['user']
|
|
token, created = Token.objects.get_or_create(user=user)
|
|
return Response({'token': token.key})
|
|
|
|
|
|
obtain_auth_token = ObtainAuthToken.as_view()
|