<commit_before>from django.contrib.auth import get_user_model, authenticate
from rest_framework import serializers

from oscarapi.utils import overridable


User = get_user_model()

def field_length(fieldname):
    field = next(field for field in User._meta.fields if field.name == fieldname)
    return field.max_length

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = overridable('OSCARAPI_USER_FIELDS', (
            'username', 'id', 'date_joined',))


class LoginSerializer(serializers.Serializer):

    username = serializers.CharField(max_length=field_length('username'), required=True)
    password = serializers.CharField(max_length=field_length('password'), required=True)

    def validate(self, attrs):
        user = authenticate(username=attrs['username'],
                            password=attrs['password'])
        if user is None:
            raise serializers.ValidationError('invalid login')
        elif not user.is_active:
            raise serializers.ValidationError(
                'Can not log in as inactive user')
        elif user.is_staff and overridable('OSCARAPI_BLOCK_ADMIN_API_ACCESS', True):
            raise serializers.ValidationError(
                'Staff users can not log in via the rest api')

        return attrs

    def restore_object(self, attrs, instance=None):
        return authenticate(username=attrs['username'],
                            password=attrs['password'])<commit_msg>The .object property is deprecated and replaced by update and create methods. We don't want that here so we created a custom method<commit_after>from django.contrib.auth import get_user_model, authenticate
from rest_framework import serializers

from oscarapi.utils import overridable


User = get_user_model()

def field_length(fieldname):
    field = next(field for field in User._meta.fields if field.name == fieldname)
    return field.max_length

class UserSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = overridable('OSCARAPI_USER_FIELDS', (
            'username', 'id', 'date_joined',))


class LoginSerializer(serializers.Serializer):

    username = serializers.CharField(max_length=field_length('username'), required=True)
    password = serializers.CharField(max_length=field_length('password'), required=True)

    def validate(self, attrs):
        user = authenticate(username=attrs['username'],
                            password=attrs['password'])
        if user is None:
            raise serializers.ValidationError('invalid login')
        elif not user.is_active:
            raise serializers.ValidationError(
                'Can not log in as inactive user')
        elif user.is_staff and overridable('OSCARAPI_BLOCK_ADMIN_API_ACCESS', True):
            raise serializers.ValidationError(
                'Staff users can not log in via the rest api')

        return attrs

    def get_user(self):
        return authenticate(username=self.validated_data['username'],
                            password=self.validated_data['password'])
{'fraction_non_alphanumeric': 0.08617363344051447, 'fraction_numerical': 0.0, 'mean_word_length': 3.1646586345381524, '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': '7460920', 'n_tokens_mistral': 822, 'n_tokens_neox': 794, 'n_words': 215}