aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2012-11-07 17:37:06 +0100
committerThibaut Horel <thibaut.horel@gmail.com>2012-11-07 17:37:06 +0100
commit1225be7b79ed98873a51729635e4b18f622da3da (patch)
treec24ffbcce1239fcfe5d66bd1908f7a185573b675
parenta3a62275ac16c4a887c6d054508d26d04a7d8558 (diff)
downloadfamille-flask-1225be7b79ed98873a51729635e4b18f622da3da.tar.gz
Add comments feature: show list, add
-rw-r--r--famille.py15
-rw-r--r--schema.sql11
-rw-r--r--templates/news/show.html8
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/<int:news_id>/')
+@app.route('/news/<int:news_id>/', 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 @@
</div>
<hr/>
-<!--
<h2 id="comments">Commentaires</h2>
{% for comment in comments %}
-<h3 class="comment">Posté par <a href="/user/view/name/<?=$comment['name']?>">{{comment.author}}</a> le {{comment.date}} </h3>
+<h3 class="comment">Posté par <a href="">{{comment.name}}</a> le {{comment.date}} </h3>
<div class="comment_content"/>
-{{comment.text}}
+{{comment.content}}
</div>
{% endfor %}
<h2 id="add">Ajouter un commentaire</h2>
-<form class="comment_add" method="post" action="/home/view/id/<?=$this->news['id']?>/#add">
+<form class="comment_add" method="post" action="{{url_for('show_news', news_id=news.id)}}">
<textarea name="content" cols="90" rows="10">
</textarea>
<p class="submit"><input type="submit" value="Ajouter"/></p>
--->
</div>
<div style="clear:both"/>
{% endblock %}