You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
stacosys/app/interface/form.py

72 lines
1.9 KiB
Python

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import logging
6 years ago
from datetime import datetime
from flask import request, abort, redirect
from core import app
from models.site import Site
from models.comment import Comment
from helpers.hashing import md5
logger = logging.getLogger(__name__)
6 years ago
@app.route("/newcomment", methods=["POST"])
def new_form_comment():
try:
data = request.form
7 years ago
# add client IP if provided by HTTP proxy
6 years ago
ip = ""
if "X-Forwarded-For" in request.headers:
ip = request.headers["X-Forwarded-For"]
# log
logger.info(data)
# validate token: retrieve site entity
6 years ago
token = data.get("token", "")
site = Site.select().where(Site.token == token).get()
if site is None:
6 years ago
logger.warn("Unknown site %s" % token)
abort(400)
# honeypot for spammers
6 years ago
captcha = data.get("captcha", "")
if captcha:
6 years ago
logger.warn("discard spam: data %s" % data)
abort(400)
6 years ago
url = data.get("url", "")
author_name = data.get("author", "").strip()
author_gravatar = data.get("email", "").strip()
author_site = data.get("site", "").to_lower().strip()
if author_site and author_site[:4] != "http":
author_site = "http://" + author_site
message = data.get("message", "")
created = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# add a row to Comment table
comment = Comment(
site=site,
url=url,
author_name=author_name,
author_site=author_site,
author_gravatar=author_gravatar,
content=message,
created=created,
notified=None,
published=None,
ip=ip,
)
comment.save()
except:
logger.exception("new comment failure")
abort(400)
6 years ago
return redirect("/redirect/", code=302)