clean-up code
This commit is contained in:
parent
0e427e08b1
commit
fc958e7fb9
11 changed files with 274 additions and 91 deletions
|
|
@ -1 +0,0 @@
|
|||
__version__ = '0.1.0'
|
||||
|
|
@ -56,9 +56,8 @@ def submit_new_comment():
|
|||
mailer.send(site.admin_email, subject, email_body)
|
||||
logger.debug("new comment processed ")
|
||||
|
||||
# update comment
|
||||
comment.notified = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
comment.save()
|
||||
# notify site admin and save notification datetime
|
||||
comment.notify_site_admin()
|
||||
|
||||
|
||||
def reply_comment_email(data):
|
||||
|
|
@ -98,26 +97,14 @@ def reply_comment_email(data):
|
|||
return
|
||||
|
||||
# safe logic: no answer or unknown answer is a go for publishing
|
||||
if message[:2].upper() in ("NO", "SP"):
|
||||
|
||||
# put a log to help fail2ban
|
||||
if message[:2].upper() == "SP": # SPAM
|
||||
if comment.ip:
|
||||
logger.info(
|
||||
"SPAM comment from %s: %d" % (comment.ip, comment_id)
|
||||
)
|
||||
else:
|
||||
logger.info("cannot identify SPAM source: %d" % comment_id)
|
||||
|
||||
if message[:2].upper() in ("NO"):
|
||||
logger.info("discard comment: %d" % comment_id)
|
||||
comment.delete_instance()
|
||||
email_body = get_template("drop_comment").render(original=message)
|
||||
mailer.send(from_email, "Re: " + subject, email_body)
|
||||
else:
|
||||
# update Comment row
|
||||
comment.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
comment.ip = None
|
||||
comment.save()
|
||||
# save publishing datetime
|
||||
comment.publish()
|
||||
logger.info("commit comment: %d" % comment_id)
|
||||
|
||||
# rebuild RSS
|
||||
|
|
|
|||
|
|
@ -10,83 +10,67 @@ from conf import config
|
|||
logger = logging.getLogger(__name__)
|
||||
app = config.flaskapp()
|
||||
|
||||
@app.route("/ping", methods=['GET'])
|
||||
|
||||
@app.route("/ping", methods=["GET"])
|
||||
def ping():
|
||||
return "OK"
|
||||
|
||||
|
||||
@app.route("/comments", methods=['GET'])
|
||||
@app.route("/comments", methods=["GET"])
|
||||
def query_comments():
|
||||
|
||||
comments = []
|
||||
try:
|
||||
token = request.args.get('token', '')
|
||||
url = request.args.get('url', '')
|
||||
token = request.args.get("token", "")
|
||||
url = request.args.get("url", "")
|
||||
|
||||
logger.info('retrieve comments for token %s, url %s' % (token, url))
|
||||
for comment in Comment.select(Comment).join(Site).where(
|
||||
(Comment.url == url) &
|
||||
(Comment.published.is_null(False)) &
|
||||
(Site.token == token)).order_by(+Comment.published):
|
||||
logger.info("retrieve comments for token %s, url %s" % (token, url))
|
||||
for comment in (
|
||||
Comment.select(Comment)
|
||||
.join(Site)
|
||||
.where(
|
||||
(Comment.url == url)
|
||||
& (Comment.published.is_null(False))
|
||||
& (Site.token == token)
|
||||
)
|
||||
.order_by(+Comment.published)
|
||||
):
|
||||
d = {}
|
||||
d['author'] = comment.author_name
|
||||
d['content'] = comment.content
|
||||
d["author"] = comment.author_name
|
||||
d["content"] = comment.content
|
||||
if comment.author_site:
|
||||
d['site'] = comment.author_site
|
||||
d['avatar'] = comment.author_gravatar
|
||||
d['date'] = comment.published.strftime("%Y-%m-%d %H:%M:%S")
|
||||
d["site"] = comment.author_site
|
||||
d["avatar"] = comment.author_gravatar
|
||||
d["date"] = comment.published.strftime("%Y-%m-%d %H:%M:%S")
|
||||
logger.debug(d)
|
||||
comments.append(d)
|
||||
r = jsonify({'data': comments})
|
||||
r = jsonify({"data": comments})
|
||||
r.status_code = 200
|
||||
except:
|
||||
logger.warn('bad request')
|
||||
r = jsonify({'data': []})
|
||||
logger.warn("bad request")
|
||||
r = jsonify({"data": []})
|
||||
r.status_code = 400
|
||||
return r
|
||||
|
||||
|
||||
@app.route("/comments/count", methods=['GET'])
|
||||
@app.route("/comments/count", methods=["GET"])
|
||||
def get_comments_count():
|
||||
try:
|
||||
token = request.args.get('token', '')
|
||||
url = request.args.get('url', '')
|
||||
count = Comment.select(Comment).join(Site).where(
|
||||
(Comment.url == url) &
|
||||
(Comment.published.is_null(False)) &
|
||||
(Site.token == token)).count()
|
||||
r = jsonify({'count': count})
|
||||
token = request.args.get("token", "")
|
||||
url = request.args.get("url", "")
|
||||
count = (
|
||||
Comment.select(Comment)
|
||||
.join(Site)
|
||||
.where(
|
||||
(Comment.url == url)
|
||||
& (Comment.published.is_null(False))
|
||||
& (Site.token == token)
|
||||
)
|
||||
.count()
|
||||
)
|
||||
r = jsonify({"count": count})
|
||||
r.status_code = 200
|
||||
except:
|
||||
r = jsonify({'count': 0})
|
||||
r = jsonify({"count": 0})
|
||||
r.status_code = 200
|
||||
return r
|
||||
|
||||
|
||||
@app.route("/comments", methods=['POST'])
|
||||
def new_comment():
|
||||
|
||||
try:
|
||||
data = request.get_json()
|
||||
logger.info(data)
|
||||
|
||||
# validate token: retrieve site entity
|
||||
token = data.get('token', '')
|
||||
site = Site.select().where(Site.token == token).get()
|
||||
if site is None:
|
||||
logger.warn('Unknown site %s' % token)
|
||||
abort(400)
|
||||
|
||||
# honeypot for spammers
|
||||
captcha = data.get('captcha', '')
|
||||
if captcha:
|
||||
logger.warn('discard spam: data %s' % data)
|
||||
abort(400)
|
||||
|
||||
processor.enqueue({'request': 'new_comment', 'data': data})
|
||||
|
||||
except:
|
||||
logger.exception("new comment failure")
|
||||
abort(400)
|
||||
|
||||
return "OK"
|
||||
|
|
|
|||
|
|
@ -12,6 +12,7 @@ from helper.hashing import md5
|
|||
logger = logging.getLogger(__name__)
|
||||
app = config.flaskapp()
|
||||
|
||||
|
||||
@app.route("/newcomment", methods=["POST"])
|
||||
def new_form_comment():
|
||||
|
||||
|
|
@ -19,11 +20,6 @@ def new_form_comment():
|
|||
data = request.form
|
||||
logger.info("form data " + str(data))
|
||||
|
||||
# add client IP if provided by HTTP proxy
|
||||
ip = ""
|
||||
if "X-Forwarded-For" in request.headers:
|
||||
ip = request.headers["X-Forwarded-For"]
|
||||
|
||||
# validate token: retrieve site entity
|
||||
token = data.get("token", "")
|
||||
site = Site.select().where(Site.token == token).get()
|
||||
|
|
@ -32,12 +28,12 @@ def new_form_comment():
|
|||
abort(400)
|
||||
|
||||
# honeypot for spammers
|
||||
captcha = data.get("remarque", "")
|
||||
captcha = data.get("remarque", "")
|
||||
if captcha:
|
||||
logger.warn("discard spam: data %s" % data)
|
||||
abort(400)
|
||||
|
||||
url = data.get("url", "")
|
||||
url = data.get("url", "")
|
||||
author_name = data.get("author", "").strip()
|
||||
author_gravatar = data.get("email", "").strip()
|
||||
author_site = data.get("site", "").lower().strip()
|
||||
|
|
@ -63,7 +59,6 @@ def new_form_comment():
|
|||
created=created,
|
||||
notified=None,
|
||||
published=None,
|
||||
ip=ip,
|
||||
)
|
||||
comment.save()
|
||||
|
||||
|
|
@ -73,12 +68,13 @@ def new_form_comment():
|
|||
|
||||
return redirect("/redirect/", code=302)
|
||||
|
||||
|
||||
def check_form_data(data):
|
||||
fields = ['url', 'message', 'site', 'remarque', 'author', 'token', 'email']
|
||||
fields = ["url", "message", "site", "remarque", "author", "token", "email"]
|
||||
d = data.to_dict()
|
||||
for field in fields:
|
||||
if field in d:
|
||||
del d[field]
|
||||
if d:
|
||||
if d:
|
||||
logger.warn("additional field: data %s" % data)
|
||||
abort(400)
|
||||
abort(400)
|
||||
|
|
|
|||
|
|
@ -8,19 +8,27 @@ from peewee import DateTimeField
|
|||
from peewee import ForeignKeyField
|
||||
from model.site import Site
|
||||
from core.database import get_db
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class Comment(Model):
|
||||
url = CharField()
|
||||
created = DateTimeField()
|
||||
notified = DateTimeField(null=True,default=None)
|
||||
notified = DateTimeField(null=True, default=None)
|
||||
published = DateTimeField(null=True, default=None)
|
||||
author_name = CharField()
|
||||
author_site = CharField(default='')
|
||||
author_gravatar = CharField(default='')
|
||||
ip = CharField(default='')
|
||||
author_site = CharField(default="")
|
||||
author_gravatar = CharField(default="")
|
||||
content = TextField()
|
||||
site = ForeignKeyField(Site, related_name='site')
|
||||
site = ForeignKeyField(Site, related_name="site")
|
||||
|
||||
class Meta:
|
||||
database = get_db()
|
||||
|
||||
def notify_site_admin(self):
|
||||
self.notified = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
self.save()
|
||||
|
||||
def publish(self):
|
||||
self.published = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||
self.save()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue