From bafc0af92cd0f8fb10503fbcc93b1bbff0c7ced4 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Wed, 30 Nov 2022 11:46:57 +0100 Subject: [PATCH] Share new comment function --- stacosys/interface/__init__.py | 34 ++++++++++++++++++++++++++++++++++ stacosys/interface/api.py | 6 ++++-- stacosys/interface/form.py | 32 ++------------------------------ 3 files changed, 40 insertions(+), 32 deletions(-) diff --git a/stacosys/interface/__init__.py b/stacosys/interface/__init__.py index 540e40e..d72483d 100644 --- a/stacosys/interface/__init__.py +++ b/stacosys/interface/__init__.py @@ -1,9 +1,43 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import logging + +import background from flask import Flask +from stacosys.db import dao +from stacosys.service import config, mailer +from stacosys.service.configuration import ConfigParameter + app = Flask(__name__) # Set the secret key to some random bytes. Keep this really secret! 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) diff --git a/stacosys/interface/api.py b/stacosys/interface/api.py index 06aa07f..13dbcff 100644 --- a/stacosys/interface/api.py +++ b/stacosys/interface/api.py @@ -6,7 +6,7 @@ import logging from flask import jsonify, request from stacosys.db import dao -from stacosys.interface import app +from stacosys.interface import app, submit_new_comment logger = logging.getLogger(__name__) @@ -38,6 +38,8 @@ def query_comments(): @app.route("/api/comments/count", methods=["GET"]) 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", "") return jsonify({"count": dao.count_published_comments(url)}) diff --git a/stacosys/interface/form.py b/stacosys/interface/form.py index 20722f9..8513438 100644 --- a/stacosys/interface/form.py +++ b/stacosys/interface/form.py @@ -2,12 +2,11 @@ # -*- coding: utf-8 -*- import logging -import background from flask import abort, redirect, request from stacosys.db import dao -from stacosys.interface import app -from stacosys.service import config, mailer +from stacosys.interface import app, submit_new_comment +from stacosys.service import config from stacosys.service.configuration import ConfigParameter logger = logging.getLogger(__name__) @@ -55,30 +54,3 @@ def check_form_data(posted_comment): fields = ["url", "message", "site", "remarque", "author", "token", "email"] filtered = dict(filter(lambda x: x[0] not in fields, posted_comment.items())) 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)