<s> from django . contrib . auth import get_user_model <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from rest_framework import serializers , exceptions <EOL> from . models import TimedAuthToken <EOL> class TimedAuthTokenCreateSerializer ( serializers . Serializer ) : <EOL> username = serializers . CharField ( ) <EOL> password = serializers . CharField ( ) <EOL> def validate_username ( self , username ) : <EOL> try : <EOL> User = get_user_model ( ) <EOL> self . user = User . objects . get ( ** { User . USERNAME_FIELD : username } ) <EOL> except ObjectDoesNotExist : <EOL> msg = _ ( '<STR_LIT>' . format ( User . USERNAME_FIELD ) ) <EOL> raise serializers . ValidationError ( msg ) <EOL> def validate ( self , data ) : <EOL> password = data [ '<STR_LIT:password>' ] <EOL> if not self . user . check_password ( password ) : <EOL> raise serializers . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> if not self . user . is_active : <EOL> raise exceptions . AuthenticationFailed ( _ ( '<STR_LIT>' ) ) <EOL> return data <EOL> def create ( self , validated_data ) : <EOL> return TimedAuthToken . objects . create ( user = self . user ) <EOL> def to_representation ( self , token ) : <EOL> return TimedAuthTokenReadSerializer ( token ) . data <EOL> def update ( self , instance , validated_data ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> class TimedAuthTokenReadSerializer ( serializers . ModelSerializer ) : <EOL> class Meta : <EOL> model = TimedAuthToken <EOL> fields = '<STR_LIT>' , <EOL> token = serializers . CharField ( source = '<STR_LIT:key>' ) </s>
