<s> """<STR_LIT>""" <EOL> import re <EOL> from datetime import datetime <EOL> from pymongo import ASCENDING , DESCENDING <EOL> import bombolone . model . ranks <EOL> from bombolone . config import ACTIVATED <EOL> from bombolone . model import db <EOL> from model_engine import db_engine <EOL> from bombolone . core . utils import ensure_objectid , is_iterable <EOL> def find ( user_id = None , <EOL> username = None , <EOL> email = None , <EOL> rank = None , <EOL> lan = None , <EOL> expand_rank = False , <EOL> sorted_by = '<STR_LIT:username>' , <EOL> sort_ascending = True , <EOL> only_one = False , <EOL> my_rank = None , <EOL> my_id = None ) : <EOL> """<STR_LIT>""" <EOL> def denormalize ( user ) : <EOL> if user is None : <EOL> return user <EOL> if expand_rank : <EOL> user [ '<STR_LIT>' ] = { x [ '<STR_LIT>' ] : x [ '<STR_LIT:name>' ] for x in model . ranks . find ( ) } [ user [ '<STR_LIT>' ] ] <EOL> if isinstance ( my_rank , int ) and my_rank <= <NUM_LIT> : <EOL> return user <EOL> if str ( my_id ) == str ( user [ "<STR_LIT>" ] ) : <EOL> return user <EOL> user_to_show = { <EOL> "<STR_LIT>" : user . get ( "<STR_LIT>" , None ) , <EOL> "<STR_LIT>" : user . get ( "<STR_LIT>" , None ) , <EOL> "<STR_LIT:description>" : user . get ( "<STR_LIT:description>" , "<STR_LIT>" ) , <EOL> "<STR_LIT:image>" : user . get ( "<STR_LIT:image>" , "<STR_LIT>" ) , <EOL> "<STR_LIT:location>" : user . get ( "<STR_LIT:location>" , "<STR_LIT>" ) , <EOL> "<STR_LIT:name>" : user . get ( "<STR_LIT:name>" , "<STR_LIT>" ) , <EOL> "<STR_LIT:username>" : user . get ( "<STR_LIT:username>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : user . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> } <EOL> return user_to_show <EOL> if username : <EOL> if is_iterable ( username ) : <EOL> list_users = list ( db . users . find ( { "<STR_LIT:username>" : { "<STR_LIT>" : list ( username ) } } ) ) <EOL> return [ denormalize ( u ) for u in list_users ] <EOL> else : <EOL> regex = re . compile ( '<STR_LIT>' + username + '<STR_LIT:$>' , re . IGNORECASE ) <EOL> return denormalize ( db . users . find_one ( { "<STR_LIT:username>" : regex } ) ) <EOL> conditions = [ ] <EOL> if email : <EOL> email = email . lower ( ) <EOL> conditions . append ( { '<STR_LIT:email>' : email } ) <EOL> if rank : <EOL> conditions . append ( { '<STR_LIT>' : rank } ) <EOL> if lan : <EOL> conditions . append ( { '<STR_LIT>' : lan } ) <EOL> return db_engine ( collection = db . users , <EOL> item_id = user_id , <EOL> only_one = only_one , <EOL> conditions = conditions , <EOL> sorted_by = sorted_by , <EOL> sort_ascending = sort_ascending , <EOL> denormalize = denormalize ) <EOL> def create ( user = None ) : <EOL> """<STR_LIT>""" <EOL> if not "<STR_LIT:username>" in user : <EOL> return ( None , "<STR_LIT>" ) <EOL> if not "<STR_LIT>" in user : <EOL> return ( None , "<STR_LIT>" ) <EOL> if not "<STR_LIT>" in user : <EOL> return ( None , "<STR_LIT>" ) <EOL> new_user = { <EOL> "<STR_LIT>" : datetime . utcnow ( ) , <EOL> "<STR_LIT:description>" : "<STR_LIT>" , <EOL> "<STR_LIT:email>" : user . get ( "<STR_LIT:email>" ) , <EOL> "<STR_LIT:image>" : [ ] , <EOL> "<STR_LIT:location>" : "<STR_LIT>" , <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:username>" : user [ "<STR_LIT:username>" ] , <EOL> "<STR_LIT:password>" : user [ "<STR_LIT:password>" ] , <EOL> "<STR_LIT>" : <NUM_LIT> , <EOL> "<STR_LIT>" : user . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : user . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : "<STR_LIT>" , <EOL> "<STR_LIT:status>" : user . get ( "<STR_LIT:status>" , ACTIVATED ) <EOL> } <EOL> _id = db . users . insert ( new_user ) <EOL> return ( _id , None ) <EOL> def update ( user_id = None , <EOL> email = None , <EOL> lan = None , <EOL> language = None , <EOL> image = None , <EOL> password = None , <EOL> status = None , <EOL> unset = None , <EOL> pull = None , <EOL> addToSet = None , <EOL> user = None ) : <EOL> """<STR_LIT>""" <EOL> user_id = ensure_objectid ( user_id ) <EOL> if user_id is None : <EOL> return False <EOL> if user : <EOL> db . users . update ( { "<STR_LIT>" : user_id } , user ) <EOL> return True <EOL> if pull : <EOL> db . users . update ( { "<STR_LIT>" : user_id } , { "<STR_LIT>" : pull } ) <EOL> return True <EOL> if addToSet : <EOL> db . users . update ( { "<STR_LIT>" : user_id } , { "<STR_LIT>" : addToSet } ) <EOL> return True <EOL> dict_set = { } <EOL> local = locals ( ) <EOL> for item in [ "<STR_LIT:email>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT>" , <EOL> "<STR_LIT:image>" , <EOL> "<STR_LIT:password>" , <EOL> "<STR_LIT:status>" ] : <EOL> if not local [ item ] is None : <EOL> dict_set [ item ] = local [ item ] <EOL> if not unset is None : <EOL> dict_unset = { } <EOL> for item in unset : <EOL> dict_unset [ item ] = <NUM_LIT:1> <EOL> if is_iterable ( user_id ) : <EOL> for _id in user_id : <EOL> if ensure_objectid ( _id ) : <EOL> if unset : <EOL> db . users . update ( { "<STR_LIT>" : _id } , { "<STR_LIT>" : dict_unset } , False ) <EOL> db . users . update ( { "<STR_LIT>" : _id } , { "<STR_LIT>" : dict_set } ) <EOL> else : <EOL> if unset : <EOL> db . users . update ( { "<STR_LIT>" : user_id } , { "<STR_LIT>" : dict_unset } , False ) <EOL> db . users . update ( { "<STR_LIT>" : user_id } , { "<STR_LIT>" : dict_set } ) <EOL> db . users . ensure_index ( '<STR_LIT:username>' ) <EOL> return True <EOL> def remove ( username = None , my_rank = None ) : <EOL> """<STR_LIT>""" <EOL> if my_rank is None or my_rank > <NUM_LIT:30> : <EOL> return False <EOL> if username is None : <EOL> return False <EOL> else : <EOL> user = db . users . find_one ( { "<STR_LIT:username>" : username } ) <EOL> if user is None : <EOL> return False <EOL> if user . get ( '<STR_LIT>' ) is None or user [ "<STR_LIT>" ] <= my_rank : <EOL> return False <EOL> db . users . remove ( { "<STR_LIT:username>" : username } ) <EOL> return True </s>
