<s> """<STR_LIT>""" <EOL> from __future__ import with_statement <EOL> from sqlite3 import dbapi2 as sqlite3 <EOL> from contextlib import closing <EOL> from flask import Flask , request , session , g , redirect , url_for , abort , render_template , flash <EOL> from flask . ext . mako import MakoTemplates <EOL> DATABASE = '<STR_LIT>' <EOL> DEBUG = True <EOL> SECRET_KEY = '<STR_LIT>' <EOL> USERNAME = '<STR_LIT>' <EOL> PASSWORD = '<STR_LIT:default>' <EOL> MAKO_TEMPLATE_DIR = '<STR_LIT>' <EOL> app = Flask ( __name__ ) <EOL> app . config . from_object ( __name__ ) <EOL> app . config . from_envvar ( '<STR_LIT>' , silent = True ) <EOL> mako = MakoTemplates ( app ) <EOL> def connect_db ( ) : <EOL> """<STR_LIT>""" <EOL> return sqlite3 . connect ( app . config [ '<STR_LIT>' ] ) <EOL> def init_db ( ) : <EOL> """<STR_LIT>""" <EOL> with closing ( connect_db ( ) ) as db : <EOL> with app . open_resource ( '<STR_LIT>' ) as f : <EOL> db . cursor ( ) . executescript ( f . read ( ) ) <EOL> db . commit ( ) <EOL> @ app . before_request <EOL> def before_request ( ) : <EOL> """<STR_LIT>""" <EOL> g . db = connect_db ( ) <EOL> @ app . teardown_request <EOL> def teardown_request ( exception ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( g , '<STR_LIT>' ) : <EOL> g . db . close ( ) <EOL> @ app . route ( '<STR_LIT:/>' ) <EOL> def show_entries ( ) : <EOL> cur = g . db . execute ( '<STR_LIT>' ) <EOL> entries = [ dict ( title = row [ <NUM_LIT:0> ] , text = row [ <NUM_LIT:1> ] ) for row in cur . fetchall ( ) ] <EOL> return mako . render ( '<STR_LIT>' , entries = entries ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> def add_entry ( ) : <EOL> if not session . get ( '<STR_LIT>' ) : <EOL> abort ( <NUM_LIT> ) <EOL> g . db . execute ( '<STR_LIT>' , <EOL> [ request . form [ '<STR_LIT:title>' ] , request . form [ '<STR_LIT:text>' ] ] ) <EOL> g . db . commit ( ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> @ app . route ( '<STR_LIT>' , methods = [ '<STR_LIT:GET>' , '<STR_LIT:POST>' ] ) <EOL> def login ( ) : <EOL> error = None <EOL> if request . method == '<STR_LIT:POST>' : <EOL> if request . form [ '<STR_LIT:username>' ] != app . config [ '<STR_LIT>' ] : <EOL> error = '<STR_LIT>' <EOL> elif request . form [ '<STR_LIT:password>' ] != app . config [ '<STR_LIT>' ] : <EOL> error = '<STR_LIT>' <EOL> else : <EOL> session [ '<STR_LIT>' ] = True <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return mako . render ( '<STR_LIT>' , error = error ) <EOL> @ app . route ( '<STR_LIT>' ) <EOL> def logout ( ) : <EOL> session . pop ( '<STR_LIT>' , None ) <EOL> flash ( '<STR_LIT>' ) <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> app . run ( ) </s>
