From 1225be7b79ed98873a51729635e4b18f622da3da Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Wed, 7 Nov 2012 17:37:06 +0100 Subject: Add comments feature: show list, add --- famille.py | 15 +++++++++++++-- schema.sql | 11 +++++++++++ templates/news/show.html | 8 +++----- 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/famille.py b/famille.py index 237a122..4a8cb73 100644 --- a/famille.py +++ b/famille.py @@ -56,12 +56,23 @@ def add_news(): elif request.method == 'GET': return render_template("news/add.html") -@app.route('/news//') +@app.route('/news//', methods=['GET', 'POST']) @login_required def show_news(news_id): news = query_db("SELECT * FROM news LEFT JOIN users ON news.user_id = users.id " "WHERE news.id = ?", (news_id,), True) - return render_template("news/show.html", news=news) + if request.method == 'GET': + comments = query_db("SELECT * FROM comments LEFT JOIN users " + "ON comments.user_id = users.id " + "WHERE comments.news_id = ?", (news_id,)) + return render_template("news/show.html", news=news, comments=comments) + elif request.method == 'POST': + user_id = session['user_id'] + content = request.form['content'] + g.db.execute("INSERT INTO comments ('user_id', 'content', 'news_id') " + "VALUES (?, ?, ?)", (user_id, content, news_id)) + g.db.commit() + return redirect(url_for('show_news', news_id=news_id)) @app.route('/login/', methods=['GET', 'POST']) def login(): diff --git a/schema.sql b/schema.sql index 2119322..43e4822 100644 --- a/schema.sql +++ b/schema.sql @@ -14,3 +14,14 @@ create table news ( user_id integer, FOREIGN KEY(user_id) REFERENCES users(id) ); + +drop table if exists comments; +create table comments ( + id integer primary key autoincrement, + date string default (strftime('%Y-%m-%dT%H:%M:%S','now')), + content string not null, + user_id integer, + news_id integer, + FOREIGN KEY(user_id) REFERENCES users(id), + FOREIGN KEY(news_id) REFERENCES news(id) +); diff --git a/templates/news/show.html b/templates/news/show.html index da188fe..c7795cd 100644 --- a/templates/news/show.html +++ b/templates/news/show.html @@ -17,23 +17,21 @@
-
{% endblock %} -- cgit v1.2.3-70-g09d2