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

Allow using several custom template directories #10587

Merged
merged 10 commits into from
Aug 17, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10587.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow multiple custom directories in `read_templates`.
33 changes: 19 additions & 14 deletions synapse/config/_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ def read_template(self, filename: str) -> jinja2.Template:
def read_templates(
self,
filenames: List[str],
custom_template_directory: Optional[str] = None,
custom_template_directories: Optional[List[Optional[str]]] = None,
babolivier marked this conversation as resolved.
Show resolved Hide resolved
) -> List[jinja2.Template]:
"""Load a list of template files from disk using the given variables.

Expand All @@ -251,8 +251,8 @@ def read_templates(
Args:
filenames: A list of template filenames to read.

custom_template_directory: A directory to try to look for the templates
before using the default Synapse template directory instead.
custom_template_directories: A list of directory to try to look for the
templates before using the default Synapse template directory instead.

Raises:
ConfigError: if the file's path is incorrect or otherwise cannot be read.
Expand All @@ -262,18 +262,23 @@ def read_templates(
"""
search_directories = [self.default_template_dir]

# The loader will first look in the custom template directory (if specified) for the
# given filename. If it doesn't find it, it will use the default template dir instead
if custom_template_directory:
# Check that the given template directory exists
if not self.path_exists(custom_template_directory):
raise ConfigError(
"Configured template directory does not exist: %s"
% (custom_template_directory,)
)
# The loader will first look in the custom template directories (if specified)
# for the given filename. If it doesn't find it, it will use the default
# template dir instead.
if custom_template_directories is not None:
for custom_template_directory in custom_template_directories:
# Elements in the list might be None if they were retrieved from the
# configuration dict using config_dict.get(...).
babolivier marked this conversation as resolved.
Show resolved Hide resolved
if custom_template_directory:
# Check that the given template directory exists
if not self.path_exists(custom_template_directory):
raise ConfigError(
"Configured template directory does not exist: %s"
% (custom_template_directory,)
)

# Search the custom template directory as well
search_directories.insert(0, custom_template_directory)
# Search the custom template directory as well
search_directories.insert(0, custom_template_directory)
babolivier marked this conversation as resolved.
Show resolved Hide resolved

# TODO: switch to synapse.util.templates.build_jinja_env
loader = jinja2.FileSystemLoader(search_directories)
Expand Down
2 changes: 1 addition & 1 deletion synapse/config/account_validity.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,5 @@ def read_config(self, config, **kwargs):
"account_previously_renewed.html",
invalid_token_template_filename,
],
account_validity_template_dir,
[account_validity_template_dir],
babolivier marked this conversation as resolved.
Show resolved Hide resolved
)
6 changes: 3 additions & 3 deletions synapse/config/emailconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def read_config(self, config, **kwargs):
registration_template_success_html,
add_threepid_template_success_html,
],
template_dir,
[template_dir],
)

# Render templates that do not contain any placeholders
Expand Down Expand Up @@ -297,7 +297,7 @@ def read_config(self, config, **kwargs):
self.email_notif_template_text,
) = self.read_templates(
[notif_template_html, notif_template_text],
template_dir,
[template_dir],
)

self.email_notif_for_new_users = email_config.get(
Expand All @@ -320,7 +320,7 @@ def read_config(self, config, **kwargs):
self.account_validity_template_text,
) = self.read_templates(
[expiry_template_html, expiry_template_text],
template_dir,
[template_dir],
)

subjects_config = email_config.get("subjects", {})
Expand Down
2 changes: 1 addition & 1 deletion synapse/config/sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def read_config(self, config, **kwargs):
"sso_auth_success.html",
"sso_auth_bad_user.html",
],
self.sso_template_dir,
[self.sso_template_dir],
)

# These templates have no placeholders, so render them here
Expand Down
2 changes: 1 addition & 1 deletion synapse/module_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ def read_templates(
A list containing the loaded templates, with the orders matching the one of
the filenames parameter.
"""
return self._hs.config.read_templates(filenames, custom_template_directory)
return self._hs.config.read_templates(filenames, [custom_template_directory])


class PublicRoomListManager:
Expand Down
6 changes: 3 additions & 3 deletions tests/config/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_loading_missing_templates(self):
# contain template files
with tempfile.TemporaryDirectory() as tmp_dir:
# Attempt to load an HTML template from our custom template directory
template = self.hs.config.read_templates(["sso_error.html"], tmp_dir)[0]
template = self.hs.config.read_templates(["sso_error.html"], [tmp_dir])[0]

# If no errors, we should've gotten the default template instead

Expand Down Expand Up @@ -60,7 +60,7 @@ def test_loading_custom_templates(self):

# Attempt to load the template from our custom template directory
template = (
self.hs.config.read_templates([template_filename], tmp_dir)
self.hs.config.read_templates([template_filename], [tmp_dir])
)[0]

# Render the template
Expand All @@ -77,5 +77,5 @@ def test_loading_custom_templates(self):
def test_loading_template_from_nonexistent_custom_directory(self):
with self.assertRaises(ConfigError):
self.hs.config.read_templates(
["some_filename.html"], "a_nonexistent_directory"
["some_filename.html"], ["a_nonexistent_directory"]
)