<s> import datetime <EOL> import urllib <EOL> from django . contrib import auth <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . db import models <EOL> from django . db . models . manager import EmptyManager <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . utils . encoding import smart_str <EOL> from django . utils . hashcompat import md5_constructor , sha_constructor <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> UNUSABLE_PASSWORD = '<STR_LIT:!>' <EOL> try : <EOL> set <EOL> except NameError : <EOL> from sets import Set as set <EOL> def get_hexdigest ( algorithm , salt , raw_password ) : <EOL> """<STR_LIT>""" <EOL> raw_password , salt = smart_str ( raw_password ) , smart_str ( salt ) <EOL> if algorithm == '<STR_LIT>' : <EOL> try : <EOL> import crypt <EOL> except ImportError : <EOL> raise ValueError ( '<STR_LIT>' ) <EOL> return crypt . crypt ( raw_password , salt ) <EOL> if algorithm == '<STR_LIT>' : <EOL> return md5_constructor ( salt + raw_password ) . hexdigest ( ) <EOL> elif algorithm == '<STR_LIT>' : <EOL> return sha_constructor ( salt + raw_password ) . hexdigest ( ) <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> def check_password ( raw_password , enc_password ) : <EOL> """<STR_LIT>""" <EOL> algo , salt , hsh = enc_password . split ( '<STR_LIT:$>' ) <EOL> return hsh == get_hexdigest ( algo , salt , raw_password ) <EOL> class SiteProfileNotAvailable ( Exception ) : <EOL> pass <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> 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> def __unicode__ ( self ) : <EOL> return u"<STR_LIT>" % ( <EOL> unicode ( self . content_type . app_label ) , <EOL> unicode ( self . content_type ) , <EOL> unicode ( self . name ) ) <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 , verbose_name = _ ( '<STR_LIT>' ) , blank = True ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . name <EOL> class UserManager ( models . Manager ) : <EOL> def create_user ( self , username , email , password = None ) : <EOL> "<STR_LIT>" <EOL> now = datetime . datetime . now ( ) <EOL> user = self . model ( None , username , '<STR_LIT>' , '<STR_LIT>' , email . strip ( ) . lower ( ) , '<STR_LIT>' , False , True , False , now , now ) <EOL> if password : <EOL> user . set_password ( password ) <EOL> else : <EOL> user . set_unusable_password ( ) <EOL> user . save ( ) <EOL> return user <EOL> def create_superuser ( self , username , email , password ) : <EOL> u = self . create_user ( username , email , password ) <EOL> u . is_staff = True <EOL> u . is_active = True <EOL> u . is_superuser = True <EOL> u . save ( ) <EOL> return u <EOL> def make_random_password ( self , length = <NUM_LIT:10> , allowed_chars = '<STR_LIT>' ) : <EOL> "<STR_LIT>" <EOL> from random import choice <EOL> return '<STR_LIT>' . join ( [ choice ( allowed_chars ) for i in range ( length ) ] ) <EOL> class User ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> username = models . CharField ( _ ( '<STR_LIT:username>' ) , max_length = <NUM_LIT:30> , unique = True , help_text = _ ( "<STR_LIT>" ) ) <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> password = models . CharField ( _ ( '<STR_LIT:password>' ) , max_length = <NUM_LIT> , help_text = _ ( "<STR_LIT>" ) ) <EOL> is_staff = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False , help_text = _ ( "<STR_LIT>" ) ) <EOL> is_active = models . BooleanField ( _ ( '<STR_LIT>' ) , default = True , help_text = _ ( "<STR_LIT>" ) ) <EOL> is_superuser = models . BooleanField ( _ ( '<STR_LIT>' ) , default = False , help_text = _ ( "<STR_LIT>" ) ) <EOL> last_login = models . DateTimeField ( _ ( '<STR_LIT>' ) , default = datetime . datetime . now ) <EOL> date_joined = models . DateTimeField ( _ ( '<STR_LIT>' ) , default = datetime . datetime . now ) <EOL> groups = models . ManyToManyField ( Group , verbose_name = _ ( '<STR_LIT>' ) , blank = True , <EOL> help_text = _ ( "<STR_LIT>" ) ) <EOL> user_permissions = models . ManyToManyField ( Permission , verbose_name = _ ( '<STR_LIT>' ) , blank = True ) <EOL> objects = UserManager ( ) <EOL> class Meta : <EOL> verbose_name = _ ( '<STR_LIT:user>' ) <EOL> verbose_name_plural = _ ( '<STR_LIT>' ) <EOL> def __unicode__ ( self ) : <EOL> return self . username <EOL> def get_absolute_url ( self ) : <EOL> return "<STR_LIT>" % urllib . quote ( smart_str ( self . 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 get_full_name ( self ) : <EOL> "<STR_LIT>" <EOL> full_name = u'<STR_LIT>' % ( self . first_name , self . last_name ) <EOL> return full_name . strip ( ) <EOL> def set_password ( self , raw_password ) : <EOL> import random <EOL> algo = '<STR_LIT>' <EOL> salt = get_hexdigest ( algo , str ( random . random ( ) ) , str ( random . random ( ) ) ) [ : <NUM_LIT:5> ] <EOL> hsh = get_hexdigest ( algo , salt , raw_password ) <EOL> self . password = '<STR_LIT>' % ( algo , salt , hsh ) <EOL> def check_password ( self , raw_password ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT:$>' not in self . password : <EOL> is_correct = ( self . password == get_hexdigest ( '<STR_LIT>' , '<STR_LIT>' , raw_password ) ) <EOL> if is_correct : <EOL> self . set_password ( raw_password ) <EOL> self . save ( ) <EOL> return is_correct <EOL> return check_password ( raw_password , self . password ) <EOL> def set_unusable_password ( self ) : <EOL> self . password = UNUSABLE_PASSWORD <EOL> def has_usable_password ( self ) : <EOL> return self . password != UNUSABLE_PASSWORD <EOL> def get_group_permissions ( self ) : <EOL> """<STR_LIT>""" <EOL> permissions = set ( ) <EOL> for backend in auth . get_backends ( ) : <EOL> if hasattr ( backend , "<STR_LIT>" ) : <EOL> permissions . update ( backend . get_group_permissions ( self ) ) <EOL> return permissions <EOL> def get_all_permissions ( self ) : <EOL> permissions = set ( ) <EOL> for backend in auth . get_backends ( ) : <EOL> if hasattr ( backend , "<STR_LIT>" ) : <EOL> permissions . update ( backend . get_all_permissions ( self ) ) <EOL> return permissions <EOL> def has_perm ( self , perm ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_active : <EOL> return False <EOL> if self . is_superuser : <EOL> return True <EOL> for backend in auth . get_backends ( ) : <EOL> if hasattr ( backend , "<STR_LIT>" ) : <EOL> if backend . has_perm ( self , perm ) : <EOL> return True <EOL> return False <EOL> def has_perms ( self , perm_list ) : <EOL> """<STR_LIT>""" <EOL> for perm in perm_list : <EOL> if not self . has_perm ( perm ) : <EOL> return False <EOL> return True <EOL> def has_module_perms ( self , app_label ) : <EOL> """<STR_LIT>""" <EOL> if not self . is_active : <EOL> return False <EOL> if self . is_superuser : <EOL> return True <EOL> for backend in auth . get_backends ( ) : <EOL> if hasattr ( backend , "<STR_LIT>" ) : <EOL> if backend . has_module_perms ( self , app_label ) : <EOL> return True <EOL> return False <EOL> def get_and_delete_messages ( self ) : <EOL> messages = [ ] <EOL> for m in self . message_set . all ( ) : <EOL> messages . append ( m . message ) <EOL> m . delete ( ) <EOL> return messages <EOL> def email_user ( self , subject , message , from_email = None ) : <EOL> "<STR_LIT>" <EOL> from django . core . mail import send_mail <EOL> send_mail ( subject , message , from_email , [ self . email ] ) <EOL> def get_profile ( self ) : <EOL> """<STR_LIT>""" <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> try : <EOL> app_label , model_name = settings . AUTH_PROFILE_MODULE . split ( '<STR_LIT:.>' ) <EOL> model = models . get_model ( app_label , model_name ) <EOL> self . _profile_cache = model . _default_manager . 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 Message ( models . Model ) : <EOL> """<STR_LIT>""" <EOL> user = models . ForeignKey ( User ) <EOL> message = models . TextField ( _ ( '<STR_LIT:message>' ) ) <EOL> def __unicode__ ( self ) : <EOL> return self . message <EOL> class AnonymousUser ( object ) : <EOL> id = 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 __unicode__ ( self ) : <EOL> return '<STR_LIT>' <EOL> def __str__ ( self ) : <EOL> return unicode ( self ) . encode ( '<STR_LIT:utf-8>' ) <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 has_perm ( self , perm ) : <EOL> return False <EOL> def has_perms ( self , perm_list ) : <EOL> return False <EOL> def has_module_perms ( self , module ) : <EOL> return False <EOL> def get_and_delete_messages ( self ) : <EOL> return [ ] <EOL> def is_anonymous ( self ) : <EOL> return True <EOL> def is_authenticated ( self ) : <EOL> return False </s>
