Example #1
0
def edit(thread, cid):
    """Edit an existing comment."""
    url = request.args.get("url")
    comment = Comment.get_comment(thread, cid)
    if not comment:
        flash("The comment wasn't found!")
        return redirect(url or url_for("index"))

    # Submitting?
    if request.method == "POST":
        action  = request.form.get("action")
        message = request.form.get("message")
        url     = request.form.get("url") # Preserve the URL!
        if len(message) == 0:
            flash("The comment must have a message!")
            return redirect(url_for(".edit", thread=thread, cid=cid, url=url))

        # Update the real comment data with the submitted message (for preview),
        # if they clicked Save it will then be saved back to disk.
        comment["message"] = message

        if action == "save":
            # Saving the changes!
            Comment.update_comment(thread, cid, comment)
            flash("Comment updated successfully!")
            return redirect(url or url_for("index"))

    # Render the Markdown.
    comment["formatted_message"] = Comment.format_message(comment["message"])

    g.info["thread"]  = thread
    g.info["cid"]     = cid
    g.info["comment"] = comment
    g.info["url"]     = url or ""
    return template("comment/edit.html")
Example #2
0
def edit(thread, cid):
    """Edit an existing comment."""
    url = request.args.get("url")
    comment = Comment.get_comment(thread, cid)
    if not comment:
        flash("The comment wasn't found!")
        return redirect(url or url_for("index"))

    # Submitting?
    if request.method == "POST":
        action = request.form.get("action")
        message = request.form.get("message")
        url = request.form.get("url")  # Preserve the URL!
        if len(message) == 0:
            flash("The comment must have a message!")
            return redirect(url_for(".edit", thread=thread, cid=cid, url=url))

        # Update the real comment data with the submitted message (for preview),
        # if they clicked Save it will then be saved back to disk.
        comment["message"] = message

        if action == "save":
            # Saving the changes!
            Comment.update_comment(thread, cid, comment)
            flash("Comment updated successfully!")
            return redirect(url or url_for("index"))

    # Render the Markdown.
    comment["formatted_message"] = Comment.format_message(comment["message"])

    g.info["thread"] = thread
    g.info["cid"] = cid
    g.info["comment"] = comment
    g.info["url"] = url or ""
    return template("comment/edit.html")
Example #3
0
def preview():
    # Get the form fields.
    form = get_comment_form(request.form)
    thread = sanitize_name(form["thread"])

    # Trap fields.
    trap1 = request.form.get("website", "x") != "http://"
    trap2 = request.form.get("email", "x") != ""
    if trap1 or trap2:
        flash("Wanna try that again?")
        return redirect(url_for("index"))

    # Validate things.
    if len(form["message"]) == 0:
        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=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"] = gravatar
    g.info["preview"] = Comment.format_message(form["message"])
    g.info["pretty_time"] = pretty_time(Config.comment.time_format,
                                        time.time())

    g.info.update(form)
    return template("comment/preview.html")
Example #4
0
def preview():
    # Get the form fields.
    form   = get_comment_form(request.form)
    thread = sanitize_name(form["thread"])

    # Trap fields.
    trap1 = request.form.get("website", "x") != "http://"
    trap2 = request.form.get("email", "x") != ""
    if trap1 or trap2:
        flash("Wanna try that again?")
        return redirect(url_for("index"))

    # Validate things.
    if len(form["message"]) == 0:
        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=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"]    = gravatar
    g.info["preview"]     = Comment.format_message(form["message"])
    g.info["pretty_time"] = pretty_time(Config.comment.time_format, time.time())

    g.info.update(form)
    return template("comment/preview.html")
Example #5
0
def partial_index(thread, subject, header=True, addable=True):
    """Partial template for including the index view of a comment thread.

    Parameters:
        thread (str): the unique name for the comment thread.
        subject (str): subject name for the comment thread.
        header (bool): show the 'Comments' H1 header.
        addable (bool): can new comments be added to the thread?
    """

    # Get all the comments on this thread.
    comments = Comment.get_comments(thread)

    # Sort the comments by most recent on bottom.
    sorted_cids = [ x for x in sorted(comments, key=lambda y: comments[y]["time"]) ]
    sorted_comments = []
    for cid in sorted_cids:
        comment = comments[cid]
        comment["id"] = cid

        # Was the commenter logged in?
        if comment["uid"] > 0:
            user = User.get_user(uid=comment["uid"])
            avatar = User.get_picture(uid=comment["uid"])
            comment["name"] = user["name"]
            comment["username"] = user["username"]
            comment["image"] = avatar

        # Add the pretty time.
        comment["pretty_time"] = pretty_time(Config.comment.time_format, comment["time"])

        # Format the message for display.
        comment["formatted_message"] = Comment.format_message(comment["message"])

        # Was this comment posted by the current user viewing it?
        comment["editable"] = Comment.is_editable(thread, cid, comment)

        sorted_comments.append(comment)

    g.info["header"] = header
    g.info["thread"] = thread
    g.info["subject"] = subject
    g.info["commenting_disabled"] = not addable
    g.info["url"] = request.url
    g.info["comments"] = sorted_comments
    g.info["photo_url"] = Config.photo.root_public
    return template("comment/index.inc.html")
Example #6
0
def partial_index(thread, subject, header=True, addable=True):
    """Partial template for including the index view of a comment thread.

    * thread: unique name for the comment thread
    * subject: subject name for the comment thread
    * header: show the Comments h1 header
    * addable: boolean, can new comments be added to the thread"""

    comments = Comment.get_comments(thread)

    # Sort the comments by most recent on bottom.
    sorted_cids = [
        x for x in sorted(comments, key=lambda y: comments[y]["time"])
    ]
    sorted_comments = []
    for cid in sorted_cids:
        comment = comments[cid]
        comment["id"] = cid

        # Was the commenter logged in?
        if comment["uid"] > 0:
            user = User.get_user(uid=comment["uid"])
            avatar = User.get_picture(uid=comment["uid"])
            comment["name"] = user["name"]
            comment["username"] = user["username"]
            comment["image"] = avatar

        # Add the pretty time.
        comment["pretty_time"] = pretty_time(Config.comment.time_format,
                                             comment["time"])

        # Format the message for display.
        comment["formatted_message"] = Comment.format_message(
            comment["message"])

        sorted_comments.append(comment)

    g.info["header"] = header
    g.info["thread"] = thread
    g.info["subject"] = subject
    g.info["commenting_disabled"] = not addable
    g.info["url"] = request.url
    g.info["comments"] = sorted_comments
    g.info["photo_url"] = Config.photo.root_public
    return template("comment/index.inc.html")
Example #7
0
def partial_index(thread, subject, header=True, addable=True):
    """Partial template for including the index view of a comment thread.

    * thread: unique name for the comment thread
    * subject: subject name for the comment thread
    * header: show the Comments h1 header
    * addable: boolean, can new comments be added to the thread"""

    comments = Comment.get_comments(thread)

    # Sort the comments by most recent on bottom.
    sorted_cids = [ x for x in sorted(comments, key=lambda y: comments[y]["time"]) ]
    sorted_comments = []
    for cid in sorted_cids:
        comment = comments[cid]
        comment["id"] = cid

        # Was the commenter logged in?
        if comment["uid"] > 0:
            user = User.get_user(uid=comment["uid"])
            avatar = User.get_picture(uid=comment["uid"])
            comment["name"] = user["name"]
            comment["username"] = user["username"]
            comment["image"] = avatar

        # Add the pretty time.
        comment["pretty_time"] = pretty_time(Config.comment.time_format, comment["time"])

        # Format the message for display.
        comment["formatted_message"] = Comment.format_message(comment["message"])

        sorted_comments.append(comment)

    g.info["header"] = header
    g.info["thread"] = thread
    g.info["subject"] = subject
    g.info["commenting_disabled"] = not addable
    g.info["url"] = request.url
    g.info["comments"] = sorted_comments
    g.info["photo_url"] = Config.photo.root_public
    return template("comment/index.inc.html")