-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
95 lines (76 loc) · 2.48 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import os
import os.path as op
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask import request, url_for, redirect, render_template
from forms.post_form import PostForm
# Create application
app = Flask(__name__)
# Create dummy secrey key so we can use sessions
app.config['SECRET_KEY'] = '123456790'
# Create in-memory database
app.config['DATABASE_FILE'] = 'sample_db.sqlite'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + app.config['DATABASE_FILE']
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
# Create models
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(120))
text = db.Column(db.Text, nullable=False)
def __init(self, title=None, text=None):
self.title = title
self.text = text
def __unicode__(self):
return self.title
@app.route('/')
def index():
posts = Post.query.all()
return render_template('list.html',
posts=posts)
@app.route('/posts/new', methods=('GET', 'POST'))
def new_post():
form = PostForm(request.form)
data = form.data
if form.validate_on_submit():
post = Post(**data)
db.session.add(post)
db.session.commit()
return redirect(url_for('.index'))
return render_template('create.html',
form=form)
@app.route('/posts/<post_id>/edit', methods=('GET', 'POST'))
def edit_post(post_id):
post = Post.query.get(post_id)
form = PostForm(obj=post)
data = form.data
if form.validate_on_submit():
db.session.query(Post)\
.filter_by(id=post.id)\
.update(dict(data))
db.session.commit()
return redirect(url_for('.index'))
return render_template('create.html',
form=form)
@app.route('/posts/<post_id>/delete', methods=('GET', 'POST'))
def delete_post(post_id):
post = Post.query.get(post_id)
db.session.delete(post)
db.session.commit()
return redirect(url_for('.index'))
def build_sample_db():
"""
Populate a small db with some example entries.
"""
db.drop_all()
db.create_all()
db.session.commit()
return
if __name__ == '__main__':
# Build a sample db on the fly, if one does not exist yet.
app_dir = op.realpath(os.path.dirname(__file__))
database_path = op.join(app_dir, app.config['DATABASE_FILE'])
if not os.path.exists(database_path):
build_sample_db()
# Start app
app.run(debug=True)