-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ [#8] Render templates with sandboxed backend
- Loading branch information
1 parent
b824e01
commit ab47a3f
Showing
3 changed files
with
63 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from django.template.backends.django import DjangoTemplates | ||
|
||
|
||
class SandboxedTemplates(DjangoTemplates): | ||
def __init__(self, params: dict) -> None: | ||
params = params.copy() | ||
params.setdefault("NAME", "django_sandboxed") | ||
# no file system paths to look up files (also blocks {% include %} etc) | ||
params.setdefault("DIRS", []) | ||
params.setdefault("APP_DIRS", False) | ||
params.setdefault("OPTIONS", {}) | ||
|
||
super().__init__(params) | ||
|
||
def get_templatetag_libraries(self, custom_libraries: dict) -> dict: | ||
""" | ||
The parent returns template tag libraries from installed | ||
applications and the supplied custom_libraries argument. | ||
""" | ||
return {} | ||
|
||
def template_dirs(self) -> list: | ||
""" | ||
The parent returns a list of directories to search for templates. | ||
We only need to render from string. | ||
""" | ||
return [] | ||
|
||
|
||
def get_sandboxed_backend() -> SandboxedTemplates: | ||
return SandboxedTemplates({}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
from typing import TYPE_CHECKING | ||
|
||
from django.conf import settings | ||
from django.core.mail import send_mail | ||
|
||
if TYPE_CHECKING: | ||
from openarchiefbeheer.accounts.models import User | ||
from openarchiefbeheer.destruction.models import DestructionList | ||
|
||
from .models import EmailConfig | ||
from .render_backend import get_sandboxed_backend | ||
|
||
|
||
def send_review_request_email( | ||
user: "User", destruction_list: "DestructionList" | ||
) -> None: | ||
config = EmailConfig.get_solo() | ||
|
||
backend = get_sandboxed_backend() | ||
template = backend.from_string(config.body_review_required) | ||
|
||
formatted_body = template.render(context={"user": user, "list": destruction_list}) | ||
|
||
send_mail( | ||
subject=config.subject_review_required, | ||
message=formatted_body, | ||
from_email=settings.DEFAULT_FROM_EMAIL, | ||
recipient_list=[user.email], | ||
fail_silently=False, | ||
) |