From 9c39cf6d3036fef240cc17200bce1a38aa1cca07 Mon Sep 17 00:00:00 2001 From: Yax <1949284+kianby@users.noreply.github.com> Date: Sun, 10 Jan 2021 14:26:59 +0100 Subject: [PATCH] connect new templater implementation --- stacosys/core/cron.py | 13 ++++++++----- stacosys/core/rss.py | 10 ++++++++-- stacosys/core/templater.py | 31 +++++++++++-------------------- tests/test_templater.py | 33 +++++++++++++-------------------- 4 files changed, 40 insertions(+), 47 deletions(-) diff --git a/stacosys/core/cron.py b/stacosys/core/cron.py index 95951ee..920dfc3 100644 --- a/stacosys/core/cron.py +++ b/stacosys/core/cron.py @@ -2,17 +2,21 @@ # -*- coding: utf-8 -*- import logging +import os import re import time from datetime import datetime from stacosys.core import rss -from stacosys.core.templater import get_template +from stacosys.core.templater import Templater, Template from stacosys.model.comment import Comment, Site from stacosys.model.email import Email logger = logging.getLogger(__name__) +current_path = os.path.dirname(__file__) +template_path = os.path.abspath(os.path.join(current_path, "../templates")) +templater = Templater(template_path) def fetch_mail_answers(lang, mailer, rss): for msg in mailer.fetch(): @@ -53,7 +57,7 @@ def _reply_comment_email(lang, mailer, rss, email: Email): if email.plain_text_content[:2].upper() in ("NO"): logger.info("discard comment: %d" % comment_id) comment.delete_instance() - new_email_body = get_template(lang, "drop_comment").render( + new_email_body = templater.get_template(lang, Template.DROP_COMMENT).render( original=email.plain_text_content ) if not mailer.send(email.from_addr, "Re: " + email.subject, new_email_body): @@ -67,7 +71,7 @@ def _reply_comment_email(lang, mailer, rss, email: Email): rss.generate_site(token) # send approval confirmation email to admin - new_email_body = get_template(lang, "approve_comment").render( + new_email_body = templater.get_template(lang, Template.APPROVE_COMMENT).render( original=email.plain_text_content ) if not mailer.send(email.from_addr, "Re: " + email.subject, new_email_body): @@ -90,8 +94,7 @@ def submit_new_comment(lang, mailer): "", ) comment_text = "\n".join(comment_list) - # TODO use constants for template names - email_body = get_template(lang, "new_comment").render( + email_body = templater.get_template(lang, Template.NEW_COMMENT).render( url=comment.url, comment=comment_text ) diff --git a/stacosys/core/rss.py b/stacosys/core/rss.py index d333772..5d8b761 100644 --- a/stacosys/core/rss.py +++ b/stacosys/core/rss.py @@ -1,13 +1,14 @@ #!/usr/bin/python # -*- coding: UTF-8 -*- +import os from datetime import datetime import markdown import PyRSS2Gen import stacosys.conf.config as config -from stacosys.core.templater import get_template +from stacosys.core.templater import Templater, Template from stacosys.model.comment import Comment from stacosys.model.site import Site @@ -17,6 +18,9 @@ class Rss: self._lang = lang self._rss_file = rss_file self._rss_proto = rss_proto + current_path = os.path.dirname(__file__) + template_path = os.path.abspath(os.path.join(current_path, "../templates")) + self._templater = Templater(template_path) def generate_all(self): @@ -26,7 +30,9 @@ class Rss: def _generate_site(self, token): site = Site.select().where(Site.token == token).get() - rss_title = get_template(self._lang, "rss_title_message").render(site=site.name) + rss_title = self._templater.get_template( + self._lang, Template.RSS_TITLE_MESSAGE + ).render(site=site.name) md = markdown.Markdown() items = [] diff --git a/stacosys/core/templater.py b/stacosys/core/templater.py index 6ab8511..025645b 100644 --- a/stacosys/core/templater.py +++ b/stacosys/core/templater.py @@ -1,31 +1,22 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -import os from jinja2 import Environment, FileSystemLoader -current_path = os.path.dirname(__file__) -template_path = os.path.abspath(os.path.join(current_path, "../templates")) -env = Environment(loader=FileSystemLoader(template_path)) - -TEMPLATE_DROP_COMMENT = "drop_comment" -TEMPLATE_APPROVE_COMMENT = "approve_comment" -TEMPLATE_NEW_COMMENT = "new_comment" -TEMPLATE_NOTIFY_MESSAGE = "notify_message" -TEMPLATE_RSS_TITLE_MESSAGE = "rss_title_message" - - -def get_template(lang, name): - return env.get_template(lang + "/" + name + ".tpl") - - class Templater: - def __init__(self, lang, template_path): + def __init__(self, template_path): self._env = Environment(loader=FileSystemLoader(template_path)) - self._lang = lang - def get_template(self, name): - return self._env.get_template(self._lang + "/" + name + ".tpl") + def get_template(self, lang, name): + return self._env.get_template(lang + "/" + name + ".tpl") + + +class Template: + DROP_COMMENT = "drop_comment" + APPROVE_COMMENT = "approve_comment" + NEW_COMMENT = "new_comment" + NOTIFY_MESSAGE = "notify_message" + RSS_TITLE_MESSAGE = "rss_title_message" diff --git a/tests/test_templater.py b/tests/test_templater.py index 2d23ba0..6f5b40f 100644 --- a/tests/test_templater.py +++ b/tests/test_templater.py @@ -4,61 +4,54 @@ import os import pytest -from stacosys.core.templater import ( - Templater, - TEMPLATE_APPROVE_COMMENT, - TEMPLATE_DROP_COMMENT, - TEMPLATE_NEW_COMMENT, - TEMPLATE_NOTIFY_MESSAGE, - TEMPLATE_RSS_TITLE_MESSAGE, -) +from stacosys.core.templater import Templater, Template def get_template_content(lang, template_name, **kwargs): current_path = os.path.dirname(__file__) template_path = os.path.abspath(os.path.join(current_path, "../stacosys/templates")) - templater = Templater(lang, template_path) - template = templater.get_template(template_name) + templater = Templater(template_path) + template = templater.get_template(lang, template_name) assert template return template.render(kwargs) def test_approve_comment(): - content = get_template_content("fr", TEMPLATE_APPROVE_COMMENT, original="[texte]") + content = get_template_content("fr", Template.APPROVE_COMMENT, original="[texte]") assert content.startswith("Bonjour,\n\nLe commentaire sera bientôt publié.") assert content.endswith("[texte]") - content = get_template_content("en", TEMPLATE_APPROVE_COMMENT, original="[texte]") + content = get_template_content("en", Template.APPROVE_COMMENT, original="[texte]") assert content.startswith("Hi,\n\nThe comment should be published soon.") assert content.endswith("[texte]") def test_drop_comment(): - content = get_template_content("fr", TEMPLATE_DROP_COMMENT, original="[texte]") + content = get_template_content("fr", Template.DROP_COMMENT, original="[texte]") assert content.startswith("Bonjour,\n\nLe commentaire ne sera pas publié.") assert content.endswith("[texte]") - content = get_template_content("en", TEMPLATE_DROP_COMMENT, original="[texte]") + content = get_template_content("en", Template.DROP_COMMENT, original="[texte]") assert content.startswith("Hi,\n\nThe comment will not be published.") assert content.endswith("[texte]") def test_new_comment(): - content = get_template_content("fr", TEMPLATE_NEW_COMMENT, comment="[comment]") + content = get_template_content("fr", Template.NEW_COMMENT, comment="[comment]") assert content.startswith("Bonjour,\n\nUn nouveau commentaire a été posté") assert content.endswith("[comment]\n\n--\nStacosys") - content = get_template_content("en", TEMPLATE_NEW_COMMENT, comment="[comment]") + content = get_template_content("en", Template.NEW_COMMENT, comment="[comment]") assert content.startswith("Hi,\n\nA new comment has been submitted") assert content.endswith("[comment]\n\n--\nStacosys") def test_notify_message(): - content = get_template_content("fr", TEMPLATE_NOTIFY_MESSAGE) + content = get_template_content("fr", Template.NOTIFY_MESSAGE) assert content == "Nouveau commentaire" - content = get_template_content("en", TEMPLATE_NOTIFY_MESSAGE) + content = get_template_content("en", Template.NOTIFY_MESSAGE) assert content == "New comment" def test_rss_title(): - content = get_template_content("fr", TEMPLATE_RSS_TITLE_MESSAGE, site="[site]") + content = get_template_content("fr", Template.RSS_TITLE_MESSAGE, site="[site]") assert content == "[site] : commentaires" - content = get_template_content("en", TEMPLATE_RSS_TITLE_MESSAGE, site="[site]") + content = get_template_content("en", Template.RSS_TITLE_MESSAGE, site="[site]") assert content == "[site] : comments"