<s> from django . db import models <EOL> from django . db . models import Q <EOL> from django . core . exceptions import ObjectDoesNotExist <EOL> from django . contrib . auth import get_user_model <EOL> from django . contrib . auth . models import UserManager , Permission , AnonymousUser <EOL> from django . contrib . contenttypes . models import ContentType <EOL> from django . utils . encoding import smart_text <EOL> from django . utils . translation import ugettext as _ <EOL> from django . conf import settings <EOL> from django . utils . six import text_type <EOL> from userena import settings as userena_settings <EOL> from userena . utils import generate_sha1 , get_profile_model , get_datetime_now , get_user_profile <EOL> from userena import signals as userena_signals <EOL> from guardian . shortcuts import assign_perm , get_perms <EOL> import re <EOL> SHA1_RE = re . compile ( '<STR_LIT>' ) <EOL> ASSIGNED_PERMISSIONS = { <EOL> '<STR_LIT>' : <EOL> ( ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) , <EOL> '<STR_LIT:user>' : <EOL> ( ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> } <EOL> class UserenaManager ( UserManager ) : <EOL> """<STR_LIT>""" <EOL> def create_user ( self , username , email , password , active = False , <EOL> send_email = True ) : <EOL> """<STR_LIT>""" <EOL> new_user = get_user_model ( ) . objects . create_user ( <EOL> username , email , password ) <EOL> new_user . is_active = active <EOL> new_user . save ( ) <EOL> for perm in ASSIGNED_PERMISSIONS [ '<STR_LIT>' ] : <EOL> assign_perm ( perm [ <NUM_LIT:0> ] , new_user , get_user_profile ( user = new_user ) ) <EOL> for perm in ASSIGNED_PERMISSIONS [ '<STR_LIT:user>' ] : <EOL> assign_perm ( perm [ <NUM_LIT:0> ] , new_user , new_user ) <EOL> userena_profile = self . create_userena_profile ( new_user ) <EOL> if send_email : <EOL> userena_profile . send_activation_email ( ) <EOL> return new_user <EOL> def create_userena_profile ( self , user ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( user . username , text_type ) : <EOL> user . username = smart_text ( user . username ) <EOL> salt , activation_key = generate_sha1 ( user . username ) <EOL> try : <EOL> profile = self . get ( user = user ) <EOL> except self . model . DoesNotExist : <EOL> profile = self . create ( user = user , <EOL> activation_key = activation_key ) <EOL> return profile <EOL> def reissue_activation ( self , activation_key ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> userena = self . get ( activation_key = activation_key ) <EOL> except self . model . DoesNotExist : <EOL> return False <EOL> try : <EOL> salt , new_activation_key = generate_sha1 ( userena . user . username ) <EOL> userena . activation_key = new_activation_key <EOL> userena . save ( using = self . _db ) <EOL> userena . user . date_joined = get_datetime_now ( ) <EOL> userena . user . save ( using = self . _db ) <EOL> userena . send_activation_email ( ) <EOL> return True <EOL> except Exception : <EOL> return False <EOL> def activate_user ( self , activation_key ) : <EOL> """<STR_LIT>""" <EOL> if SHA1_RE . search ( activation_key ) : <EOL> try : <EOL> userena = self . get ( activation_key = activation_key ) <EOL> except self . model . DoesNotExist : <EOL> return False <EOL> if not userena . activation_key_expired ( ) : <EOL> userena . activation_key = userena_settings . USERENA_ACTIVATED <EOL> user = userena . user <EOL> user . is_active = True <EOL> userena . save ( using = self . _db ) <EOL> user . save ( using = self . _db ) <EOL> userena_signals . activation_complete . send ( sender = None , <EOL> user = user ) <EOL> return user <EOL> return False <EOL> def check_expired_activation ( self , activation_key ) : <EOL> """<STR_LIT>""" <EOL> if SHA1_RE . search ( activation_key ) : <EOL> userena = self . get ( activation_key = activation_key ) <EOL> return userena . activation_key_expired ( ) <EOL> raise self . model . DoesNotExist <EOL> def confirm_email ( self , confirmation_key ) : <EOL> """<STR_LIT>""" <EOL> if SHA1_RE . search ( confirmation_key ) : <EOL> try : <EOL> userena = self . get ( email_confirmation_key = confirmation_key , <EOL> email_unconfirmed__isnull = False ) <EOL> except self . model . DoesNotExist : <EOL> return False <EOL> else : <EOL> user = userena . user <EOL> old_email = user . email <EOL> user . email = userena . email_unconfirmed <EOL> userena . email_unconfirmed , userena . email_confirmation_key = '<STR_LIT>' , '<STR_LIT>' <EOL> userena . save ( using = self . _db ) <EOL> user . save ( using = self . _db ) <EOL> userena_signals . confirmation_complete . send ( sender = None , <EOL> user = user , <EOL> old_email = old_email ) <EOL> return user <EOL> return False <EOL> def delete_expired_users ( self ) : <EOL> """<STR_LIT>""" <EOL> deleted_users = [ ] <EOL> for user in get_user_model ( ) . objects . filter ( is_staff = False , <EOL> is_active = False ) : <EOL> if user . userena_signup . activation_key_expired ( ) : <EOL> deleted_users . append ( user ) <EOL> user . delete ( ) <EOL> return deleted_users <EOL> def check_permissions ( self ) : <EOL> """<STR_LIT>""" <EOL> changed_permissions = [ ] <EOL> changed_users = [ ] <EOL> warnings = [ ] <EOL> for model , perms in ASSIGNED_PERMISSIONS . items ( ) : <EOL> if model == '<STR_LIT>' : <EOL> model_obj = get_profile_model ( ) <EOL> else : model_obj = get_user_model ( ) <EOL> model_content_type = ContentType . objects . get_for_model ( model_obj ) <EOL> for perm in perms : <EOL> try : <EOL> Permission . objects . get ( codename = perm [ <NUM_LIT:0> ] , <EOL> content_type = model_content_type ) <EOL> except Permission . DoesNotExist : <EOL> changed_permissions . append ( perm [ <NUM_LIT:1> ] ) <EOL> Permission . objects . create ( name = perm [ <NUM_LIT:1> ] , <EOL> codename = perm [ <NUM_LIT:0> ] , <EOL> content_type = model_content_type ) <EOL> for user in get_user_model ( ) . objects . exclude ( id = settings . ANONYMOUS_USER_ID ) : <EOL> try : <EOL> user_profile = get_user_profile ( user = user ) <EOL> except ObjectDoesNotExist : <EOL> warnings . append ( _ ( "<STR_LIT>" ) % { '<STR_LIT:username>' : user . username } ) <EOL> else : <EOL> all_permissions = get_perms ( user , user_profile ) + get_perms ( user , user ) <EOL> for model , perms in ASSIGNED_PERMISSIONS . items ( ) : <EOL> if model == '<STR_LIT>' : <EOL> perm_object = get_user_profile ( user = user ) <EOL> else : perm_object = user <EOL> for perm in perms : <EOL> if perm [ <NUM_LIT:0> ] not in all_permissions : <EOL> assign_perm ( perm [ <NUM_LIT:0> ] , user , perm_object ) <EOL> changed_users . append ( user ) <EOL> return ( changed_permissions , changed_users , warnings ) <EOL> class UserenaBaseProfileManager ( models . Manager ) : <EOL> """<STR_LIT>""" <EOL> def get_visible_profiles ( self , user = None ) : <EOL> """<STR_LIT>""" <EOL> profiles = self . all ( ) <EOL> filter_kwargs = { '<STR_LIT>' : True } <EOL> profiles = profiles . filter ( ** filter_kwargs ) <EOL> if user and isinstance ( user , AnonymousUser ) : <EOL> profiles = profiles . exclude ( Q ( privacy = '<STR_LIT>' ) | Q ( privacy = '<STR_LIT>' ) ) <EOL> else : profiles = profiles . exclude ( Q ( privacy = '<STR_LIT>' ) ) <EOL> return profiles </s>
