refactor: Update docs and tests to use get_template_data() (#1161)

* refactor: update docs and tests to use get_template_data()

* refactor: fix linting

* docs: add note about difference between the two methods
This commit is contained in:
Juro Oravec 2025-05-03 12:04:10 +02:00 committed by GitHub
parent c69980493d
commit 28b61c1609
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
69 changed files with 795 additions and 725 deletions

View file

@ -1,5 +1,4 @@
import time
from typing import Any
from django.core.cache import caches
from django.template import Template
@ -33,7 +32,7 @@ class TestComponentCache:
class Cache:
enabled = True
def get_context_data(self, **kwargs: Any):
def get_template_data(self, args, kwargs, slots, context):
nonlocal did_call_get
did_call_get = True
return {}
@ -55,7 +54,7 @@ class TestComponentCache:
did_call_get = False
component.render()
# get_context_data not called because the cache entry was returned
# get_template_data not called because the cache entry was returned
assert not did_call_get
assert result == "Hello"
@ -68,7 +67,7 @@ class TestComponentCache:
class Cache:
enabled = False
def get_context_data(self, **kwargs: Any):
def get_template_data(self, args, kwargs, slots, context):
nonlocal did_call_get
did_call_get = True
return {}
@ -89,7 +88,7 @@ class TestComponentCache:
did_call_get = False
result = component.render()
# get_context_data IS called because the cache is NOT used
# get_template_data IS called because the cache is NOT used
assert did_call_get
assert result == "Hello"
@ -151,8 +150,8 @@ class TestComponentCache:
class Cache:
enabled = True
def get_context_data(self, input, **kwargs: Any):
return {"input": input}
def get_template_data(self, args, kwargs, slots, context):
return {"input": kwargs["input"]}
component = TestComponent()
component.render(
@ -200,7 +199,7 @@ class TestComponentCache:
# Custom hash method for args and kwargs
return "custom-args-and-kwargs"
def get_context_data(self, *args, **kwargs: Any):
def get_template_data(self, args, kwargs, slots, context):
return {}
component = TestComponent()