<s> from flask import Blueprint , render_template , redirect , url_for , request , g <EOL> from . . decorators import must_be_able_to <EOL> from . . extensions import db <EOL> from . . utils import notify , get_or_404 <EOL> from . models import User <EOL> from . forms import EditUserForm , CreateUserForm <EOL> users = Blueprint ( '<STR_LIT>' , __name__ , url_prefix = '<STR_LIT>' ) <EOL> @ users . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ must_be_able_to ( '<STR_LIT>' ) <EOL> def create ( ) : <EOL> form = CreateUserForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> user = User ( ) <EOL> form . populate_obj ( user ) <EOL> user . set_password ( form . password . data ) <EOL> if User . query . filter_by ( username = form . username . data ) . first ( ) is None : <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <EOL> notify ( u'<STR_LIT>' . format ( user . username ) , <EOL> category = '<STR_LIT:success>' , action = '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = user . id ) ) <EOL> form . username . errors = [ u'<STR_LIT>' ] <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ users . route ( '<STR_LIT>' ) <EOL> def view ( id ) : <EOL> user = get_or_404 ( User , id = id ) <EOL> return render_template ( '<STR_LIT>' , user = user ) <EOL> @ users . route ( '<STR_LIT:/>' ) <EOL> def all ( ) : <EOL> users = User . query . all ( ) <EOL> return render_template ( '<STR_LIT>' , users = users ) <EOL> @ users . route ( '<STR_LIT>' ) <EOL> @ must_be_able_to ( '<STR_LIT>' ) <EOL> def delete ( id ) : <EOL> user = get_or_404 ( User , id = id ) <EOL> notify ( u'<STR_LIT>' . format ( user . username ) , <EOL> category = '<STR_LIT:success>' , action = '<STR_LIT>' ) <EOL> db . session . delete ( user ) <EOL> db . session . commit ( ) <EOL> return redirect ( request . args . get ( '<STR_LIT>' ) <EOL> or url_for ( '<STR_LIT>' ) ) <EOL> @ users . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def edit ( id ) : <EOL> user = get_or_404 ( User , id = id ) <EOL> if not ( g . user . can ( '<STR_LIT>' ) or user . id == g . user . id ) : <EOL> notify ( u"<STR_LIT>" , <EOL> category = '<STR_LIT:error>' , action = '<STR_LIT>' ) <EOL> return redirect ( request . args . get ( '<STR_LIT>' ) <EOL> or request . referrer <EOL> or url_for ( '<STR_LIT>' ) ) <EOL> form = EditUserForm ( request . form , user ) <EOL> if form . validate_on_submit ( ) : <EOL> form . populate_obj ( user ) <EOL> if form . password . data : <EOL> user . set_password ( form . password . data ) <EOL> db . session . add ( user ) <EOL> db . session . commit ( ) <EOL> notify ( u'<STR_LIT>' . format ( user . username ) , <EOL> category = '<STR_LIT:success>' , action = '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = id ) ) <EOL> return render_template ( '<STR_LIT>' , user = user , form = form ) </s>
