<s> from models . user import User <EOL> from models . user_sets import UserSets <EOL> from models . follow import Follow <EOL> from modules . content import get as c <EOL> from modules . discuss import get_posts_facade <EOL> from framework . routes import get , post , put , delete , abort <EOL> from framework . session import get_current_user , log_in_user , log_out_user <EOL> def _log_in ( user ) : <EOL> """<STR_LIT>""" <EOL> session_id = log_in_user ( user ) <EOL> if session_id : <EOL> return <NUM_LIT:200> , { <EOL> '<STR_LIT:user>' : user . deliver ( access = '<STR_LIT>' ) , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : session_id <EOL> } , <EOL> } <EOL> return abort ( <NUM_LIT> ) <EOL> @ get ( '<STR_LIT>' ) <EOL> def get_current_user_route ( request ) : <EOL> """<STR_LIT>""" <EOL> current_user = get_current_user ( request ) <EOL> if not current_user : <EOL> return abort ( <NUM_LIT> ) <EOL> return <NUM_LIT:200> , { '<STR_LIT:user>' : current_user . deliver ( access = '<STR_LIT>' ) } <EOL> @ get ( '<STR_LIT>' ) <EOL> def get_user_route ( request , user_id ) : <EOL> """<STR_LIT>""" <EOL> db_conn = request [ '<STR_LIT>' ] <EOL> user = User . get ( db_conn , id = user_id ) <EOL> current_user = get_current_user ( request ) <EOL> if not user : <EOL> return abort ( <NUM_LIT> ) <EOL> data = { } <EOL> data [ '<STR_LIT:user>' ] = user . deliver ( access = '<STR_LIT>' <EOL> if current_user <EOL> and user [ '<STR_LIT:id>' ] == current_user [ '<STR_LIT:id>' ] <EOL> else None ) <EOL> if '<STR_LIT>' in request [ '<STR_LIT>' ] : <EOL> data [ '<STR_LIT>' ] = [ post . deliver ( ) for post in <EOL> get_posts_facade ( db_conn , user_id = user [ '<STR_LIT:id>' ] ) ] <EOL> if ( '<STR_LIT>' in request [ '<STR_LIT>' ] <EOL> and user [ '<STR_LIT>' ] [ '<STR_LIT>' ] == '<STR_LIT>' ) : <EOL> u_sets = UserSets . get ( db_conn , user_id = user [ '<STR_LIT:id>' ] ) <EOL> data [ '<STR_LIT>' ] = [ set_ . deliver ( ) for set_ in u_sets . list_sets ( db_conn ) ] <EOL> if ( '<STR_LIT>' in request [ '<STR_LIT>' ] <EOL> and user [ '<STR_LIT>' ] [ '<STR_LIT>' ] == '<STR_LIT>' ) : <EOL> data [ '<STR_LIT>' ] = [ follow . deliver ( ) for follow in <EOL> Follow . list ( db_conn , user_id = user [ '<STR_LIT:id>' ] ) ] <EOL> if '<STR_LIT>' in request [ '<STR_LIT>' ] : <EOL> size = int ( request [ '<STR_LIT>' ] [ '<STR_LIT>' ] ) <EOL> data [ '<STR_LIT>' ] = user . get_avatar ( size if size else None ) <EOL> return <NUM_LIT:200> , data <EOL> @ post ( '<STR_LIT>' ) <EOL> def create_user_route ( request ) : <EOL> """<STR_LIT>""" <EOL> db_conn = request [ '<STR_LIT>' ] <EOL> user , errors = User . insert ( db_conn , request [ '<STR_LIT>' ] ) <EOL> if len ( errors ) : <EOL> return <NUM_LIT> , { <EOL> '<STR_LIT>' : errors , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> return _log_in ( user ) <EOL> @ post ( '<STR_LIT>' ) <EOL> def log_in_route ( request ) : <EOL> """<STR_LIT>""" <EOL> db_conn = request [ '<STR_LIT>' ] <EOL> name = request [ '<STR_LIT>' ] . get ( '<STR_LIT:name>' ) or '<STR_LIT>' <EOL> name = name . lower ( ) . strip ( ) <EOL> user = User . get ( db_conn , name = name ) <EOL> if not user : <EOL> user = User . get ( db_conn , email = request [ '<STR_LIT>' ] . get ( '<STR_LIT:name>' ) ) <EOL> if not user : <EOL> return <NUM_LIT> , { <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:name>' : '<STR_LIT:name>' , <EOL> '<STR_LIT:message>' : c ( '<STR_LIT>' ) , <EOL> } ] , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> if not user . is_password_valid ( request [ '<STR_LIT>' ] . get ( '<STR_LIT:password>' ) ) : <EOL> return <NUM_LIT> , { <EOL> '<STR_LIT>' : [ { <EOL> '<STR_LIT:name>' : '<STR_LIT:password>' , <EOL> '<STR_LIT:message>' : c ( '<STR_LIT>' ) , <EOL> } ] , <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> return _log_in ( user ) <EOL> @ delete ( '<STR_LIT>' ) <EOL> def log_out_route ( request ) : <EOL> """<STR_LIT>""" <EOL> log_out_user ( request ) <EOL> return <NUM_LIT:200> , { <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : None <EOL> } <EOL> } <EOL> @ put ( '<STR_LIT>' ) <EOL> def update_user_route ( request , user_id ) : <EOL> """<STR_LIT>""" <EOL> db_conn = request [ '<STR_LIT>' ] <EOL> user = User . get ( db_conn , id = user_id ) <EOL> current_user = get_current_user ( request ) <EOL> if not user : <EOL> return abort ( <NUM_LIT> ) <EOL> if not user [ '<STR_LIT:id>' ] == current_user [ '<STR_LIT:id>' ] : <EOL> return abort ( <NUM_LIT> ) <EOL> user , errors = user . update ( db_conn , request [ '<STR_LIT>' ] ) <EOL> if len ( errors ) : <EOL> return <NUM_LIT> , { <EOL> '<STR_LIT>' : errors , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> return <NUM_LIT:200> , { '<STR_LIT:user>' : user . deliver ( access = '<STR_LIT>' ) } <EOL> @ post ( '<STR_LIT>' ) <EOL> def create_token_route ( request ) : <EOL> """<STR_LIT>""" <EOL> db_conn = request [ '<STR_LIT>' ] <EOL> user = User . get ( db_conn , email = request [ '<STR_LIT>' ] . get ( '<STR_LIT:email>' ) ) <EOL> if not user : <EOL> return abort ( <NUM_LIT> ) <EOL> user . get_email_token ( ) <EOL> return <NUM_LIT:200> , { } <EOL> @ post ( '<STR_LIT>' ) <EOL> def create_password_route ( request , user_id ) : <EOL> """<STR_LIT>""" <EOL> db_conn = request [ '<STR_LIT>' ] <EOL> user = User . get ( db_conn , id = user_id ) <EOL> if not user : <EOL> return abort ( <NUM_LIT> ) <EOL> valid = user . is_valid_token ( request [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) ) <EOL> if not valid : <EOL> return abort ( <NUM_LIT> ) <EOL> user [ '<STR_LIT:password>' ] = request [ '<STR_LIT>' ] . get ( '<STR_LIT:password>' ) <EOL> user . save ( db_conn ) <EOL> return _log_in ( user ) </s>
