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 84ce22c
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 2 deletions.
5 changes: 4 additions & 1 deletion slippers/templatetags/slippers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,11 @@ def render(self, context):
attributes = {**props}

# Stage 2: Render template
ctx_dict = {**attributes, "children": children}
if request := context.get('request'):
ctx_dict['request'] = request
raw_output = template.render(
Context({**attributes, "children": children}, autoescape=context.autoescape)
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" %}
10 changes: 10 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,11 @@ 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>')
9 changes: 8 additions & 1 deletion tests/urls.py
Original file line number Diff line number Diff line change
@@ -1 +1,8 @@
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 84ce22c

Please sign in to comment.