connect new templater implementation
This commit is contained in:
parent
6397e547d8
commit
9c39cf6d30
4 changed files with 40 additions and 47 deletions
|
@ -2,17 +2,21 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
import os
|
||||||
import re
|
import re
|
||||||
import time
|
import time
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
from stacosys.core import rss
|
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.comment import Comment, Site
|
||||||
from stacosys.model.email import Email
|
from stacosys.model.email import Email
|
||||||
|
|
||||||
logger = logging.getLogger(__name__)
|
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):
|
def fetch_mail_answers(lang, mailer, rss):
|
||||||
for msg in mailer.fetch():
|
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"):
|
if email.plain_text_content[:2].upper() in ("NO"):
|
||||||
logger.info("discard comment: %d" % comment_id)
|
logger.info("discard comment: %d" % comment_id)
|
||||||
comment.delete_instance()
|
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
|
original=email.plain_text_content
|
||||||
)
|
)
|
||||||
if not mailer.send(email.from_addr, "Re: " + email.subject, new_email_body):
|
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)
|
rss.generate_site(token)
|
||||||
|
|
||||||
# send approval confirmation email to admin
|
# 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
|
original=email.plain_text_content
|
||||||
)
|
)
|
||||||
if not mailer.send(email.from_addr, "Re: " + email.subject, new_email_body):
|
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)
|
comment_text = "\n".join(comment_list)
|
||||||
# TODO use constants for template names
|
email_body = templater.get_template(lang, Template.NEW_COMMENT).render(
|
||||||
email_body = get_template(lang, "new_comment").render(
|
|
||||||
url=comment.url, comment=comment_text
|
url=comment.url, comment=comment_text
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
#!/usr/bin/python
|
#!/usr/bin/python
|
||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
|
import os
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
|
|
||||||
import markdown
|
import markdown
|
||||||
import PyRSS2Gen
|
import PyRSS2Gen
|
||||||
|
|
||||||
import stacosys.conf.config as config
|
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.comment import Comment
|
||||||
from stacosys.model.site import Site
|
from stacosys.model.site import Site
|
||||||
|
|
||||||
|
@ -17,6 +18,9 @@ class Rss:
|
||||||
self._lang = lang
|
self._lang = lang
|
||||||
self._rss_file = rss_file
|
self._rss_file = rss_file
|
||||||
self._rss_proto = rss_proto
|
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):
|
def generate_all(self):
|
||||||
|
|
||||||
|
@ -26,7 +30,9 @@ class Rss:
|
||||||
def _generate_site(self, token):
|
def _generate_site(self, token):
|
||||||
|
|
||||||
site = Site.select().where(Site.token == token).get()
|
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()
|
md = markdown.Markdown()
|
||||||
|
|
||||||
items = []
|
items = []
|
||||||
|
|
|
@ -1,31 +1,22 @@
|
||||||
#!/usr/bin/env python
|
#!/usr/bin/env python
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
import os
|
|
||||||
|
|
||||||
from jinja2 import Environment, FileSystemLoader
|
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:
|
class Templater:
|
||||||
def __init__(self, lang, template_path):
|
def __init__(self, template_path):
|
||||||
self._env = Environment(loader=FileSystemLoader(template_path))
|
self._env = Environment(loader=FileSystemLoader(template_path))
|
||||||
self._lang = lang
|
|
||||||
|
|
||||||
def get_template(self, name):
|
def get_template(self, lang, name):
|
||||||
return self._env.get_template(self._lang + "/" + name + ".tpl")
|
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"
|
||||||
|
|
||||||
|
|
|
@ -4,61 +4,54 @@
|
||||||
import os
|
import os
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from stacosys.core.templater import (
|
from stacosys.core.templater import Templater, Template
|
||||||
Templater,
|
|
||||||
TEMPLATE_APPROVE_COMMENT,
|
|
||||||
TEMPLATE_DROP_COMMENT,
|
|
||||||
TEMPLATE_NEW_COMMENT,
|
|
||||||
TEMPLATE_NOTIFY_MESSAGE,
|
|
||||||
TEMPLATE_RSS_TITLE_MESSAGE,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def get_template_content(lang, template_name, **kwargs):
|
def get_template_content(lang, template_name, **kwargs):
|
||||||
current_path = os.path.dirname(__file__)
|
current_path = os.path.dirname(__file__)
|
||||||
template_path = os.path.abspath(os.path.join(current_path, "../stacosys/templates"))
|
template_path = os.path.abspath(os.path.join(current_path, "../stacosys/templates"))
|
||||||
templater = Templater(lang, template_path)
|
templater = Templater(template_path)
|
||||||
template = templater.get_template(template_name)
|
template = templater.get_template(lang, template_name)
|
||||||
assert template
|
assert template
|
||||||
return template.render(kwargs)
|
return template.render(kwargs)
|
||||||
|
|
||||||
|
|
||||||
def test_approve_comment():
|
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.startswith("Bonjour,\n\nLe commentaire sera bientôt publié.")
|
||||||
assert content.endswith("[texte]")
|
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.startswith("Hi,\n\nThe comment should be published soon.")
|
||||||
assert content.endswith("[texte]")
|
assert content.endswith("[texte]")
|
||||||
|
|
||||||
|
|
||||||
def test_drop_comment():
|
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.startswith("Bonjour,\n\nLe commentaire ne sera pas publié.")
|
||||||
assert content.endswith("[texte]")
|
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.startswith("Hi,\n\nThe comment will not be published.")
|
||||||
assert content.endswith("[texte]")
|
assert content.endswith("[texte]")
|
||||||
|
|
||||||
|
|
||||||
def test_new_comment():
|
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.startswith("Bonjour,\n\nUn nouveau commentaire a été posté")
|
||||||
assert content.endswith("[comment]\n\n--\nStacosys")
|
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.startswith("Hi,\n\nA new comment has been submitted")
|
||||||
assert content.endswith("[comment]\n\n--\nStacosys")
|
assert content.endswith("[comment]\n\n--\nStacosys")
|
||||||
|
|
||||||
|
|
||||||
def test_notify_message():
|
def test_notify_message():
|
||||||
content = get_template_content("fr", TEMPLATE_NOTIFY_MESSAGE)
|
content = get_template_content("fr", Template.NOTIFY_MESSAGE)
|
||||||
assert content == "Nouveau commentaire"
|
assert content == "Nouveau commentaire"
|
||||||
content = get_template_content("en", TEMPLATE_NOTIFY_MESSAGE)
|
content = get_template_content("en", Template.NOTIFY_MESSAGE)
|
||||||
assert content == "New comment"
|
assert content == "New comment"
|
||||||
|
|
||||||
|
|
||||||
def test_rss_title():
|
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"
|
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"
|
assert content == "[site] : comments"
|
||||||
|
|
Loading…
Add table
Reference in a new issue