<s> from __future__ import unicode_literals <EOL> import re <EOL> import warnings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . core . mail import send_mail <EOL> from django . core import validators <EOL> from django . db import models <EOL> from django . db . models . manager import EmptyManager <EOL> from django . utils . crypto import get_random_string <EOL> from django . utils . http import urlquote <EOL> from django . utils import six <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . utils import timezone <EOL> from django . contrib import auth <EOL> from django . contrib . auth . hashers import ( <EOL> check_password , make_password , is_password_usable , UNUSABLE_PASSWORD ) <EOL> from django . contrib . auth . signals import user_logged_in <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . utils . encoding import python_2_unicode_compatible <EOL> def update_last_login ( sender , user , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> user . last_login = timezone . now ( ) <EOL> user . save ( update_fields = [ '<STR_LIT>' ] ) <EOL> user_logged_in . connect ( update_last_login ) <EOL> class SiteProfileNotAvailable ( Exception ) : <EOL> pass <EOL> class PermissionManager ( models . Manager ) : <EOL> def get_by_natural_key ( self , codename , app_label , model ) : <EOL> return self . get ( <EOL> codename = codename , <EOL> content_type = ContentType . objects . get_by_natural_key ( app_label , <EOL> model ) , <EOL> ) <EOL> @ python_2_unicode_compatible <EOL> class Permission ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( _ ( '<STR_LIT:name>' ) , max_length = <NUM_LIT:50> ) <EOL> content_type = models . ForeignKey ( ContentType ) <EOL> codename = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:100> ) <EOL> objects = PermissionManager ( ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> unique_together = ( ( '<STR_LIT>' , '<STR_LIT>' ) , ) <EOL> ordering = ( '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( <EOL> six . text_type ( self . content_type . app_label ) , <EOL> six . text_type ( self . content_type ) , <EOL> six . text_type ( self . name ) ) <EOL> def natural_key ( self ) : <EOL> return ( self . codename , ) + self . content_type . natural_key ( ) <EOL> natural_key . dependencies = [ '<STR_LIT>' ] <EOL> class GroupManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def get_by_natural_key ( self , name ) : <EOL> return self . get ( name = name ) <EOL> @ python_2_unicode_compatible <EOL> class Group ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> name = models . CharField ( _ ( '<STR_LIT:name>' ) , max_length = <NUM_LIT> , unique = True ) <EOL> permissions = models . ManyToManyField ( Permission , <EOL> verbose_name = _ ( '<STR_LIT>' ) , blank = True ) <EOL> objects = GroupManager ( ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> def natural_key ( self ) : <EOL> return ( self . name , ) <EOL> class BaseUserManager ( models . Manager ) : <EOL> @ classmethod <EOL> def normalize_email ( cls , email ) : <EOL> """<STR_LIT>""" <EOL> email = email or '<STR_LIT>' <EOL> try : <EOL> email_name , domain_part = email . strip ( ) . rsplit ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> email = '<STR_LIT:@>' . join ( [ email_name , domain_part . lower ( ) ] ) <EOL> return email <EOL> def make_random_password ( self , length = <NUM_LIT:10> , <EOL> allowed_chars = '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> return get_random_string ( length , allowed_chars ) <EOL> def get_by_natural_key ( self , username ) : <EOL> return self . get ( ** { self . model . USERNAME_FIELD : username } ) <EOL> class UserManager ( BaseUserManager ) : <EOL> def create_user ( self , username , email = None , password = None , ** extra_fields ) : <EOL> """<STR_LIT>""" <EOL> now = timezone . now ( ) <EOL> if not username : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> email = UserManager . normalize_email ( email ) <EOL> user = self . model ( username = username , email = email , <EOL> is_staff = False , is_active = True , is_superuser = False , <EOL> last_login = now , date_joined = now , ** extra_fields ) <EOL> user . set_password ( password ) <EOL> user . save ( using = self . _db ) <EOL> return user <EOL> def create_superuser ( self , username , email , password , ** extra_fields ) : <EOL> u = self . create_user ( username , email , password , ** extra_fields ) <EOL> u . is_staff = True <EOL> u . is_active = True <EOL> u . is_superuser = True <EOL> u . save ( using = self . _db ) <EOL> return u <EOL> @ python_2_unicode_compatible <EOL> class AbstractBaseUser ( models . Model ) : <EOL> password = models . CharField ( _ ( '<STR_LIT:password>' ) , max_length = <NUM_LIT> ) <EOL> last_login = models . DateTimeField ( _ ( '<STR_LIT>' ) , default = timezone . now ) <EOL> is_active = True <EOL> REQUIRED_FIELDS = [ ] <EOL> class Meta : <EOL> abstract = True <EOL> def get_username ( self ) : <EOL> "<STR_LIT>" <EOL> return getattr ( self , self . USERNAME_FIELD ) <EOL> def __str__ ( self ) : <EOL> return self . get_username ( ) <EOL> def natural_key ( self ) : <EOL> return ( self . get_username ( ) , ) <EOL> def is_anonymous ( self ) : <EOL> """<STR_LIT>""" <EOL> return False <EOL> def is_authenticated ( self ) : <EOL> """<STR_LIT>""" <EOL> return True <EOL> def set_password ( self , raw_password ) : <EOL> self . password = make_password ( raw_password ) <EOL> def check_password ( self , raw_password ) : <EOL> """<STR_LIT>""" <EOL> def setter ( raw_password ) : <EOL> self . set_password ( raw_password ) <EOL> self . save ( update_fields = [ "<STR_LIT:password>" ] ) <EOL> return check_password ( raw_password , self . password , setter ) <EOL> def set_unusable_password ( self ) : <EOL> self . password = make_password ( None ) <EOL> def has_usable_password ( self ) : <EOL> return is_password_usable ( self . password ) <EOL> def get_full_name ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def get_short_name ( self ) : <EOL> raise NotImplementedError ( ) <EOL> def _user_get_all_permissions ( user , obj ) : <EOL> permissions = set ( ) <EOL> for backend in auth . get_backends ( ) : <EOL> if hasattr ( backend , "<STR_LIT>" ) : <EOL> if obj is not None : <EOL> permissions . update ( backend . get_all_permissions ( user , obj ) ) <EOL> else : <EOL> permissions . update ( backend . get_all_permissions ( user ) ) <EOL> return permissions <EOL> def _user_has_perm ( user , perm , obj ) : <EOL> for backend in auth . get_backends ( ) : <EOL> if hasattr ( backend , "<STR_LIT>" ) : <EOL> if obj is not None : <EOL> if backend . has_perm ( user , perm , obj ) : <EOL> return True <EOL> else : <EOL> if backend . has_perm ( user , perm ) : <EOL> return True <EOL> return False <EOL> def _user_has_module_perms ( user , app_label ) : <EOL> for backend in auth . get_backends ( ) : <EOL> if hasattr ( backend , "<STR_LIT>" ) : <EOL> if backend . has_module_perms ( user , app_label ) : <EOL> return True <EOL> return False <EOL> class PermissionsMixin ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> is_superuser = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> groups = models . ManyToManyField ( Group , verbose_name = _ ( '<STR_LIT>' ) , <EOL> blank = True , help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> user_permissions = models . ManyToManyField ( Permission , <EOL> verbose_name = _ ( '<STR_LIT>' ) , blank = True , <EOL> help_text = '<STR_LIT>' ) <EOL> class Meta : <EOL> abstract = True <EOL> def get_group_permissions ( self , obj = None ) : <EOL> """<STR_LIT>""" <EOL> permissions = set ( ) <EOL> for backend in auth . get_backends ( ) : <EOL> if hasattr ( backend , "<STR_LIT>" ) : <EOL> if obj is not None : <EOL> permissions . update ( backend . get_group_permissions ( self , <EOL> obj ) ) <EOL> else : <EOL> permissions . update ( backend . get_group_permissions ( self ) ) <EOL> return permissions <EOL> def get_all_permissions ( self , obj = None ) : <EOL> return _user_get_all_permissions ( self , obj ) <EOL> def has_perm ( self , perm , obj = None ) : <EOL> """<STR_LIT>""" <EOL> if self . is_active and self . is_superuser : <EOL> return True <EOL> return _user_has_perm ( self , perm , obj ) <EOL> def has_perms ( self , perm_list , obj = None ) : <EOL> """<STR_LIT>""" <EOL> for perm in perm_list : <EOL> if not self . has_perm ( perm , obj ) : <EOL> return False <EOL> return True <EOL> def has_module_perms ( self , app_label ) : <EOL> """<STR_LIT>""" <EOL> if self . is_active and self . is_superuser : <EOL> return True <EOL> return _user_has_module_perms ( self , app_label ) <EOL> class AbstractUser ( AbstractBaseUser , PermissionsMixin ) : <EOL> """<STR_LIT>""" <EOL> username = models . CharField ( _ ( '<STR_LIT:username>' ) , max_length = <NUM_LIT:30> , unique = True , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> validators = [ <EOL> validators . RegexValidator ( re . compile ( '<STR_LIT>' ) , _ ( '<STR_LIT>' ) , '<STR_LIT>' ) <EOL> ] ) <EOL> first_name = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:30> , blank = True ) <EOL> last_name = models . CharField ( _ ( '<STR_LIT>' ) , max_length = <NUM_LIT:30> , blank = True ) <EOL> email = models . EmailField ( _ ( '<STR_LIT>' ) , blank = True ) <EOL> is_staff = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> is_active = models . BooleanField ( _ ( '<STR_LIT>' ) , default = True , <EOL> help_text = _ ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> date_joined = models . DateTimeField ( _ ( '<STR_LIT>' ) , default = timezone . now ) <EOL> objects = UserManager ( ) <EOL> USERNAME_FIELD = '<STR_LIT:username>' <EOL> REQUIRED_FIELDS = [ '<STR_LIT:email>' ] <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT:user>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> abstract = True <EOL> def get_absolute_url ( self ) : <EOL> return "<STR_LIT>" % urlquote ( self . username ) <EOL> def get_full_name ( self ) : <EOL> """<STR_LIT>""" <EOL> full_name = '<STR_LIT>' % ( self . first_name , self . last_name ) <EOL> return full_name . strip ( ) <EOL> def get_short_name ( self ) : <EOL> "<STR_LIT>" <EOL> return self . first_name <EOL> def email_user ( self , subject , message , from_email = None ) : <EOL> """<STR_LIT>""" <EOL> send_mail ( subject , message , from_email , [ self . email ] ) <EOL> def get_profile ( self ) : <EOL> """<STR_LIT>""" <EOL> warnings . warn ( "<STR_LIT>" , <EOL> PendingDeprecationWarning ) <EOL> if not hasattr ( self , '<STR_LIT>' ) : <EOL> from django . conf import settings <EOL> if not getattr ( settings , '<STR_LIT>' , False ) : <EOL> raise SiteProfileNotAvailable ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> app_label , model_name = settings . AUTH_PROFILE_MODULE . split ( '<STR_LIT:.>' ) <EOL> except ValueError : <EOL> raise SiteProfileNotAvailable ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> try : <EOL> model = models . get_model ( app_label , model_name ) <EOL> if model is None : <EOL> raise SiteProfileNotAvailable ( <EOL> '<STR_LIT>' <EOL> '<STR_LIT>' ) <EOL> self . _profile_cache = model . _default_manager . using ( <EOL> self . _state . db ) . get ( user__id__exact = self . id ) <EOL> self . _profile_cache . user = self <EOL> except ( ImportError , ImproperlyConfigured ) : <EOL> raise SiteProfileNotAvailable <EOL> return self . _profile_cache <EOL> class User ( AbstractUser ) : <EOL> """<STR_LIT>""" <EOL> class Meta : <EOL> swappable = '<STR_LIT>' <EOL> @ python_2_unicode_compatible <EOL> class AnonymousUser ( object ) : <EOL> id = None <EOL> pk = None <EOL> username = '<STR_LIT>' <EOL> is_staff = False <EOL> is_active = False <EOL> is_superuser = False <EOL> _groups = EmptyManager ( ) <EOL> _user_permissions = EmptyManager ( ) <EOL> def __init__ ( self ) : <EOL> pass <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __eq__ ( self , other ) : <EOL> return isinstance ( other , self . __class__ ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return <NUM_LIT:1> <EOL> def save ( self ) : <EOL> raise NotImplementedError <EOL> def delete ( self ) : <EOL> raise NotImplementedError <EOL> def set_password ( self , raw_password ) : <EOL> raise NotImplementedError <EOL> def check_password ( self , raw_password ) : <EOL> raise NotImplementedError <EOL> def _get_groups ( self ) : <EOL> return self . _groups <EOL> groups = property ( _get_groups ) <EOL> def _get_user_permissions ( self ) : <EOL> return self . _user_permissions <EOL> user_permissions = property ( _get_user_permissions ) <EOL> def get_group_permissions ( self , obj = None ) : <EOL> return set ( ) <EOL> def get_all_permissions ( self , obj = None ) : <EOL> return _user_get_all_permissions ( self , obj = obj ) <EOL> def has_perm ( self , perm , obj = None ) : <EOL> return _user_has_perm ( self , perm , obj = obj ) <EOL> def has_perms ( self , perm_list , obj = None ) : <EOL> for perm in perm_list : <EOL> if not self . has_perm ( perm , obj ) : <EOL> return False <EOL> return True <EOL> def has_module_perms ( self , module ) : <EOL> return _user_has_module_perms ( self , module ) <EOL> def is_anonymous ( self ) : <EOL> return True <EOL> def is_authenticated ( self ) : <EOL> return False </s>
