From 9b2f907dfffd92b6f236d73cc5d0c3b9fb92cc53 Mon Sep 17 00:00:00 2001 From: Guillaume Horel Date: Wed, 7 Nov 2012 23:38:09 -0500 Subject: fixed up the comment system --- famille.py | 13 ++++++++++++- templates/news/list.html | 10 +++++++--- templates/news/show.html | 5 ++++- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/famille.py b/famille.py index bda0440..d026bfe 100644 --- a/famille.py +++ b/famille.py @@ -25,6 +25,15 @@ def format_date(datetime_string, format="%a %d %b %Y"): return datetime.strptime(datetime_string[:-1], "%Y-%m-%dT%H:%M:%S").strftime(format) +@app.template_filter('pluralize') +def pluralize(ncomments): + if ncomments==0: + return "Aucun Commentaire" + elif ncomments==1: + return "1 Commentaire" + else: + return "{0} Commentaires".format(ncomments) + def query_db(query, args=(), one=False): cur = g.db.execute(query, args) rv = cur.fetchone() if one else cur.fetchall() @@ -54,7 +63,9 @@ def login_required(f): @app.route('/news/') @login_required def list_news(): - news = query_db("SELECT * FROM news LEFT JOIN users ON news.user_id = users.id") + news = query_db("SELECT news.*, users.user_name, count(comments.id) as ncomments FROM news " + "LEFT JOIN users ON news.user_id = users.id " + "LEFT JOIN comments ON news.id=comments.news_id GROUP BY news.id") return render_template("news/list.html", news=news) @app.route('/news/add/', methods=['GET', 'POST']) diff --git a/templates/news/list.html b/templates/news/list.html index c7ea740..b2bee8a 100644 --- a/templates/news/list.html +++ b/templates/news/list.html @@ -11,13 +11,17 @@ {% if news.user_name==session.user_name %}
- Modifier cette actualité + Modifier cette actualité
{% endif %} -

Posté par {{ news.user_name }} le {{news.date|format_date}}

+

Posté par {{ news.user_name }} le {{news.date|format_date}}

+

+ {{news.ncomments|pluralize}}| + Ajouter un commentaire +

- {{ news.content_cache|safe }} +

{{ news.content_cache|safe }}

{% endfor %} diff --git a/templates/news/show.html b/templates/news/show.html index 782b0a4..dcc4e4a 100644 --- a/templates/news/show.html +++ b/templates/news/show.html @@ -18,9 +18,12 @@

Commentaires

+{% if not comments %} +Pas de commentaires pour l'instant. +{% endif %} {% for comment in comments %}

- Posté par {{comment.user_name}} le {{comment.date|format_date}} + Posté par {{comment.user_name}} le {{comment.date|format_date}}

{{comment.content_cache|safe}} -- cgit v1.3