From acd73bdf5f351445b4b66c28dd0e3a8883d4face Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Wed, 1 Apr 2015 21:30:26 -0400 Subject: Add edit comment feature This is handled in javascript. When clicking the edit link: 1. a form is shown to edit the RST source of the comment. 2. on submit, an AJAX POST request is sent to the new "comment" endpoint in the flask app. 3. the endpoint compiles the RST source, update the comment in the database and sends back formatted content to the client. 4. the client then updates the comment content and hides the form. --- famille.py | 18 +++++++++++++++++- static/famille.js | 20 +++++++++++++++++++- static/main.css | 15 ++++++++++++++- templates/news/show.html | 17 ++++++++++++++++- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/famille.py b/famille.py index c1cedd3..d958bcb 100644 --- a/famille.py +++ b/famille.py @@ -2,7 +2,7 @@ import sqlite3 #all the imports from flask import Flask, request, session, g, redirect,\ - url_for, render_template, flash + url_for, render_template, flash, jsonify from functools import wraps import hashlib from pytz import timezone @@ -203,6 +203,22 @@ def show_news(news_id): return redirect(url_for('show_news', news_id=news_id)) +@app.route('/comment/', methods=['POST']) +@login_required +def update_comment(comment_id): + comment = query_db("SELECT * FROM comments WHERE comments.id = ?", + (comment_id,), True) + if comment["user_id"] == session["user_id"]: + content = request.form['content'] + content_cache = rstify(content) + db = get_db() + db.execute("UPDATE comments set content=?, content_cache=? " + "WHERE comments.id=?", + (content, content_cache, comment_id)) + db.commit() + return jsonify(content=content, content_cache=content_cache) + + @app.route('/news//edit', methods=['GET', 'POST']) @login_required def edit_news(news_id): diff --git a/static/famille.js b/static/famille.js index b460392..3bb9e51 100644 --- a/static/famille.js +++ b/static/famille.js @@ -24,5 +24,23 @@ $(document).ready(function(){ }); $('span.timeago').timeago(); var addr = $(location).attr('pathname'); - $("#"+addr.replace(/\//g,"")).addClass("active") + $("#"+addr.replace(/\//g,"")).addClass("active"); + + $(".edit").click(function(e){ + e.preventDefault(); + $(this).parents(".media-heading").siblings(".comment-content").hide(); + $(this).parents(".media-heading").siblings(".comment-source").show(); + }); + + $(".comment-source").submit(function(e){ + e.preventDefault(); + var comment_id = $(this).data("id"); + var form = $(this); + $.post("/comment/" + comment_id, {content: form.find("textarea").val()}, function(data) { + var comment_content = form.parents(".media-body").find(".comment-content"); + comment_content.html(data.content_cache); + comment_content.show(); + form.hide(); + }); + }); }); diff --git a/static/main.css b/static/main.css index 5ae7b30..88598a7 100644 --- a/static/main.css +++ b/static/main.css @@ -13,6 +13,19 @@ font-size: 14px; } +.media-body .edit { + display: none; + margin-right: 1em; +} + +.media-body:hover .edit { + display: initial; +} + +.comment-source { + display: none; +} + article > header { margin-bottom: 2em; } @@ -44,4 +57,4 @@ p.kadoscope{ .media-left img{ width: 64px; -} \ No newline at end of file +} diff --git a/templates/news/show.html b/templates/news/show.html index 3bdaa9b..8667f5d 100644 --- a/templates/news/show.html +++ b/templates/news/show.html @@ -21,9 +21,24 @@ 'gfx/'+comment.user_name+'.jpg') }}" alt="{{comment.user_name}}" class="img-circle">
-

{{comment.date|format_date}}

+

+ {% if news.user_name==session.user_name %} + Modifier + {% endif %} + {{comment.date|format_date}} +

{{comment.user_name}}

+
{{comment.content_cache|safe}} +
+ {% if news.user_name==session.user_name %} +
+
+ +
+ +
+ {% endif %}
{% endfor %} -- cgit v1.2.3-70-g09d2