<s> from flask import Flask , render_template <EOL> app = Flask ( __name__ ) <EOL> from schema import db <EOL> import flask <EOL> from flask import request , redirect , abort <EOL> from functools import wraps <EOL> from flask import request , Response <EOL> from users import verify_user <EOL> def authenticate ( ) : <EOL> """<STR_LIT>""" <EOL> return Response ( '<STR_LIT>' , <EOL> <NUM_LIT> , { '<STR_LIT>' : '<STR_LIT>' } ) <EOL> @ app . before_request <EOL> def auth_helper ( ) : <EOL> request . user = None <EOL> auth = request . authorization <EOL> if not auth : <EOL> return <EOL> user = verify_user ( auth . username , auth . password ) <EOL> if not user : <EOL> return <EOL> request . user = user <EOL> return <EOL> def requires_auth ( f ) : <EOL> """<STR_LIT>""" <EOL> @ wraps ( f ) <EOL> def decorated ( * args , ** kwargs ) : <EOL> if not request . user : <EOL> return authenticate ( ) <EOL> return f ( * args , ** kwargs ) <EOL> return decorated <EOL> import users , repos , keys <EOL> @ app . route ( "<STR_LIT:/>" ) <EOL> def home ( ) : <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ requires_auth <EOL> def list_users ( ) : <EOL> userlist = users . get_users ( ) <EOL> return render_template ( '<STR_LIT>' , userlist = userlist ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ requires_auth <EOL> def user_details ( id ) : <EOL> user = users . get_user_by_id ( id ) <EOL> return render_template ( '<STR_LIT>' , user = user ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ requires_auth <EOL> def change_user_permissions ( id ) : <EOL> if not request . user [ '<STR_LIT>' ] : <EOL> raise KeyError ( "<STR_LIT>" ) <EOL> can_create_users = request . form [ '<STR_LIT>' ] <EOL> can_create_repositories = request . form [ '<STR_LIT>' ] <EOL> users . update_permissions ( id , <EOL> can_create_users = can_create_users , <EOL> can_create_repositories = can_create_repositories ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = id ) ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ requires_auth <EOL> def change_user_password ( id ) : <EOL> if id != request . user [ '<STR_LIT>' ] and not request . user [ '<STR_LIT>' ] : <EOL> raise KeyError ( "<STR_LIT>" ) <EOL> new_password = request . form [ '<STR_LIT>' ] <EOL> try : <EOL> old_password = request . form [ '<STR_LIT>' ] <EOL> except KeyError : <EOL> old_password = None <EOL> user = users . get_user_by_id ( id ) <EOL> name = user [ '<STR_LIT>' ] <EOL> if old_password is None and not request . user [ '<STR_LIT>' ] : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> users . change_password ( name , new_password , old_password ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = id ) ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ requires_auth <EOL> def add_user ( ) : <EOL> if not request . user [ '<STR_LIT>' ] : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> for key in [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if key not in request . form or len ( request . form [ key ] ) < <NUM_LIT:1> : <EOL> raise Exception ( "<STR_LIT>" <EOL> . format ( key ) ) <EOL> user_name = request . form [ '<STR_LIT>' ] <EOL> password = request . form [ '<STR_LIT>' ] <EOL> commit_name = request . form [ '<STR_LIT>' ] <EOL> commit_email = request . form [ '<STR_LIT>' ] <EOL> user_id = users . create_user ( <EOL> user_name , <EOL> password = password , <EOL> commit_name = commit_name , <EOL> commit_email = commit_email ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ requires_auth <EOL> def delete_user ( id ) : <EOL> if not request . user [ '<STR_LIT>' ] : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> users . delete_user ( id ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ requires_auth <EOL> def list_keys ( ) : <EOL> keylist = keys . get_keys ( request . user [ '<STR_LIT>' ] ) <EOL> return render_template ( '<STR_LIT>' , keylist = keylist ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ requires_auth <EOL> def add_key ( ) : <EOL> keys . create_key ( <EOL> user_id = request . user [ '<STR_LIT>' ] , <EOL> key = request . form [ '<STR_LIT:key>' ] . strip ( ) , <EOL> name = request . form [ '<STR_LIT>' ] . strip ( ) , <EOL> ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ requires_auth <EOL> def delete_key ( id ) : <EOL> key = keys . get_key_by_id ( id ) <EOL> if not key : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> if key [ '<STR_LIT>' ] != request . user [ '<STR_LIT>' ] : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> keys . delete_key ( id ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ requires_auth <EOL> def list_repos ( ) : <EOL> repolist = repos . get_repos_for_user ( request . user [ '<STR_LIT>' ] ) <EOL> return render_template ( '<STR_LIT>' , repolist = repolist ) <EOL> def assert_repo_permission ( repo_id , permission ) : <EOL> user_acls = repos . get_repo_acls ( <EOL> repo_id = repo_id , <EOL> user_id = request . user [ '<STR_LIT>' ] ) <EOL> user_acls = user_acls [ <NUM_LIT:0> ] <EOL> if not user_acls [ permission ] : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( <EOL> request . user [ '<STR_LIT>' ] , <EOL> permission , <EOL> repo_id ) ) <EOL> def render_repo_details ( id , template ) : <EOL> repo = repos . get_repo_by_id ( id ) <EOL> repo_acls = repos . get_repo_acls ( repo_id = repo [ '<STR_LIT>' ] ) <EOL> user_id = request . user [ '<STR_LIT>' ] <EOL> user_acls = [ a for a in repo_acls if a [ '<STR_LIT>' ] == user_id ] [ <NUM_LIT:0> ] <EOL> import urlparse <EOL> parsed_url = urlparse . urlparse ( request . url ) <EOL> return render_template ( template , <EOL> repo = repo , <EOL> repo_acls = repo_acls , <EOL> hostname = parsed_url . hostname , <EOL> user_acls = user_acls ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ requires_auth <EOL> def repo_details ( id ) : <EOL> assert_repo_permission ( id , '<STR_LIT>' ) <EOL> return render_repo_details ( id , '<STR_LIT>' ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ requires_auth <EOL> def edit_repo ( id ) : <EOL> assert_repo_permission ( id , '<STR_LIT>' ) <EOL> if request . method == '<STR_LIT:GET>' : <EOL> return render_repo_details ( id , '<STR_LIT>' ) <EOL> permissions = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ] <EOL> for user in users . get_users ( ) : <EOL> user_permissions = { } <EOL> if '<STR_LIT>' + str ( user [ '<STR_LIT>' ] ) not in request . form : <EOL> continue <EOL> for permission in permissions : <EOL> name = '<STR_LIT>' . format ( permission , user [ '<STR_LIT>' ] ) <EOL> value = name in request . form <EOL> user_permissions [ permission ] = value <EOL> repos . update_repo_acls ( <EOL> repo_id = id , <EOL> user_id = user [ '<STR_LIT>' ] , <EOL> ** user_permissions <EOL> ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = id ) ) <EOL> @ app . route ( "<STR_LIT>" ) <EOL> @ requires_auth <EOL> def delete_repo ( id ) : <EOL> repo = repos . get_repo_by_id ( id ) <EOL> repo_acls = repos . get_repo_acls ( <EOL> repo_id = repo [ '<STR_LIT>' ] , <EOL> user_id = request . user [ '<STR_LIT>' ] ) <EOL> if not repo_acls or len ( repo_acls ) < <NUM_LIT:1> or not repo_acls [ <NUM_LIT:0> ] [ '<STR_LIT>' ] : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> repos . delete_repo_by_id ( id ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ app . route ( "<STR_LIT>" , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ requires_auth <EOL> def add_repo ( ) : <EOL> if not request . user [ '<STR_LIT>' ] : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> name = request . form [ '<STR_LIT>' ] <EOL> if not name or len ( name ) <= <NUM_LIT:3> : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> id = repos . create_repo ( name = name , owner_id = request . user [ '<STR_LIT>' ] ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = id ) ) <EOL> @ app . template_global ( ) <EOL> def url_for ( target , * args , ** kwargs ) : <EOL> return flask . url_for ( target , * args , ** kwargs ) </s>
