Skip to content

Commit

Permalink
Refs mixxorz#45 -- Passed request to components
Browse files Browse the repository at this point in the history
  • Loading branch information
giannis-bugshell committed Apr 23, 2024
1 parent 645bbaa commit c9f32ee
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 4 deletions.
1 change: 1 addition & 0 deletions slippers/template.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
Overrides for the Django Template system to allow finer control over template parsing.
"""

import re

from django.template.base import (
Expand Down
7 changes: 4 additions & 3 deletions slippers/templatetags/slippers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,10 @@ def render(self, context):
attributes = {**props}

# Stage 2: Render template
raw_output = template.render(
Context({**attributes, "children": children}, autoescape=context.autoescape)
)
ctx_dict = {**attributes, "children": children}
if request := context.get("request"):
ctx_dict["request"] = request
raw_output = template.render(Context(ctx_dict, autoescape=context.autoescape))

output_template_section = mark_safe(extract_template_parts(raw_output)[1])

Expand Down
1 change: 1 addition & 0 deletions tests/templates/components.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ components:
special_attributes: "special_attributes.html"
type_checking: "type_checking.html"
component_code: "component_code.html"
movie: "movie.html"
1 change: 1 addition & 0 deletions tests/templates/movie.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<span>Movie: {{ title }} - Reviewer: {{ request.user.last_name }}</span>
1 change: 1 addition & 0 deletions tests/templates/tests/movie_detail.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% movie title="Monsters" %}
13 changes: 13 additions & 0 deletions tests/test_templatetags.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from unittest.mock import patch

from django.conf import settings as django_settings
from django.contrib.auth.models import User
from django.template import Context, Template, TemplateSyntaxError
from django.test import TestCase, override_settings
from django.urls import reverse

from typeguard import get_type_name

Expand Down Expand Up @@ -617,3 +619,14 @@ def test_with_variables(self):
"""

self.assertHTMLEqual(expected, Template(template).render(context))


class RequestPassingTest(TestCase):
def test_request_may_be_passed_to_component(self):
user = User.objects.create_user("_", last_name="Monocle")
self.client.force_login(user)
response = self.client.get(reverse("movie_detail"))
self.assertEqual(
response.content.decode().strip(),
"<span>Movie: Monsters - Reviewer: Monocle</span>",
)
8 changes: 7 additions & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
urlpatterns = []
from django.urls import path

from tests import views

urlpatterns = [
path("movie-detail/", views.MovieDetail.as_view(), name="movie_detail"),
]
5 changes: 5 additions & 0 deletions tests/views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.views.generic import TemplateView


class MovieDetail(TemplateView):
template_name = "tests/movie_detail.html"

0 comments on commit c9f32ee

Please sign in to comment.