<s> import warnings <EOL> from django . db . utils import IntegrityError <EOL> from django . db import transaction <EOL> from django . conf import settings <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import BaseUserManager <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . contrib . auth . backends import ModelBackend <EOL> from django . utils import timezone <EOL> from google . appengine . api import users <EOL> from djangae . contrib . gauth . common . models import GaeAbstractBaseUser <EOL> if hasattr ( settings , "<STR_LIT>" ) : <EOL> warnings . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> warnings . warn ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> ) <EOL> def should_create_unknown_user ( ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> return settings . DJANGAE_CREATE_UNKNOWN_USER <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> return not settings . DJANGAE_FORCE_USER_PRE_CREATION <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> return settings . DJANGAE_ALLOW_USER_PRE_CREATION <EOL> if hasattr ( settings , '<STR_LIT>' ) : <EOL> return settings . ALLOW_USER_PRE_CREATION <EOL> return False <EOL> class BaseAppEngineUserAPIBackend ( ModelBackend ) : <EOL> atomic = transaction . atomic <EOL> atomic_kwargs = { } <EOL> def authenticate ( self , google_user = None ) : <EOL> """<STR_LIT>""" <EOL> if google_user is None : <EOL> return None <EOL> User = get_user_model ( ) <EOL> if not issubclass ( User , GaeAbstractBaseUser ) : <EOL> raise ImproperlyConfigured ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> ) <EOL> user_id = google_user . user_id ( ) <EOL> email = BaseUserManager . normalize_email ( google_user . email ( ) ) <EOL> try : <EOL> return User . objects . get ( username = user_id ) <EOL> except User . DoesNotExist : <EOL> pass <EOL> auto_create = should_create_unknown_user ( ) <EOL> user_is_admin = users . is_current_user_admin ( ) <EOL> if not ( auto_create or user_is_admin ) : <EOL> return None <EOL> try : <EOL> existing_user = User . objects . get ( email = email ) <EOL> except User . DoesNotExist : <EOL> existing_user = None <EOL> if existing_user : <EOL> if existing_user . username is None : <EOL> existing_user . username = user_id <EOL> existing_user . email = email <EOL> existing_user . last_login = timezone . now ( ) <EOL> existing_user . save ( ) <EOL> return existing_user <EOL> else : <EOL> with self . atomic ( ** self . atomic_kwargs ) : <EOL> existing_user = User . objects . get ( pk = existing_user . pk ) <EOL> existing_user . email = None <EOL> existing_user . save ( ) <EOL> return User . objects . create_user ( user_id , email = email ) <EOL> else : <EOL> try : <EOL> return User . objects . create_user ( user_id , email = email ) <EOL> except IntegrityError : <EOL> return User . objects . get ( username = user_id ) </s>
