Skip to content

Commit 13267d3

Browse files
committed
Refactor bolt.jinja to bolt.templates, render templates immediately
1 parent f402c7b commit 13267d3

File tree

27 files changed

+83
-243
lines changed

27 files changed

+83
-243
lines changed

bolt-auth/bolt/auth/forms.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import unicodedata
22

3-
from bolt import forms, jinja
3+
from bolt import forms
44
from bolt.auth import authenticate, get_user_model, password_validation
55
from bolt.auth.models import User
66
from bolt.auth.tokens import default_token_generator
77
from bolt.db.forms import ModelForm
88
from bolt.exceptions import ValidationError
99
from bolt.mail import EmailMultiAlternatives
10+
from bolt.templates import Template
1011
from bolt.utils.encoding import force_bytes
1112
from bolt.utils.http import urlsafe_base64_encode
1213

@@ -279,16 +280,16 @@ def send_mail(
279280
"""
280281
Send a bolt.mail.EmailMultiAlternatives to `to_email`.
281282
"""
282-
template = jinja.environment.from_string(subject_template_name)
283+
template = Template(subject_template_name)
283284
subject = template.render(context)
284285
# Email subject *must not* contain newlines
285286
subject = "".join(subject.splitlines())
286-
template = jinja.environment.from_string(email_template_name)
287+
template = Template(email_template_name)
287288
body = template.render(context)
288289

289290
email_message = EmailMultiAlternatives(subject, body, from_email, [to_email])
290291
if html_email_template_name is not None:
291-
template = jinja.environment.from_string(html_email_template_name)
292+
template = Template(html_email_template_name)
292293
html_email = template.render(context)
293294
email_message.attach_alternative(html_email, "text/html")
294295

bolt-htmx/bolt/htmx/jinja.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import jinja2
22
from jinja2.ext import Extension
33

4-
from bolt.jinja.extensions import InclusionTagExtension
4+
from bolt.templates.jinja.extensions import InclusionTagExtension
55

66

77
class HTMXJSExtension(InclusionTagExtension):

bolt-htmx/bolt/htmx/views.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
import re
22

3-
from bolt.http import HttpResponse
4-
53

64
class HTMXViewMixin:
75
htmx_template_name = ""
86

9-
def get_template_response(self, context=None) -> HttpResponse:
7+
def render_template(self):
8+
template = super().get_template()
9+
context = self.get_context()
10+
1011
if self.is_htmx_request and self.htmx_fragment_name:
1112
from .jinja import HTMXFragmentExtension
1213

13-
template = self.get_template()
14-
if context is None:
15-
context = self.get_context()
16-
rendered = HTMXFragmentExtension.render_template_fragment(
17-
template=template,
14+
return HTMXFragmentExtension.render_template_fragment(
15+
template=template._jinja_template,
1816
fragment_name=self.htmx_fragment_name,
1917
context=context,
2018
)
21-
return HttpResponse(rendered, content_type=self.content_type)
2219

23-
return super().get_template_response(context=context)
20+
return template.render(context)
2421

2522
def get_request_handler(self):
2623
if self.is_htmx_request:

bolt-importmap/bolt/importmap/jinja.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import json
22

3-
from bolt.jinja.extensions import InclusionTagExtension
43
from bolt.runtime import settings
4+
from bolt.templates.jinja.extensions import InclusionTagExtension
55

66
from .core import Importmap
77

bolt-oauth/bolt/oauth/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
from bolt import jinja
21
from bolt.auth.mixins import LoginRequiredMixin
32
from bolt.http import HttpResponseBadRequest, HttpResponseRedirect
3+
from bolt.templates import jinja
44
from bolt.views import View
55

66
from .exceptions import (

bolt-pages/bolt/pages/pages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
import frontmatter
44
import pycmarkgfm
55

6-
from bolt.jinja import environment
76
from bolt.runtime import settings
7+
from bolt.templates.jinja import environment
88
from bolt.utils.functional import cached_property
99

1010

bolt-staff/bolt/admin/cards/base.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from enum import Enum
22

3-
from bolt import jinja
43
from bolt.admin.dates import DatetimeRange, DatetimeRangeAliases
4+
from bolt.templates import Template
55
from bolt.utils.text import slugify
66

77

@@ -39,8 +39,7 @@ def render(self, request, datetime_range):
3939
# If fixed, show that on the card too (I guess you could use description for this)
4040
else:
4141
self.datetime_range = datetime_range
42-
template = jinja.environment.get_template(self.template_name)
43-
return template.render(self.get_context())
42+
return Template(self.template_name).render(self.get_context())
4443

4544
@classmethod
4645
def view_name(cls) -> str:

bolt-staff/bolt/querystats/middleware.py

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,3 @@ def is_staff_request(request):
8686
return request.impersonator and request.impersonator.is_staff
8787

8888
return hasattr(request, "user") and request.user and request.user.is_staff
89-
90-
def process_template_response(self, request, response):
91-
# Template hasn't been rendered yet, so we can't include querystats themselves
92-
# unless we're pulling the previous page stats from the session storage
93-
if (
94-
response.context_data is not None
95-
and hasattr(_local, "querystats")
96-
and self.is_staff_request(request)
97-
):
98-
response.context_data["querystats_enabled"] = True
99-
100-
return response

bolt-staff/bolt/toolbar/jinja.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from bolt.jinja.extensions import InclusionTagExtension
21
from bolt.runtime import settings
2+
from bolt.templates.jinja.extensions import InclusionTagExtension
33
from bolt.utils.module_loading import import_string
44

55

bolt-staff/bolt/toolbar/templates/toolbar/toolbar.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
{% endif %}
2323
</div>
2424
<div class="flex items-center space-x-4">
25-
{% if querystats_enabled|default(false) %}{% include "querystats/toolbar.html" %}{% endif %}
25+
{% include "querystats/toolbar.html" %}
2626

2727
<div class="flex items-center space-x-3 transition-all">
2828
{% include "toolbar/links.html" %}

0 commit comments

Comments
 (0)