Share new comment function

pull/10/head
Yax 2 years ago
parent 9e2e2b1750
commit bafc0af92c

@ -1,9 +1,43 @@
#!/usr/bin/env python #!/usr/bin/env python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging
import background
from flask import Flask from flask import Flask
from stacosys.db import dao
from stacosys.service import config, mailer
from stacosys.service.configuration import ConfigParameter
app = Flask(__name__) app = Flask(__name__)
# Set the secret key to some random bytes. Keep this really secret! # Set the secret key to some random bytes. Keep this really secret!
app.secret_key = b'_5#y2L"F4Q8z\n\xec]/' app.secret_key = b'_5#y2L"F4Q8z\n\xec]/'
logger = logging.getLogger(__name__)
@background.task
def submit_new_comment(comment):
site_url = config.get(ConfigParameter.SITE_URL)
comment_list = (
f"Web admin interface: {site_url}/web/admin",
"",
f"author: {comment.author_name}",
f"site: {comment.author_site}",
f"date: {comment.created}",
f"url: {comment.url}",
"",
comment.content,
"",
)
email_body = "\n".join(comment_list)
# send email to notify admin
site_name = config.get(ConfigParameter.SITE_NAME)
subject = f"STACOSYS {site_name}"
if mailer.send(subject, email_body):
logger.debug("new comment processed")
# save notification datetime
dao.notify_comment(comment)

@ -6,7 +6,7 @@ import logging
from flask import jsonify, request from flask import jsonify, request
from stacosys.db import dao from stacosys.db import dao
from stacosys.interface import app from stacosys.interface import app, submit_new_comment
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -38,6 +38,8 @@ def query_comments():
@app.route("/api/comments/count", methods=["GET"]) @app.route("/api/comments/count", methods=["GET"])
def get_comments_count(): def get_comments_count():
# TODO process pending comments # send notification for pending e-mails asynchronously
for comment in dao.find_not_notified_comments():
submit_new_comment(comment)
url = request.args.get("url", "") url = request.args.get("url", "")
return jsonify({"count": dao.count_published_comments(url)}) return jsonify({"count": dao.count_published_comments(url)})

@ -2,12 +2,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import logging import logging
import background
from flask import abort, redirect, request from flask import abort, redirect, request
from stacosys.db import dao from stacosys.db import dao
from stacosys.interface import app from stacosys.interface import app, submit_new_comment
from stacosys.service import config, mailer from stacosys.service import config
from stacosys.service.configuration import ConfigParameter from stacosys.service.configuration import ConfigParameter
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
@ -55,30 +54,3 @@ def check_form_data(posted_comment):
fields = ["url", "message", "site", "remarque", "author", "token", "email"] fields = ["url", "message", "site", "remarque", "author", "token", "email"]
filtered = dict(filter(lambda x: x[0] not in fields, posted_comment.items())) filtered = dict(filter(lambda x: x[0] not in fields, posted_comment.items()))
return not filtered return not filtered
@background.task
def submit_new_comment(comment):
site_url = config.get(ConfigParameter.SITE_URL)
comment_list = (
f"Web admin interface: {site_url}/web/admin",
"",
f"author: {comment.author_name}",
f"site: {comment.author_site}",
f"date: {comment.created}",
f"url: {comment.url}",
"",
comment.content,
"",
)
email_body = "\n".join(comment_list)
# send email to notify admin
site_name = config.get(ConfigParameter.SITE_NAME)
subject = f"STACOSYS {site_name}"
if mailer.send(subject, email_body):
logger.debug("new comment processed")
# save notification datetime
dao.notify_comment(comment)
else:
logger.warning("rescheduled. send mail failure %s", subject)

Loading…
Cancel
Save