<s> from mongoengine import * <EOL> from flask_login import UserMixin <EOL> from random import randint , random , Random <EOL> import time , hashlib , datetime <EOL> from utils import get_hexdigest <EOL> righthand = '<STR_LIT>' <EOL> lefthand = '<STR_LIT>' <EOL> allchars = righthand + lefthand <EOL> def generate_password ( length = <NUM_LIT:8> ) : <EOL> rng = Random ( ) <EOL> chars = [ ] <EOL> for i in range ( <NUM_LIT:0> , length ) : <EOL> chars . append ( rng . choice ( allchars ) ) <EOL> return '<STR_LIT>' . join ( chars ) <EOL> class BaseUser ( Document , UserMixin ) : <EOL> email = StringField ( unique = True ) <EOL> name = StringField ( ) <EOL> password = StringField ( max_length = <NUM_LIT> ) <EOL> date_joined = DateTimeField ( default = datetime . datetime . now ) <EOL> email_activation_key = StringField ( default = "<STR_LIT>" ) <EOL> is_email_activated = BooleanField ( default = True ) <EOL> password_reset_token = StringField ( ) <EOL> roles = ListField ( StringField ( ) , default = [ ] ) <EOL> meta = { "<STR_LIT>" : True } <EOL> @ property <EOL> def first_name ( self ) : <EOL> return name . split ( "<STR_LIT:U+0020>" ) [ <NUM_LIT:0> ] <EOL> @ property <EOL> def last_name ( self ) : <EOL> arr = name . split ( "<STR_LIT:U+0020>" ) <EOL> if ( len ( arr ) > <NUM_LIT:1> ) : <EOL> return '<STR_LIT:U+0020>' . join ( arr [ <NUM_LIT:1> : ] ) <EOL> else : <EOL> return '<STR_LIT>' <EOL> @ property <EOL> def short_name ( self ) : <EOL> if ( re . search ( "<STR_LIT>" , self . name [ <NUM_LIT:1> ] ) ) : return self . name <EOL> arr = self . name . split ( "<STR_LIT:U+0020>" ) <EOL> if ( len ( arr ) == <NUM_LIT:1> ) : return self . name <EOL> s = "<STR_LIT>" <EOL> for i in range ( <NUM_LIT:0> , len ( arr ) - <NUM_LIT:1> ) : <EOL> s += arr [ i ] [ <NUM_LIT:0> ] <EOL> s += "<STR_LIT:U+0020>" + arr [ - <NUM_LIT:1> ] <EOL> return s . strip ( ) <EOL> def has_role ( self , role ) : <EOL> if ( not self . roles or role not in self . roles ) : <EOL> return False <EOL> return True <EOL> def add_role ( self , role ) : <EOL> if ( role not in self . roles ) : <EOL> self . roles . append ( role ) <EOL> def remove_role ( self , role ) : <EOL> if ( role in self . roles ) : <EOL> self . roles . remove ( role ) <EOL> def mark_email_for_activation ( self ) : <EOL> self . is_email_activated = False <EOL> self . email_activation_key = sha_constructor ( str ( time . time ( ) ) + str ( randint ( <NUM_LIT:1> , <NUM_LIT> ) ) ) . hexdigest ( ) <EOL> def set_password ( self , raw_password ) : <EOL> """<STR_LIT>""" <EOL> algo = '<STR_LIT>' <EOL> salt = get_hexdigest ( algo , str ( random ( ) ) , str ( random ( ) ) ) [ : <NUM_LIT:5> ] <EOL> hash = get_hexdigest ( algo , salt , raw_password ) <EOL> self . password = '<STR_LIT>' % ( algo , salt , hash ) <EOL> self . save ( ) <EOL> return self <EOL> def check_password ( self , raw_password ) : <EOL> """<STR_LIT>""" <EOL> algo , salt , hash = self . password . split ( '<STR_LIT:$>' ) <EOL> return hash == get_hexdigest ( algo , salt , raw_password ) <EOL> def generate_password_reset_token ( self ) : <EOL> self . password_reset_token = hashlib . sha1 ( "<STR_LIT>" % ( str ( self . id ) , str ( time . time ( ) ) , random ( ) ) ) . hexdigest ( ) <EOL> self . save ( ) <EOL> return self . password_reset_token <EOL> @ classmethod <EOL> def create_user ( cls , name , email , password , email_verified = True ) : <EOL> """<STR_LIT>""" <EOL> now = datetime . datetime . utcnow ( ) <EOL> try : <EOL> email_name , domain_part = email . strip ( ) . split ( '<STR_LIT:@>' , <NUM_LIT:1> ) <EOL> except ValueError : <EOL> pass <EOL> else : <EOL> email = '<STR_LIT:@>' . join ( [ email_name . lower ( ) , domain_part . lower ( ) ] ) <EOL> user = User ( name = name , email = email , date_joined = now ) <EOL> if ( not password ) : <EOL> password = generate_password ( ) <EOL> user . set_password ( password ) <EOL> if ( not email_verified ) : <EOL> user . mark_email_for_activation ( ) <EOL> else : <EOL> user . is_email_activated = True <EOL> user . save ( ) <EOL> return user <EOL> def authenticate ( email = None , password = None ) : <EOL> user = User . objects ( email = email ) . first ( ) <EOL> if user : <EOL> if password and user . check_password ( password ) : <EOL> return user <EOL> return None <EOL> from werkzeug import import_string , LocalProxy <EOL> from flask import current_app <EOL> User = LocalProxy ( lambda : import_string ( current_app . config . get ( "<STR_LIT>" , "<STR_LIT>" ) ) ) </s>
