Beispiel #1
0
def do_comment(request, post, attrs, all_comments=None):
    # make sure the form came through correctly
    if not ("name" in attrs and "text" in attrs and "email" in attrs and "lastname" in attrs):
        return False
    # 'lastname' is a honeypot field
    if not attrs["lastname"] == "":
        return False
    # keyword parameter is for prefetching
    if all_comments is None:
        all_comments = list(post.comments.all())
    else:
        all_comments = all_comments[:]  # copy so we don't mutate later
    ### create a new comment record
    comment = Comment()
    comment.post = post
    comment.name = attrs["name"].strip()
    if len(comment.name) == 0:
        comment.name = "Anonymous"
    comment.text = attrs["text"]
    comment.email = attrs["email"]
    ### check for spam (requires a web request to Akismet)
    is_spam = akismet_check(request, comment)
    if is_spam:
        return False  # don't even save spam comments
    comment.spam = False

    ### set the comment's parent if necessary
    if "parent" in attrs and attrs["parent"] != "":
        comment.parent_id = int(attrs["parent"])

    if isLegitEmail(comment.email):
        comment.subscribed = attrs.get("subscribed", False)
    else:
        comment.subscribed = False
        # make sure comments under the same name have a consistent gravatar
        comment.email = hashlib.sha1(comment.name.encode("utf-8")).hexdigest()
    comment.save()
    all_comments.append(comment)
    ### send out notification emails
    emails = {}
    for c in all_comments:
        if c.subscribed and c.email != comment.email and isLegitEmail(c.email):
            emails[c.email] = c.name
    for name, email in settings.MANAGERS:
        emails[email] = name
    template = get_template("comment_email.html")
    subject = "Someone replied to your comment"
    for email, name in emails.iteritems():
        text = template.render(Context({"comment": comment, "email": email, "name": name}))
        msg = EmailMessage(subject, text, "*****@*****.**", [email])
        msg.content_subtype = "html"
        msg.send()
    return True