Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # all the imports
- from __future__ import with_statement
- from contextlib import closing
- import sqlite3
- from flask import Flask, request, session, g, redirect, url_for, \
- abort, render_template, flash
- # configuration
- DATABASE = 'C:\Users\Zach\Desktop\codechan\codechan\database\codechan.db'
- DEBUG = True
- SECRET_KEY = 'development key'
- app = Flask(__name__)
- app.config.from_object(__name__)
- app.config.from_envvar('FLASKR_SETTINGS', silent=True)
- def connect_db():
- return sqlite3.connect(app.config['DATABASE'])
- def init_db():
- with closing(connect_db()) as db:
- with app.open_resource('schema.sql') as f:
- db.cursor().executescript(f.read())
- db.commit()
- @app.before_request
- def before_request():
- g.db = connect_db()
- @app.after_request
- def after_request(response):
- g.db.close()
- return response
- @app.route('/')
- def show_entries():
- cur = g.db.execute('select title, name, text from posts order by id desc')
- entries = [dict(title=row[0], name=row[1], text=row[2]) for row in cur.fetchall()]
- return render_template('show_entries.html', entries=entries)
- @app.route('/replies/')
- def show_replies():
- cur = g.db.execute('select name, text from replies order by replyid desc')
- replies = [dict(name=row[0], text=row[1]) for row in cur.fetchall()]
- return render_template('show_replies.html', replies=replies)
- @app.route('/add', methods=['POST'])
- def add_entry():
- g.db.execute('insert into posts (title, name, text) values (?, ?, ?)',
- [request.form['title'], request.form['name'], request.form['text']])
- g.db.commit()
- flash('New post was successfully posted')
- return redirect(url_for('show_entries'))
- @app.route('/reply/<int:postid>', methods=['POST'])
- def reply_to_entry():
- g.db.execute('insert into replies (name, text) values(?, ?)', [request.form['name'], request.form['text']])
- g.db.commit()
- flash('Reply successfully posted')
- return redirect(url_for('show_replies'))
- if __name__ == '__main__':
- app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement