<s> from datetime import datetime <EOL> from flask import render_template , redirect , url_for , flash , abort , current_app , request , jsonify <EOL> from flask . ext . login import current_user , login_required <EOL> from . import main <EOL> from . . import db <EOL> from . forms import NoteForm , ShareForm , NotebookForm , SearchForm <EOL> from . . email import send_email <EOL> from . . models import User , Note , Tag , Notebook <EOL> @ main . route ( '<STR_LIT:/>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def index ( ) : <EOL> if current_user . is_authenticated ( ) : <EOL> notes = Note . query . filter_by ( <EOL> author_id = current_user . id , <EOL> is_deleted = False , is_archived = False ) . order_by ( <EOL> Note . is_favorite . desc ( ) , <EOL> Note . updated_date . desc ( ) ) . all ( ) <EOL> return render_template ( '<STR_LIT>' , notes = notes ) <EOL> else : <EOL> stats = [ ] <EOL> users = User . query . count ( ) <EOL> stats . append ( users ) <EOL> notes = Note . query . count ( ) <EOL> stats . append ( notes ) <EOL> return render_template ( '<STR_LIT>' , stats = stats ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def add ( ) : <EOL> if request . args . get ( '<STR_LIT>' ) : <EOL> notebook = Notebook . query . filter_by ( <EOL> id = int ( request . args . get ( '<STR_LIT>' ) ) ) . first ( ) <EOL> form = NoteForm ( notebook = notebook . id ) <EOL> else : <EOL> form = NoteForm ( ) <EOL> form . notebook . choices = [ <EOL> ( n . id , n . title ) for n in <EOL> Notebook . query . filter_by ( <EOL> author_id = current_user . id ) . all ( ) ] <EOL> if form . validate_on_submit ( ) : <EOL> note = Note ( <EOL> title = form . title . data , <EOL> body = form . body . data , <EOL> body_html = form . body_html . data , <EOL> notebook_id = form . notebook . data , <EOL> author = current_user . _get_current_object ( ) ) <EOL> db . session . add ( note ) <EOL> db . session . commit ( ) <EOL> tags = [ ] <EOL> if not len ( form . tags . data ) == <NUM_LIT:0> : <EOL> for tag in form . tags . data . split ( '<STR_LIT:U+002C>' ) : <EOL> tags . append ( tag . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> note . str_tags = ( tags ) <EOL> db . session . commit ( ) <EOL> return redirect ( url_for ( '<STR_LIT>' , id = note . notebook_id ) ) <EOL> return render_template ( '<STR_LIT>' , form = form ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def settings ( ) : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def trash ( ) : <EOL> if current_user . is_authenticated ( ) : <EOL> notes = Note . query . filter_by ( <EOL> author_id = current_user . id , <EOL> is_deleted = True ) . order_by ( <EOL> Note . updated_date . desc ( ) ) . all ( ) <EOL> if len ( notes ) == <NUM_LIT:0> : <EOL> flash ( "<STR_LIT>" ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , notes = notes ) <EOL> else : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def empty_trash ( ) : <EOL> notes = Note . query . filter_by ( <EOL> author_id = current_user . id , <EOL> is_deleted = True ) . all ( ) <EOL> for note in notes : <EOL> delete_forever ( note . id ) <EOL> flash ( "<STR_LIT>" ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def note ( id ) : <EOL> note = Note . query . get_or_404 ( id ) <EOL> if current_user != note . author : <EOL> abort ( <NUM_LIT> ) <EOL> return render_template ( '<STR_LIT>' , notes = [ note ] ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def edit ( id ) : <EOL> note = Note . query . get_or_404 ( id ) <EOL> if current_user != note . author : <EOL> abort ( <NUM_LIT> ) <EOL> form = NoteForm ( notebook = note . notebook_id ) <EOL> form . notebook . choices = [ <EOL> ( n . id , n . title ) for n in <EOL> Notebook . query . filter_by ( <EOL> author_id = current_user . id ) . all ( ) ] <EOL> if form . validate_on_submit ( ) : <EOL> note . title = form . title . data <EOL> note . body = form . body . data <EOL> note . body_html = form . body_html . data <EOL> note . notebook_id = form . notebook . data <EOL> note . updated_date = datetime . utcnow ( ) <EOL> db . session . add ( note ) <EOL> tags = [ ] <EOL> if not len ( form . tags . data ) == <NUM_LIT:0> : <EOL> for tag in form . tags . data . split ( '<STR_LIT:U+002C>' ) : <EOL> tags . append ( tag . replace ( "<STR_LIT:U+0020>" , "<STR_LIT>" ) ) <EOL> print form . tags . data <EOL> print tags <EOL> note . str_tags = ( tags ) <EOL> db . session . commit ( ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' , active_note = note . id ) ) <EOL> form . title . data = note . title <EOL> form . body . data = note . body <EOL> form . tags . data = '<STR_LIT:U+002CU+0020>' . join ( note . _get_tags ( ) ) <EOL> return render_template ( '<STR_LIT>' , note = note , form = form ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def delete ( id ) : <EOL> note = Note . query . get_or_404 ( id ) <EOL> if current_user != note . author : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> note . is_deleted = True <EOL> note . updated_date = datetime . utcnow ( ) <EOL> db . session . commit ( ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( request . referrer ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def delete_forever ( id ) : <EOL> note = Note . query . get_or_404 ( id ) <EOL> if current_user != note . author : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> db . session . delete ( note ) <EOL> db . session . commit ( ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( request . referrer ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def restore ( id ) : <EOL> note = Note . query . get_or_404 ( id ) <EOL> if current_user != note . author : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> note . is_deleted = False <EOL> note . updated_date = datetime . utcnow ( ) <EOL> db . session . commit ( ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( request . referrer ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def share ( id ) : <EOL> note = Note . query . get_or_404 ( id ) <EOL> if current_user != note . author : <EOL> abort ( <NUM_LIT> ) <EOL> form = ShareForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> send_email ( <EOL> form . recipient_email . data , '<STR_LIT>' <EOL> . format ( current_user . username ) , <EOL> '<STR_LIT>' , <EOL> user = current_user , note = note ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , form = form , notes = [ note ] ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def tag ( name ) : <EOL> tag = Tag . query . filter_by ( tag = name ) . first ( ) <EOL> return render_template ( '<STR_LIT>' , notes = tag . _get_notes ( ) , tag = name ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def notebooks ( ) : <EOL> form = NotebookForm ( ) <EOL> if form . validate_on_submit ( ) : <EOL> if Notebook . query . filter_by ( <EOL> title = form . title . data , <EOL> author_id = current_user . id ) . first ( ) is None : <EOL> notebook = Notebook ( <EOL> title = form . title . data , <EOL> author_id = current_user . id ) <EOL> db . session . add ( notebook ) <EOL> db . session . commit ( ) <EOL> else : <EOL> flash ( '<STR_LIT>' . format ( <EOL> form . title . data ) ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> notebooks = Notebook . query . filter_by ( <EOL> author_id = current_user . id , <EOL> is_deleted = False ) . all ( ) <EOL> return render_template ( <EOL> '<STR_LIT>' , <EOL> notebooks = notebooks , <EOL> form = form ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' ] ) <EOL> @ login_required <EOL> def favorites ( ) : <EOL> heading = "<STR_LIT>" <EOL> notes = Note . query . filter_by ( <EOL> author_id = current_user . id , <EOL> is_deleted = False , <EOL> is_favorite = True , is_archived = False ) . order_by ( <EOL> Note . updated_date . desc ( ) ) . all ( ) <EOL> if len ( notes ) == <NUM_LIT:0> : <EOL> flash ( "<STR_LIT>" ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , notes = notes , heading = heading ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def notebook ( id ) : <EOL> notebook = Notebook . query . filter_by ( id = id ) . first ( ) <EOL> if current_user != notebook . author : <EOL> abort ( <NUM_LIT> ) <EOL> return render_template ( <EOL> '<STR_LIT>' , <EOL> notebook = notebook , <EOL> notes = notebook . _show_notes ( ) ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def search ( ) : <EOL> form = SearchForm ( ) <EOL> if request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> query = request . args . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> results = Note . query . search ( query ) . filter_by ( author_id = current_user . id ) . order_by ( Note . updated_date . desc ( ) ) . all ( ) <EOL> if len ( results ) == <NUM_LIT:0> : <EOL> flash ( '<STR_LIT>' ) <EOL> return render_template ( <EOL> '<STR_LIT>' , <EOL> form = form , <EOL> notes = results ) <EOL> return render_template ( <EOL> '<STR_LIT>' , <EOL> form = form ) <EOL> @ main . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> @ login_required <EOL> def favorite ( id ) : <EOL> note = Note . query . get_or_404 ( id ) <EOL> if current_user != note . author : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> if not note . is_favorite : <EOL> note . is_favorite = True <EOL> note . updated_date = datetime . utcnow ( ) <EOL> db . session . commit ( ) <EOL> flash ( '<STR_LIT>' ) <EOL> else : <EOL> note . is_favorite = False <EOL> note . updated_date = datetime . utcnow ( ) <EOL> db . session . commit ( ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( request . referrer ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def view_archive ( ) : <EOL> if current_user . is_authenticated ( ) : <EOL> notes = Note . query . filter_by ( <EOL> author_id = current_user . id , <EOL> is_deleted = False , is_archived = True ) . order_by ( <EOL> Note . updated_date . desc ( ) ) . all ( ) <EOL> if len ( notes ) == <NUM_LIT:0> : <EOL> flash ( "<STR_LIT>" ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return render_template ( '<STR_LIT>' , notes = notes ) <EOL> else : <EOL> return render_template ( '<STR_LIT>' ) <EOL> @ main . route ( '<STR_LIT>' ) <EOL> @ login_required <EOL> def archive ( id ) : <EOL> note = Note . query . get_or_404 ( id ) <EOL> if current_user != note . author : <EOL> abort ( <NUM_LIT> ) <EOL> else : <EOL> note . is_archived = True <EOL> note . updated_date = datetime . utcnow ( ) <EOL> db . session . commit ( ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( request . referrer ) <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>' </s>
