<commit_before>
from rest_framework.views import APIView
from rest_framework import status
from rest_framework import parsers
from rest_framework import renderers
from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from django.contrib.auth import authenticate
from rest_framework import serializers
import datetime

class AuthTokenSerializer(serializers.Serializer):
    def __init__(self, *args, **kwargs):
        super(AuthTokenSerializer, self).__init__(*args, **kwargs)
        self.user = None

    username = serializers.CharField()
    password = serializers.CharField()

    def validate(self, attrs):
        username = attrs.get('username')
        password = attrs.get('password')

        if username and password:
            self.user = authenticate(username=username, password=password)

            if self.user:
                if not self.user.is_active:
                    raise serializers.ValidationError('User account is disabled.')
                return attrs
            else:
                raise serializers.ValidationError('Unable to login with provided credentials.')
        else:
            raise serializers.ValidationError('Must include "username" and "password"')



class ObtainAuthToken(APIView):
    throttle_classes = ()
    permission_classes = ()
    parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
    renderer_classes = (renderers.JSONRenderer,)
    serializer_class = AuthTokenSerializer
    model = Token

    def post(self, request):
        serializer = self.serializer_class(data=request.data)

        if not request.user.is_anonymous():
            user = request.user
        elif serializer.is_valid():
            user = serializer.user
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


        token, created = Token.objects.get_or_create(user=user)
        if not created:
            token.created = datetime.datetime.now()
            token.save()
        return Response({'token': token.key})


obtain_auth_token = ObtainAuthToken.as_view()
<commit_msg>Add amcat_version to get_token response<commit_after>
from rest_framework.views import APIView
from rest_framework import status
from rest_framework import parsers
from rest_framework import renderers
from rest_framework.response import Response
from rest_framework.authtoken.models import Token
from django.contrib.auth import authenticate
from rest_framework import serializers
import datetime

from amcat._version import __version__

class AuthTokenSerializer(serializers.Serializer):
    def __init__(self, *args, **kwargs):
        super(AuthTokenSerializer, self).__init__(*args, **kwargs)
        self.user = None

    username = serializers.CharField()
    password = serializers.CharField()

    def validate(self, attrs):
        username = attrs.get('username')
        password = attrs.get('password')

        if username and password:
            self.user = authenticate(username=username, password=password)

            if self.user:
                if not self.user.is_active:
                    raise serializers.ValidationError('User account is disabled.')
                return attrs
            else:
                raise serializers.ValidationError('Unable to login with provided credentials.')
        else:
            raise serializers.ValidationError('Must include "username" and "password"')



class ObtainAuthToken(APIView):
    throttle_classes = ()
    permission_classes = ()
    parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
    renderer_classes = (renderers.JSONRenderer,)
    serializer_class = AuthTokenSerializer
    model = Token

    def post(self, request):
        serializer = self.serializer_class(data=request.data)

        if not request.user.is_anonymous():
            user = request.user
        elif serializer.is_valid():
            user = serializer.user
        else:
            return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)


        token, created = Token.objects.get_or_create(user=user)
        if not created:
            token.created = datetime.datetime.now()
            token.save()
        return Response({'token': token.key, 'version':__version__})


obtain_auth_token = ObtainAuthToken.as_view()

{'fraction_non_alphanumeric': 0.08209467184998856, 'fraction_numerical': 0.001372055796935742, 'mean_word_length': 3.020220588235294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2058088', 'n_tokens_mistral': 1154, 'n_tokens_neox': 1119, 'n_words': 283}