Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Refactor jinja2 templating. Remove old XXX comment
Browse files Browse the repository at this point in the history
  • Loading branch information
anoadragon453 committed Sep 4, 2019
1 parent 2253193 commit 2ee17d7
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 61 deletions.
12 changes: 9 additions & 3 deletions synapse/handlers/account_validity.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
class AccountValidityHandler(object):
def __init__(self, hs):
self.hs = hs
self.config = hs.config
self.store = self.hs.get_datastore()
self.sendmail = self.hs.get_sendmail()
self.clock = self.hs.get_clock()
Expand All @@ -62,9 +63,14 @@ def __init__(self, hs):
self._raw_from = email.utils.parseaddr(self._from_string)[1]

self._template_html, self._template_text = load_jinja2_templates(
config=self.hs.config,
template_html_name=self.hs.config.email_expiry_template_html,
template_text_name=self.hs.config.email_expiry_template_text,
self.config.email_template_dir,
[
self.config.email_expiry_template_html,
self.config.email_expiry_template_text,
],
apply_format_ts_filter=True,
apply_mxc_to_http_filter=True,
public_baseurl=self.config.public_baseurl,
)

# Check the renewal emails to send and send them every 30min.
Expand Down
2 changes: 0 additions & 2 deletions synapse/handlers/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,8 +363,6 @@ def _check_auth_dict(self, authdict, clientip):
login_type = authdict["type"]
checker = self.checkers.get(login_type)
if checker is not None:
# XXX: Temporary workaround for having Synapse handle password resets
# See AuthHandler.check_auth for further details
res = yield checker(authdict, clientip=clientip)
return res

Expand Down
56 changes: 30 additions & 26 deletions synapse/push/mailer.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,46 +629,50 @@ def format_ts_filter(value, format):
return time.strftime(format, time.localtime(value / 1000))


# XXX: This method and the next could likely be combined in a smarter way
@staticmethod
def load_jinja2_template(template_dir, template_filename, template_vars):
def load_jinja2_templates(
template_dir,
template_filenames,
apply_format_ts_filter=False,
apply_mxc_to_http_filter=False,
public_baseurl=None,
):
"""Loads a jinja2 template with variables to insert
Args:
template_dir (str): The directory where templates are stored
template_filename (str): The name of the template in the template_dir
template_vars (Dict): Dictionary of keys in the template
alongside their values to insert
template_filenames (list[str]): A list of template filenames
apply_format_ts_filter (bool): Whether to apply a template filter that formats
timestamps
apply_mxc_to_http_filter (bool): Whether to apply a template filter that converts
mxc urls to http urls
public_baseurl (str|None): The public baseurl of the server. Required for
apply_mxc_to_http_filter to be enabled
Returns:
str containing the contents of the rendered template
A list of jinja2 templates corresponding to the given list of filepaths,
with order preserved
"""
logger.info(
"loading email templates %s from '%s'", template_filenames, template_dir
)
loader = jinja2.FileSystemLoader(template_dir)
env = jinja2.Environment(loader=loader)

template = env.get_template(template_filename)
return template.render(**template_vars)


def load_jinja2_templates(config, template_html_name, template_text_name):
"""Load the jinja2 email templates from disk
if apply_format_ts_filter:
env.filters["format_ts"] = format_ts_filter

Returns:
(template_html, template_text)
"""
logger.info("loading email templates from '%s'", config.email_template_dir)
loader = jinja2.FileSystemLoader(config.email_template_dir)
env = jinja2.Environment(loader=loader)
env.filters["format_ts"] = format_ts_filter
env.filters["mxc_to_http"] = _create_mxc_to_http_filter(config)
if apply_mxc_to_http_filter and public_baseurl:
env.filters["mxc_to_http"] = _create_mxc_to_http_filter(public_baseurl)

template_html = env.get_template(template_html_name)
template_text = env.get_template(template_text_name)
templates = []
for template_filename in template_filenames:
template = env.get_template(template_filename)
templates.append(template)

return template_html, template_text
return templates


def _create_mxc_to_http_filter(config):
def _create_mxc_to_http_filter(public_baseurl):
def mxc_to_http_filter(value, width, height, resize_method="crop"):
if value[0:6] != "mxc://":
return ""
Expand All @@ -681,7 +685,7 @@ def mxc_to_http_filter(value, width, height, resize_method="crop"):

params = {"width": width, "height": height, "method": resize_method}
return "%s_matrix/media/v1/thumbnail/%s?%s%s" % (
config.public_baseurl,
public_baseurl,
serverAndMediaId,
urllib.parse.urlencode(params),
fragment or "",
Expand Down
17 changes: 11 additions & 6 deletions synapse/push/pusher.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,19 +35,24 @@
class PusherFactory(object):
def __init__(self, hs):
self.hs = hs
self.config = hs.config

self.pusher_types = {"http": HttpPusher}

logger.info("email enable notifs: %r", hs.config.email_enable_notifs)
if hs.config.email_enable_notifs:
self.mailers = {} # app_name -> Mailer

templates = load_jinja2_templates(
config=hs.config,
template_html_name=hs.config.email_notif_template_html,
template_text_name=hs.config.email_notif_template_text,
self.notif_template_html, self.notif_template_text = load_jinja2_templates(
self.config.email_template_dir,
[
self.config.email_notif_template_html,
self.config.email_notif_template_text,
],
apply_format_ts_filter=True,
apply_mxc_to_http_filter=True,
public_baseurl=self.config.public_baseurl,
)
self.notif_template_html, self.notif_template_text = templates

self.pusher_types["email"] = self._create_email_pusher

Expand Down Expand Up @@ -78,6 +83,6 @@ def _app_name_from_pusherdict(self, pusherdict):
if "data" in pusherdict and "brand" in pusherdict["data"]:
app_name = pusherdict["data"]["brand"]
else:
app_name = self.hs.config.email_app_name
app_name = self.config.email_app_name

return app_name
30 changes: 19 additions & 11 deletions synapse/rest/client/v2_alpha/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
parse_json_object_from_request,
parse_string,
)
from synapse.push.mailer import load_jinja2_template
from synapse.push.mailer import load_jinja2_templates
from synapse.util.msisdn import phone_number_to_msisdn
from synapse.util.threepids import check_3pid_allowed

Expand All @@ -52,16 +52,21 @@ def __init__(self, hs):
if self.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
from synapse.push.mailer import Mailer, load_jinja2_templates

This comment has been minimized.

Copy link
@richvdh

richvdh Sep 4, 2019

Member

import of load_jinja2_templates here is redundant? And I think we may as well move Mailerto the top too?

(I think the import is here because jinja used to be an optional dep. It's no longer optional, so can be a normal import)


templates = load_jinja2_templates(
config=hs.config,
template_html_name=hs.config.email_password_reset_template_html,
template_text_name=hs.config.email_password_reset_template_text,
template_html, template_text = load_jinja2_templates(
self.config.email_template_dir,
[
self.config.email_password_reset_template_html,
self.config.email_password_reset_template_text,
],
apply_format_ts_filter=True,
apply_mxc_to_http_filter=True,
public_baseurl=self.config.public_baseurl,
)
self.mailer = Mailer(
hs=self.hs,
app_name=self.config.email_app_name,
template_html=templates[0],
template_text=templates[1],
template_html=template_html,
template_text=template_text,
)

@defer.inlineCallbacks
Expand Down Expand Up @@ -255,13 +260,16 @@ def on_GET(self, request, medium):
html = self.config.email_password_reset_template_success_html
request.setResponseCode(200)
except ThreepidValidationError as e:
request.setResponseCode(e.code)

# Show a failure page with a reason
html = load_jinja2_template(
html_template = load_jinja2_templates(
self.config.email_template_dir,
self.config.email_password_reset_template_failure_html,
template_vars={"failure_reason": e.msg},
[self.config.email_password_reset_template_failure_html],
)
request.setResponseCode(e.code)

template_vars = {"failure_reason": e.msg}
html = html_template.render(**template_vars)

request.write(html.encode("utf-8"))
finish_request(request)
Expand Down
34 changes: 21 additions & 13 deletions synapse/rest/client/v2_alpha/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
parse_json_object_from_request,
parse_string,
)
from synapse.push.mailer import load_jinja2_template
from synapse.push.mailer import load_jinja2_templates
from synapse.util.msisdn import phone_number_to_msisdn
from synapse.util.ratelimitutils import FederationRateLimiter
from synapse.util.threepids import check_3pid_allowed
Expand Down Expand Up @@ -79,16 +79,21 @@ def __init__(self, hs):
if self.hs.config.threepid_behaviour_email == ThreepidBehaviour.LOCAL:
from synapse.push.mailer import Mailer, load_jinja2_templates

templates = load_jinja2_templates(
config=hs.config,
template_html_name=hs.config.email_registration_template_html,
template_text_name=hs.config.email_registration_template_text,
template_html, template_text = load_jinja2_templates(
self.config.email_template_dir,
[
self.config.email_registration_template_html,
self.config.email_registration_template_text,
],
apply_format_ts_filter=True,
apply_mxc_to_http_filter=True,
public_baseurl=self.config.public_baseurl,
)
self.mailer = Mailer(
hs=self.hs,
app_name=self.hs.config.email_app_name,
template_html=templates[0],
template_text=templates[1],
app_name=self.config.email_app_name,
template_html=template_html,
template_text=template_text,
)

@defer.inlineCallbacks
Expand Down Expand Up @@ -285,16 +290,19 @@ def on_GET(self, request, medium):
request.setResponseCode(200)
except ThreepidValidationError as e:
# Show a failure page with a reason
html = load_jinja2_template(
request.setResponseCode(e.code)

# Show a failure page with a reason
html_template = load_jinja2_templates(
self.config.email_template_dir,
self.config.email_registration_template_failure_html,
template_vars={"failure_reason": e.msg},
[self.config.email_registration_template_failure_html],
)
request.setResponseCode(e.code)

template_vars = {"failure_reason": e.msg}
html = html_template.render(**template_vars)

request.write(html.encode("utf-8"))
finish_request(request)
return None


class UsernameAvailabilityRestServlet(RestServlet):
Expand Down

0 comments on commit 2ee17d7

Please sign in to comment.