From 2b3f0a748a1cb194168e25b0fa78979adae4622b Mon Sep 17 00:00:00 2001 From: Noah Petherbridge Date: Tue, 29 Apr 2014 21:40:40 -0700 Subject: [PATCH] Add editable preview screen for leaving comments --- rophako/modules/comment.py | 73 ++++++++++++++---------------- rophako/www/comment/form.inc.html | 65 ++++++++++++++++++++++++++ rophako/www/comment/index.inc.html | 58 +----------------------- rophako/www/comment/preview.html | 32 +++++++------ 4 files changed, 117 insertions(+), 111 deletions(-) create mode 100644 rophako/www/comment/form.inc.html diff --git a/rophako/modules/comment.py b/rophako/modules/comment.py index bffaa32..fae494f 100644 --- a/rophako/modules/comment.py +++ b/rophako/modules/comment.py @@ -23,7 +23,8 @@ def index(): @mod.route("/preview", methods=["POST"]) def preview(): # Get the form fields. - form = get_comment_form(request.form) + form = get_comment_form(request.form) + thread = sanitize_name(form["thread"]) # Trap fields. trap1 = request.form.get("website", "x") != "http://" @@ -37,51 +38,42 @@ def preview(): flash("You must provide a message with your comment.") return redirect(form["url"]) + # Gravatar? + gravatar = Comment.gravatar(form["contact"]) + + # Are they submitting? + if form["action"] == "submit": + Comment.add_comment( + thread=thread, + uid=g.info["session"]["uid"], + ip=request.remote_addr, + time=int(time.time()), + image=gravatar, + name=form["name"], + subject=form["subject"], + message=form["message"], + url=form["url"], + ) + + # Are we subscribing to the thread? + if form["subscribe"] == "true": + email = form["contact"] + if "@" in email: + Comment.add_subscriber(thread, email) + flash("You have been subscribed to future comments on this page.") + + flash("Your comment has been added!") + return redirect(form["url"]) + # Gravatar. - g.info["gravatar"] = Comment.gravatar(form.get("contact", "")) - g.info["preview"] = Comment.format_message(form.get("message", "")) + g.info["gravatar"] = gravatar + g.info["preview"] = Comment.format_message(form["message"]) + g.info["pretty_time"] = pretty_time(COMMENT_TIME_FORMAT, time.time()) g.info.update(form) return template("comment/preview.html") -@mod.route("/post", methods=["POST"]) -def post(): - # Get the form fields. - form = get_comment_form(request.form) - thread = sanitize_name(form["thread"]) - - # Gravatar? - gravatar = Comment.gravatar(form.get("contact")) - - # Validate things. - if len(form["message"]) == 0: - flash("You must provide a message with your comment.") - return redirect(form["url"]) - - Comment.add_comment( - thread=thread, - uid=g.info["session"]["uid"], - ip=request.remote_addr, - time=int(time.time()), - image=gravatar, - name=form["name"], - subject=form["subject"], - message=form["message"], - url=form["url"], - ) - - # Are we subscribing to the thread? - if form.get("subscribe", "false") == "true": - email = form.get("contact", "") - if "@" in email: - Comment.add_subscriber(thread, email) - flash("You have been subscribed to future comments on this page.") - - flash("Your comment has been added!") - return redirect(form["url"]) - - @mod.route("/delete//") @login_required def delete(thread, cid): @@ -175,6 +167,7 @@ def partial_index(thread, subject, header=True): def get_comment_form(form): return dict( + action = request.form.get("action", ""), thread = request.form.get("thread", ""), url = request.form.get("url", ""), subject = request.form.get("subject", "[No Subject]"), diff --git a/rophako/www/comment/form.inc.html b/rophako/www/comment/form.inc.html new file mode 100644 index 0000000..2e962b7 --- /dev/null +++ b/rophako/www/comment/form.inc.html @@ -0,0 +1,65 @@ +{# Common template for leaving a comment/updating the comment preview. #} + +
+ + + + + + + + + + + + + + + + + + + + +
+ Your name: + + {% if session["login"] %} + {{ session["name"] }} + {% else %} + + {% endif %} +
+ Your Email: + + + (optional) +
+ Message: + +
+ Comments can be formatted with Markdown, + and you can use
emoticons + in your comment.
+
+ +

+ +

+ If you can see this, don't touch the following fields.
+
+ +
+ +{% if preview %} + + +{% else %} + +{% endif %} + +
\ No newline at end of file diff --git a/rophako/www/comment/index.inc.html b/rophako/www/comment/index.inc.html index 1bcfc6d..96a4bb0 100644 --- a/rophako/www/comment/index.inc.html +++ b/rophako/www/comment/index.inc.html @@ -34,60 +34,4 @@ There {% if comments|length == 1 %}is{% else %}are{% endif %}

Add a Comment

-
- - - - - - - - - - - - - - - - - - - - -
- Your name: - - {% if session["login"] %} - {{ session["name"] }} - {% else %} - - {% endif %} -
- Your Email: - - - (optional) -
- Message: - -
- Comments can be formatted with Markdown, - and you can use
emoticons - in your comment.
-
- -

- -

- If you can see this, don't touch the following fields.
-
- -
- - -
+{% include "comment/form.inc.html" %} \ No newline at end of file diff --git a/rophako/www/comment/preview.html b/rophako/www/comment/preview.html index 47563e0..16b95b2 100644 --- a/rophako/www/comment/preview.html +++ b/rophako/www/comment/preview.html @@ -6,27 +6,31 @@ This is a preview of what your comment is going to look like once posted.

-


+

+
+ {% if contact %} + Avatar + {% else %} + guest + {% endif %}
+ {% if session["login"] %}{{ session["username"] }}{% else %}guest{% endif %} +
-{{ preview|safe }}

+ Posted on {{ pretty_time }} by {{ name or "Anonymous" }}.

-


+ {{ preview|safe }} + +

+
{% if subscribe == "true" and contact %} +

You will be subscribed to future comments on this thread. Notification e-mails will be sent to {{ contact }}.

{% endif %} -

- - - - - - - - - -
+

Edit Comment

+ +{% include "comment/form.inc.html" %} {% endblock %} \ No newline at end of file