<s> from __future__ import unicode_literals <EOL> from datetime import timedelta <EOL> from uuid import uuid4 <EOL> from django . db import models <EOL> from django . contrib . auth import authenticate , get_user_model <EOL> from django . conf import settings <EOL> from django . core . urlresolvers import reverse <EOL> from django . utils import timezone <EOL> now = timezone . now <EOL> def default_valid_date ( ) : <EOL> return now ( ) + timedelta ( settings . ACCOUNT_ACTIVATION_DAYS ) <EOL> class ExternalUserData ( models . Model ) : <EOL> user = models . ForeignKey ( <EOL> settings . AUTH_USER_MODEL , related_name = '<STR_LIT>' ) <EOL> service = models . CharField ( db_index = True , max_length = <NUM_LIT:255> ) <EOL> username = models . CharField ( db_index = True , max_length = <NUM_LIT:255> ) <EOL> class Meta : <EOL> unique_together = [ [ '<STR_LIT>' , '<STR_LIT:username>' ] ] <EOL> class UniqueTokenManager ( models . Manager ) : <EOL> def __init__ ( self , token_field ) : <EOL> self . token_field = token_field <EOL> super ( UniqueTokenManager , self ) . __init__ ( ) <EOL> def create ( self , ** kwargs ) : <EOL> assert self . token_field not in kwargs , '<STR_LIT>' <EOL> kwargs [ self . token_field ] = str ( uuid4 ( ) ) <EOL> return super ( UniqueTokenManager , self ) . create ( ** kwargs ) <EOL> class AbstractToken ( models . Model ) : <EOL> token = models . CharField ( max_length = <NUM_LIT> , unique = True ) <EOL> valid_until = models . DateTimeField ( default = default_valid_date ) <EOL> objects = UniqueTokenManager ( token_field = '<STR_LIT>' ) <EOL> class Meta : <EOL> abstract = True <EOL> class EmailConfirmationRequest ( AbstractToken ) : <EOL> email = models . EmailField ( ) <EOL> def get_authenticated_user ( self ) : <EOL> user , dummy_created = get_user_model ( ) . objects . get_or_create ( <EOL> email = self . email , defaults = { '<STR_LIT>' : True } ) <EOL> if not user . is_active : <EOL> return <EOL> return authenticate ( user = user ) <EOL> def get_confirmation_url ( self ) : <EOL> return reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : self . token } ) <EOL> class EmailChangeRequest ( AbstractToken ) : <EOL> user = models . ForeignKey ( <EOL> settings . AUTH_USER_MODEL , related_name = '<STR_LIT>' ) <EOL> email = models . EmailField ( ) <EOL> def get_confirmation_url ( self ) : <EOL> return reverse ( '<STR_LIT>' , <EOL> kwargs = { '<STR_LIT>' : self . token } ) </s>
