connect new templater implementation

This commit is contained in:
Yax 2021-01-10 14:26:59 +01:00
parent 6397e547d8
commit 9c39cf6d30
4 changed files with 40 additions and 47 deletions

View file

@ -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
)

View file

@ -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 = []

View file

@ -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"