Advertisement
Guest User

Untitled

a guest
Sep 20th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.03 KB | None | 0 0
  1. # all the imports
  2. from __future__ import with_statement
  3. from contextlib import closing
  4. import sqlite3
  5. from flask import Flask, request, session, g, redirect, url_for, \
  6.      abort, render_template, flash
  7.  
  8.  
  9.      
  10. # configuration
  11. DATABASE = 'C:\Users\Zach\Desktop\codechan\codechan\database\codechan.db'
  12. DEBUG = True
  13. SECRET_KEY = 'development key'
  14.  
  15.  
  16. app = Flask(__name__)
  17. app.config.from_object(__name__)
  18.  
  19. app.config.from_envvar('FLASKR_SETTINGS', silent=True)
  20.  
  21. def connect_db():
  22.     return sqlite3.connect(app.config['DATABASE'])
  23.    
  24. def init_db():
  25.     with closing(connect_db()) as db:
  26.         with app.open_resource('schema.sql') as f:
  27.             db.cursor().executescript(f.read())
  28.         db.commit()
  29.  
  30. @app.before_request
  31. def before_request():
  32.     g.db = connect_db()
  33.  
  34. @app.after_request
  35. def after_request(response):
  36.     g.db.close()
  37.     return response
  38.  
  39. @app.route('/')
  40. def show_entries():
  41.     cur = g.db.execute('select title, name, text from posts order by id desc')
  42.     entries = [dict(title=row[0], name=row[1], text=row[2]) for row in cur.fetchall()]
  43.     return render_template('show_entries.html', entries=entries)
  44.  
  45. @app.route('/replies/')
  46. def show_replies():
  47.     cur = g.db.execute('select name, text from replies order by replyid desc')
  48.     replies = [dict(name=row[0], text=row[1]) for row in cur.fetchall()]
  49.     return render_template('show_replies.html', replies=replies)
  50.  
  51. @app.route('/add', methods=['POST'])
  52. def add_entry():
  53.     g.db.execute('insert into posts (title, name, text) values (?, ?, ?)',
  54.                  [request.form['title'], request.form['name'], request.form['text']])
  55.     g.db.commit()
  56.     flash('New post was successfully posted')
  57.     return redirect(url_for('show_entries'))
  58.    
  59. @app.route('/reply/<int:postid>', methods=['POST'])
  60. def reply_to_entry():
  61.     g.db.execute('insert into replies (name, text) values(?, ?)', [request.form['name'], request.form['text']])
  62.     g.db.commit()
  63.     flash('Reply successfully posted')
  64.     return redirect(url_for('show_replies'))
  65.  
  66. if __name__ == '__main__':
  67.     app.run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement