<s> from pyramid . httpexceptions import HTTPMovedPermanently , HTTPSeeOther <EOL> from pyramid . security import remember , forget <EOL> from pyramid . view import view_config <EOL> from sqlalchemy . orm import joinedload <EOL> from warehouse . accounts import REDIRECT_FIELD_NAME <EOL> from warehouse . accounts import forms <EOL> from warehouse . accounts . interfaces import IUserService <EOL> from warehouse . cache . origin import origin_cache <EOL> from warehouse . packaging . models import Project , Release <EOL> from warehouse . utils . http import is_safe_url <EOL> @ view_config ( <EOL> route_name = "<STR_LIT>" , <EOL> renderer = "<STR_LIT>" , <EOL> decorator = [ <EOL> origin_cache ( <EOL> <NUM_LIT:1> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> , <EOL> stale_while_revalidate = <NUM_LIT:5> * <NUM_LIT> , <EOL> stale_if_error = <NUM_LIT:1> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> , <EOL> ) , <EOL> ] , <EOL> ) <EOL> def profile ( user , request ) : <EOL> if user . username != request . matchdict . get ( "<STR_LIT:username>" , user . username ) : <EOL> return HTTPMovedPermanently ( <EOL> request . current_route_path ( username = user . username ) , <EOL> ) <EOL> projects = ( <EOL> request . db . query ( Release ) <EOL> . options ( joinedload ( Release . project ) ) <EOL> . join ( Project ) <EOL> . distinct ( Project . name ) <EOL> . filter ( Project . users . contains ( user ) ) <EOL> . order_by ( Project . name ) <EOL> . all ( ) <EOL> ) <EOL> return { "<STR_LIT:user>" : user , "<STR_LIT>" : projects } <EOL> @ view_config ( <EOL> route_name = "<STR_LIT>" , <EOL> renderer = "<STR_LIT>" , <EOL> uses_session = True , <EOL> require_csrf = True , <EOL> require_methods = False , <EOL> ) <EOL> def login ( request , redirect_field_name = REDIRECT_FIELD_NAME , <EOL> _form_class = forms . LoginForm ) : <EOL> user_service = request . find_service ( IUserService , context = None ) <EOL> redirect_to = request . POST . get ( redirect_field_name , <EOL> request . GET . get ( redirect_field_name ) ) <EOL> form = _form_class ( request . POST , user_service = user_service ) <EOL> if request . method == "<STR_LIT:POST>" and form . validate ( ) : <EOL> username = form . username . data <EOL> userid = user_service . find_userid ( username ) <EOL> if ( not redirect_to or <EOL> not is_safe_url ( url = redirect_to , host = request . host ) ) : <EOL> redirect_to = "<STR_LIT:/>" <EOL> headers = _login_user ( request , userid ) <EOL> return HTTPSeeOther ( redirect_to , headers = dict ( headers ) ) <EOL> return { <EOL> "<STR_LIT>" : form , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : REDIRECT_FIELD_NAME , <EOL> "<STR_LIT:data>" : redirect_to , <EOL> } , <EOL> } <EOL> @ view_config ( <EOL> route_name = "<STR_LIT>" , <EOL> renderer = "<STR_LIT>" , <EOL> uses_session = True , <EOL> require_csrf = True , <EOL> require_methods = False , <EOL> ) <EOL> def logout ( request , redirect_field_name = REDIRECT_FIELD_NAME ) : <EOL> redirect_to = request . POST . get ( redirect_field_name , <EOL> request . GET . get ( redirect_field_name ) ) <EOL> if request . method == "<STR_LIT:POST>" : <EOL> headers = forget ( request ) <EOL> request . session . invalidate ( ) <EOL> if ( not redirect_to or <EOL> not is_safe_url ( url = redirect_to , host = request . host ) ) : <EOL> redirect_to = "<STR_LIT:/>" <EOL> return HTTPSeeOther ( redirect_to , headers = dict ( headers ) ) <EOL> return { "<STR_LIT>" : { "<STR_LIT>" : REDIRECT_FIELD_NAME , "<STR_LIT:data>" : redirect_to } } <EOL> @ view_config ( <EOL> route_name = "<STR_LIT>" , <EOL> renderer = "<STR_LIT>" , <EOL> uses_session = True , <EOL> require_csrf = True , <EOL> require_methods = False , <EOL> ) <EOL> def register ( request , _form_class = forms . RegistrationForm ) : <EOL> if request . authenticated_userid is not None : <EOL> return HTTPSeeOther ( "<STR_LIT:/>" ) <EOL> user_service = request . find_service ( IUserService , context = None ) <EOL> recaptcha_service = request . find_service ( name = "<STR_LIT>" ) <EOL> request . find_service ( name = "<STR_LIT>" ) . merge ( recaptcha_service . csp_policy ) <EOL> post_body = { <EOL> key . replace ( "<STR_LIT:->" , "<STR_LIT:_>" ) : value <EOL> for key , value in request . POST . items ( ) <EOL> } <EOL> form = _form_class ( <EOL> data = post_body , user_service = user_service , <EOL> recaptcha_service = recaptcha_service <EOL> ) <EOL> if request . method == "<STR_LIT:POST>" and form . validate ( ) : <EOL> user = user_service . create_user ( <EOL> form . username . data , form . full_name . data , form . password . data , <EOL> form . email . data <EOL> ) <EOL> return HTTPSeeOther ( <EOL> request . route_path ( "<STR_LIT:index>" ) , <EOL> headers = dict ( _login_user ( request , user . id ) ) ) <EOL> return { "<STR_LIT>" : form } <EOL> def _login_user ( request , userid ) : <EOL> if ( request . unauthenticated_userid is not None and <EOL> request . unauthenticated_userid != userid ) : <EOL> request . session . invalidate ( ) <EOL> else : <EOL> data = dict ( request . session . items ( ) ) <EOL> request . session . invalidate ( ) <EOL> request . session . update ( data ) <EOL> headers = remember ( request , userid ) <EOL> request . session . new_csrf_token ( ) <EOL> return headers </s>
