<s> """<STR_LIT>""" <EOL> from flask import render_template , redirect , url_for , abort , flash , request , current_app , make_response <EOL> from flask . ext . login import login_required , current_user <EOL> from . import main <EOL> from . forms import EditProfileForm , EditProfileAdminForm , PostForm , CommentForm <EOL> from . . import db <EOL> from . . models import Permission , Role , User , Post , Comment <EOL> from . . decorators import admin_required , permission_required <EOL> @ main . route ( '<STR_LIT:/>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def index ( ) : <EOL> form = PostForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> post = Post ( body = form . body . data , <EOL> author = current_user . _get_current_object ( ) ) <EOL> db . session . add ( post ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> page = request . args . get ( '<STR_LIT>' , <NUM_LIT:1> , type = int ) <EOL> show_followed = False <EOL> if current_user . is_authenticated ( ) : <EOL> show_followed = bool ( request . cookies . get ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> if show_followed : <EOL> query = current_user . followed_posts <EOL> else : <EOL> query = Post . query <EOL> pagination = query . order_by ( Post . timestamp . desc ( ) ) . paginate ( <EOL> page , per_page = current_app . config [ '<STR_LIT>' ] , <EOL> error_out = False ) <EOL> posts = pagination . items <EOL> return render_template ( '<STR_LIT>' , form = form , posts = posts , <EOL> show_followed = show_followed , pagination = pagination ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> def server_shutdown ( ) : <EOL> if not current_app . testing : <EOL> abort ( <NUM_LIT> ) <EOL> shutdown = request . environ . get ( '<STR_LIT>' ) <EOL> if not shutdown : <EOL> abort ( <NUM_LIT> ) <EOL> shutdown ( ) <EOL> return '<STR_LIT>' <EOL> @ main . route ( '<STR_LIT>' ) <EOL> def user ( username ) : <EOL> user = User . query . filter_by ( username = username ) . first ( ) <EOL> if user is None : <EOL> abort ( <NUM_LIT> ) <EOL> posts = user . posts . order_by ( Post . timestamp . desc ( ) ) . all ( ) <EOL> return render_template ( '<STR_LIT>' , user = user , posts = posts ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def edit_profile ( ) : <EOL> form = EditProfileForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> current_user . name = form . name . data <EOL> current_user . location = form . location . data <EOL> current_user . about_me = form . about_me . data <EOL> db . session . add ( current_user ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , username = current_user . username ) ) <EOL> form . name . data = current_user . name <EOL> form . location . data = current_user . location <EOL> form . about_me . data = current_user . about_me <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> @ admin_required <EOL> def edit_profile_admin ( id ) : <EOL> user = User . query . get_or_404 ( id ) <EOL> form = EditProfileAdminForm ( user = user ) <EOL> if form . validate_on_submit ( ) : <EOL> user . email = form . email . data <EOL> user . username = form . username . data <EOL> user . confirmed = form . confirmed . data <EOL> user . role = Role . query . get ( form . role . data ) <EOL> user . name = form . name . data <EOL> user . location = form . location . data <EOL> user . about_me = form . about_me . data <EOL> db . session . add ( user ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , username = user . username ) ) <EOL> form . email . data = user . email <EOL> form . username . data = user . username <EOL> form . confirmed . data = user . confirmed <EOL> form . role . data = user . role_id <EOL> form . name . data = user . name <EOL> form . location . data = user . location <EOL> form . about_me . data = user . about_me <EOL> return render_template ( '<STR_LIT>' , form = form , user = user ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def post ( id ) : <EOL> post = Post . query . get_or_404 ( id ) <EOL> form = CommentForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> comment = Comment ( body = form . body . data , <EOL> post = post , <EOL> author = current_user . _get_current_object ( ) ) <EOL> db . session . add ( comment ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = post . id , page = - <NUM_LIT:1> ) ) <EOL> page = request . args . get ( '<STR_LIT>' , <NUM_LIT:1> , type = int ) <EOL> if page == - <NUM_LIT:1> : <EOL> page = ( post . comments . count ( ) - <NUM_LIT:1> ) / current_app . config [ '<STR_LIT>' ] + <NUM_LIT:1> <EOL> pagination = post . comments . order_by ( Comment . timestamp . asc ( ) ) . paginate ( <EOL> page , per_page = current_app . config [ '<STR_LIT>' ] , <EOL> error_out = False ) <EOL> comments = pagination . items <EOL> return render_template ( '<STR_LIT>' , posts = [ post ] , form = form , <EOL> comments = comments , pagination = pagination ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def edit ( id ) : <EOL> post = Post . query . get_or_404 ( id ) <EOL> if current_user != post . author and not current_user . can ( Permission . ADMINISTER ) : <EOL> abort ( <NUM_LIT> ) <EOL> form = PostForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> post . body = form . body . data <EOL> db . session . add ( post ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = post . id ) ) <EOL> form . body . data = post . body <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def follow ( username ) : <EOL> user = User . query . filter_by ( username = username ) . first ( ) <EOL> if user is None : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> if current_user . is_following ( user ) : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , username = username ) ) <EOL> current_user . follow ( user ) <EOL> flash ( '<STR_LIT>' % username ) <EOL> return redirect ( url_for ( '<STR_LIT>' , username = username ) ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def unfollow ( username ) : <EOL> user = User . query . filter_by ( username = username ) . first ( ) <EOL> if user is None : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> if not current_user . is_following ( user ) : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , username = username ) ) <EOL> current_user . unfollow ( user ) <EOL> flash ( '<STR_LIT>' % username ) <EOL> return redirect ( url_for ( '<STR_LIT>' , username = username ) ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> def followers ( username ) : <EOL> user = User . query . filter_by ( username = username ) . first ( ) <EOL> if user is None : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> page = request . args . get ( '<STR_LIT>' , <NUM_LIT:1> , type = int ) <EOL> pagination = user . followers . paginate ( <EOL> page , per_page = current_app . config [ '<STR_LIT>' ] , <EOL> error_out = False ) <EOL> follows = [ { '<STR_LIT:user>' : item . follower , '<STR_LIT>' : item . timestamp } <EOL> for item in pagination . items ] <EOL> return render_template ( '<STR_LIT>' , user = user , title = "<STR_LIT>" , <EOL> endpoint = '<STR_LIT>' , pagination = pagination , <EOL> follows = follows ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> def followed_by ( username ) : <EOL> user = User . query . filter_by ( username = username ) . first ( ) <EOL> if user is None : <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> page = request . args . get ( '<STR_LIT>' , <NUM_LIT:1> , type = int ) <EOL> pagination = user . followed . paginate ( <EOL> page , per_page = current_app . config [ '<STR_LIT>' ] , <EOL> error_out = False ) <EOL> follows = [ { '<STR_LIT:user>' : item . followed , '<STR_LIT>' : item . timestamp } <EOL> for item in pagination . items ] <EOL> return render_template ( '<STR_LIT>' , user = user , title = "<STR_LIT>" , <EOL> endpoint = '<STR_LIT>' , pagination = pagination , <EOL> follows = follows ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def show_all ( ) : <EOL> resp = make_response ( redirect ( url_for ( '<STR_LIT>' ) ) ) <EOL> resp . set_cookie ( '<STR_LIT>' , '<STR_LIT>' , max_age = <NUM_LIT:30> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> return resp <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def show_followed ( ) : <EOL> resp = make_response ( redirect ( url_for ( '<STR_LIT>' ) ) ) <EOL> resp . set_cookie ( '<STR_LIT>' , '<STR_LIT:1>' , max_age = <NUM_LIT:30> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) <EOL> return resp <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> @ permission_required ( Permission . MODERATE_COMMENTS ) <EOL> def moderate ( ) : <EOL> page = request . args . get ( '<STR_LIT>' , <NUM_LIT:1> , type = int ) <EOL> pagination = Comment . query . order_by ( Comment . timestamp . desc ( ) ) . paginate ( <EOL> page , per_page = current_app . config [ '<STR_LIT>' ] , <EOL> error_out = False ) <EOL> comments = pagination . items <EOL> return render_template ( '<STR_LIT>' , comments = comments , <EOL> pagination = pagination , page = page ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> @ permission_required ( Permission . MODERATE_COMMENTS ) <EOL> def moderate_enable ( id ) : <EOL> comment = Comment . query . get_or_404 ( id ) <EOL> comment . disabled = False <EOL> db . session . add ( comment ) <EOL> return redirect ( url_for ( '<STR_LIT>' , <EOL> page = request . args . get ( '<STR_LIT>' , <NUM_LIT:1> , type = int ) ) ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> @ permission_required ( Permission . MODERATE_COMMENTS ) <EOL> def moderate_disable ( id ) : <EOL> comment = Comment . query . get_or_404 ( id ) <EOL> comment . disabled = True <EOL> db . session . add ( comment ) <EOL> return redirect ( url_for ( '<STR_LIT>' , <EOL> page = request . args . get ( '<STR_LIT>' , <NUM_LIT:1> , type = int ) ) ) </s>
