diff options
| author | Guillaume Horel <guillaume.horel@gmail.com> | 2012-11-07 23:38:09 -0500 |
|---|---|---|
| committer | Guillaume Horel <guillaume.horel@gmail.com> | 2012-11-07 23:38:09 -0500 |
| commit | 9b2f907dfffd92b6f236d73cc5d0c3b9fb92cc53 (patch) | |
| tree | 00a0fdbd6506bad1eb9e2c12e90284e6e751504f | |
| parent | d7e915f77c42f6d0c5a85e03d87106c27082ec99 (diff) | |
| download | famille-flask-9b2f907dfffd92b6f236d73cc5d0c3b9fb92cc53.tar.gz | |
fixed up the comment system
| -rw-r--r-- | famille.py | 13 | ||||
| -rw-r--r-- | templates/news/list.html | 10 | ||||
| -rw-r--r-- | templates/news/show.html | 5 |
3 files changed, 23 insertions, 5 deletions
@@ -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 @@ </h2> {% if news.user_name==session.user_name %} <div style="float:right"> - <a href="{{ url_for('edit_news', news_id = news.id)}}">Modifier cette actualité</a> + <a href="{{url_for('edit_news', news_id = news.id)}}">Modifier cette actualité</a> </div> {% endif %} - <p>Posté par <a href="{{ url_for('view_user', user_id = news.user_id)}}">{{ news.user_name }}</a> le {{news.date|format_date}}</p> + <p>Posté par <a href="{{url_for('view_user', user_id = news.user_id)}}">{{ news.user_name }}</a> le {{news.date|format_date}}</p> </div> + <p class="comments"> + <a href="{{url_for('show_news', news_id = news.id)}}#comments">{{news.ncomments|pluralize}}</a>| + <a href="{{url_for('show_news', news_id = news.id)}}#add">Ajouter un commentaire</a> +</p> <div class="news_content"> - {{ news.content_cache|safe }} + <p>{{ news.content_cache|safe }}</p> </div> {% endfor %} </div> 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 @@ <hr/> <h2 id="comments">Commentaires</h2> +{% if not comments %} +Pas de commentaires pour l'instant. +{% endif %} {% for comment in comments %} <h3 class="comment"> - Posté par <a href="{{url_for('view_user', comment.user_name)}}">{{comment.user_name}}</a> le {{comment.date|format_date}} + Posté par <a href="{{url_for('view_user', user_id=comment.user_id)}}">{{comment.user_name}}</a> le {{comment.date|format_date}} </h3> <div class="comment_content"/> {{comment.content_cache|safe}} |
