Skip to content

Commit

Permalink
Rename get_context to get_template_context
Browse files Browse the repository at this point in the history
  • Loading branch information
davegaeddert committed Jan 23, 2024
1 parent e05cc49 commit 9ee1c5b
Show file tree
Hide file tree
Showing 14 changed files with 46 additions and 48 deletions.
22 changes: 11 additions & 11 deletions bolt-auth/bolt/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def form_valid(self, form):
auth_login(self.request, form.get_user())
return HttpResponseRedirect(self.get_success_url())

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context[self.redirect_field_name] = self.get_redirect_url()
return context

Expand Down Expand Up @@ -130,8 +130,8 @@ def get_default_redirect_url(self):
else:
return self.request.path

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context.update(
{
"title": "Logged out",
Expand Down Expand Up @@ -168,8 +168,8 @@ def redirect_to_login(next, login_url=None, redirect_field_name=REDIRECT_FIELD_N
class PasswordContextMixin:
extra_context = None

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context.update(
{"title": self.title, "subtitle": None, **(self.extra_context or {})}
)
Expand Down Expand Up @@ -254,7 +254,7 @@ def get_response(self):
return response

# Display the "Password reset unsuccessful" page.
response = self.render_to_response(self.get_context())
response = self.render_to_response(self.get_template_context())
add_never_cache_headers(response)
return response

Expand Down Expand Up @@ -285,8 +285,8 @@ def form_valid(self, form):
auth_login(self.request, user, self.post_reset_login_backend)
return super().form_valid(form)

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
if self.validlink:
context["validlink"] = True
else:
Expand All @@ -304,8 +304,8 @@ class PasswordResetCompleteView(PasswordContextMixin, TemplateView):
template_name = "auth/password_reset_complete.html"
title = "Password reset complete"

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context["login_url"] = resolve_url(settings.LOGIN_URL)
return context

Expand Down
4 changes: 2 additions & 2 deletions bolt-dev/bolt/dev/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
class RequestsView(TemplateView):
template_name = "dev/requests.html"

def get_context(self):
ctx = super().get_context()
def get_template_context(self):
ctx = super().get_template_context()
requestlogs = RequestLog.load_json_logs()

if self.request.GET.get("log"):
Expand Down
2 changes: 1 addition & 1 deletion bolt-htmx/bolt/htmx/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class HTMXViewMixin:

def render_template(self):
template = self.get_template()
context = self.get_context()
context = self.get_template_context()

if self.is_htmx_request and self.htmx_fragment_name:
from .jinja import HTMXFragmentExtension
Expand Down
4 changes: 2 additions & 2 deletions bolt-pages/bolt/pages/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ def get_template_names(self) -> list[str]:
content_type_template_name = f"pages/{self.page.content_type}.html"
return [content_type_template_name] + super().get_template_names()

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context["page"] = self.page
return context

Expand Down
4 changes: 2 additions & 2 deletions bolt-staff/bolt/admin/cards/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ def render(self, request, datetime_range):
# If fixed, show that on the card too (I guess you could use description for this)
else:
self.datetime_range = datetime_range
return Template(self.template_name).render(self.get_context())
return Template(self.template_name).render(self.get_template_context())

@classmethod
def view_name(cls) -> str:
return f"card_{cls.get_slug()}"

def get_context(self):
def get_template_context(self):
context = {}
context["title"] = self.get_title()
context["slug"] = self.get_slug()
Expand Down
4 changes: 2 additions & 2 deletions bolt-staff/bolt/admin/cards/charts.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class ChartCard(Card):
template_name = "admin/cards/chart.html"

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context["chart_data"] = self.get_chart_data()
return context

Expand Down
12 changes: 6 additions & 6 deletions bolt-staff/bolt/admin/views/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class AdminView(AuthViewMixin, TemplateView):

default_datetime_range = DatetimeRangeAliases.LAST_365_DAYS

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context["title"] = self.get_title()
context["slug"] = self.get_slug()
context["description"] = self.get_description()
Expand Down Expand Up @@ -139,8 +139,8 @@ class AdminListView(HTMXViewMixin, AdminView):
show_search = False
allow_global_search = False

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()

# Make this available on self for usage in get_objects and other methods
self.filter = self.request.GET.get("filter", "")
Expand Down Expand Up @@ -268,8 +268,8 @@ class AdminDetailView(AdminView, DetailView):
template_name = None
nav_section = ""

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context["get_field_value"] = self.get_field_value
return context

Expand Down
8 changes: 4 additions & 4 deletions bolt-staff/bolt/admin/views/default.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def get(self):

return super().get()

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context["dashboards"] = registry.registered_dashboards
return context

Expand All @@ -29,8 +29,8 @@ class AdminSearchView(AdminView):
title = "Search"
slug = "search"

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context["searchable_views"] = registry.get_searchable_views()
context["global_search_query"] = self.request.GET.get("query", "")
return context
8 changes: 4 additions & 4 deletions bolt-staff/bolt/admin/views/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ def get_title(cls) -> str:
def get_slug(cls) -> str:
return cls.model._meta.model_name

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()

order_by = self.request.GET.get("order_by", "")
if order_by.startswith("-"):
Expand Down Expand Up @@ -121,8 +121,8 @@ def get_slug(cls) -> str:
def get_path(cls) -> str:
return f"{cls.model._meta.model_name}/<int:pk>/"

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()
context["fields"] = self.fields or ["pk"] + [
f.name for f in self.object._meta.get_fields() if not f.remote_field
]
Expand Down
4 changes: 2 additions & 2 deletions bolt-staff/bolt/querystats/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class QuerystatsView(AuthViewMixin, TemplateView):
template_name = "querystats/querystats.html"
staff_required = True # allow impersonator?

def get_context(self):
context = super().get_context()
def get_template_context(self):
context = super().get_template_context()

stored_querystats = self.request.session.get(
"querystats"
Expand Down
2 changes: 1 addition & 1 deletion bolt/templates/jinja/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ There is a default set of globals, filters, and extensions.
Each request is rendered with a `context`.
This will include the [default globals](#default-globals),
any app or project globals,
as well as the `get_context()` from your view.
as well as the `get_template_context()` from your view.

When a view is rendered,
the default context includes the `request` itself,
Expand Down
6 changes: 3 additions & 3 deletions bolt/views/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ def form_valid(self, form: "BaseForm") -> HttpResponse:
def form_invalid(self, form: "BaseForm") -> HttpResponse:
"""If the form is invalid, render the invalid form."""
context = {
**self.get_context(),
**self.get_template_context(),
"form": form,
}
return self.get_template().render(context)

def get_context(self) -> dict:
def get_template_context(self) -> dict:
"""Insert the form into the context dict."""
context = super().get_context()
context = super().get_template_context()
context["form"] = self.get_form()
return context

Expand Down
10 changes: 4 additions & 6 deletions bolt/views/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ def get_object(self): # Intentionally untyped... subclasses must override this.
f"get_object() is not implemented on {self.__class__.__name__}"
)

def get_context(self) -> dict:
def get_template_context(self) -> dict:
"""Insert the single object into the context dict."""
context = super().get_context() # type: ignore
context = super().get_template_context() # type: ignore
context["object"] = self.object
if self.context_object_name:
context[self.context_object_name] = self.object
Expand Down Expand Up @@ -80,8 +80,6 @@ class CreateView(ObjectTemplateViewMixin, FormView):
View for creating a new object, with a response rendered by a template.
"""

template_name_suffix = "_form"

def post(self) -> HttpResponse:
"""
Handle POST requests: instantiate a form instance with the passed
Expand Down Expand Up @@ -198,9 +196,9 @@ def get_objects(self):
f"get_objects() is not implemented on {self.__class__.__name__}"
)

def get_context(self) -> dict:
def get_template_context(self) -> dict:
"""Insert the single object into the context dict."""
context = super().get_context() # type: ignore
context = super().get_template_context() # type: ignore
context[self.context_object_name] = self.objects
return context

Expand Down
4 changes: 2 additions & 2 deletions bolt/views/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class TemplateView(View):

template_name: str | None = None

def get_context(self) -> dict:
def get_template_context(self) -> dict:
return {
"request": self.request,
"csrf_input": csrf_input_lazy(self.request),
Expand Down Expand Up @@ -54,7 +54,7 @@ def get_template(self) -> Template:
pass

def render_template(self) -> str:
return self.get_template().render(self.get_context())
return self.get_template().render(self.get_template_context())

def get(self):
return self.render_template()

0 comments on commit 9ee1c5b

Please sign in to comment.