From 5fcabaa5bacedb2886bdba6d415f65921a943096 Mon Sep 17 00:00:00 2001 From: Real-Gecko Date: Wed, 1 Sep 2021 12:23:12 +0600 Subject: [PATCH] Renamed `get_context` to `get_context_data` --- benchmarks/component_rendering.py | 4 ++-- django_components/component.py | 2 +- django_components/templatetags/component_tags.py | 2 +- tests/test_component.py | 14 +++++++------- tests/test_context.py | 12 ++++++------ tests/test_dependency_rendering.py | 2 +- tests/test_templatetags.py | 8 ++++---- 7 files changed, 22 insertions(+), 22 deletions(-) diff --git a/benchmarks/component_rendering.py b/benchmarks/component_rendering.py index 3ad695ae..e7c5cf31 100644 --- a/benchmarks/component_rendering.py +++ b/benchmarks/component_rendering.py @@ -16,7 +16,7 @@ class SlottedComponent(component.Component): class SimpleComponent(component.Component): template_name = "simple_template.html" - def get_context(self, variable, variable2="default"): + def get_context_data(self, variable, variable2="default"): return { "variable": variable, "variable2": variable2, @@ -41,7 +41,7 @@ class BreadcrumbComponent(component.Component): 'Document and website structure') ] - def get_context(self, items): + def get_context_data(self, items): if items > 4: items = 4 elif items < 0: diff --git a/django_components/component.py b/django_components/component.py index 3d343c38..e9c73722 100644 --- a/django_components/component.py +++ b/django_components/component.py @@ -49,7 +49,7 @@ class Component(metaclass=SimplifiedInterfaceMediaDefiningClass): self.instance_template = None self.slots = {} - def get_context(self, *args, **kwargs): + def get_context_data(self, *args, **kwargs): return kwargs def get_template_name(self, context=None): diff --git a/django_components/templatetags/component_tags.py b/django_components/templatetags/component_tags.py index 76334d23..3145682d 100644 --- a/django_components/templatetags/component_tags.py +++ b/django_components/templatetags/component_tags.py @@ -149,7 +149,7 @@ class ComponentNode(Node): # context method to get values to insert into the context resolved_context_args = [safe_resolve(arg, context) for arg in self.context_args] resolved_context_kwargs = {key: safe_resolve(kwarg, context) for key, kwarg in self.context_kwargs.items()} - component_context = self.component.get_context(*resolved_context_args, **resolved_context_kwargs) + component_context = self.component.get_context_data(*resolved_context_args, **resolved_context_kwargs) # Create a fresh context if requested if self.isolated_context: diff --git a/tests/test_component.py b/tests/test_component.py index 83c68b12..7b8a204e 100644 --- a/tests/test_component.py +++ b/tests/test_component.py @@ -21,7 +21,7 @@ class ComponentTest(SimpleTestCase): class SimpleComponent(component.Component): template_name = "simple_template.html" - def get_context(self, variable=None): + def get_context_data(self, variable=None): return { "variable": variable, } @@ -31,7 +31,7 @@ class ComponentTest(SimpleTestCase): js = "script.js" comp = SimpleComponent("simple_component") - context = Context(comp.get_context(variable="test")) + context = Context(comp.get_context_data(variable="test")) self.assertHTMLEqual(comp.render_dependencies(), dedent(""" @@ -61,14 +61,14 @@ class ComponentTest(SimpleTestCase): class FilteredComponent(component.Component): template_name = "filtered_template.html" - def get_context(self, var1=None, var2=None): + def get_context_data(self, var1=None, var2=None): return { "var1": var1, "var2": var2, } comp = FilteredComponent("filtered_component") - context = Context(comp.get_context(var1="test1", var2="test2")) + context = Context(comp.get_context_data(var1="test1", var2="test2")) self.assertHTMLEqual(comp.render(context), dedent(""" Var1: test1 @@ -77,7 +77,7 @@ class ComponentTest(SimpleTestCase): def test_component_with_dynamic_template(self): class SvgComponent(component.Component): - def get_context(self, name, css_class="", title="", **attrs): + def get_context_data(self, name, css_class="", title="", **attrs): return {"name": name, "css_class": css_class, "title": title, **attrs} def get_template_name(self, context): @@ -85,13 +85,13 @@ class ComponentTest(SimpleTestCase): comp = SvgComponent("svg_component") self.assertHTMLEqual( - comp.render(Context(comp.get_context(name="dynamic1"))), + comp.render(Context(comp.get_context_data(name="dynamic1"))), dedent("""\ Dynamic1 """) ) self.assertHTMLEqual( - comp.render(Context(comp.get_context(name="dynamic2"))), + comp.render(Context(comp.get_context_data(name="dynamic2"))), dedent("""\ Dynamic2 """) diff --git a/tests/test_context.py b/tests/test_context.py index 33df611b..6f8e4856 100644 --- a/tests/test_context.py +++ b/tests/test_context.py @@ -10,7 +10,7 @@ from .testutils import Django30CompatibleSimpleTestCase as SimpleTestCase class SimpleComponent(component.Component): template_name = "simple_template.html" - def get_context(self, variable=None): + def get_context_data(self, variable=None): return {"variable": variable} if variable is not None else {} @staticmethod @@ -21,7 +21,7 @@ class SimpleComponent(component.Component): class ParentComponent(component.Component): template_name = "parent_template.html" - def get_context(self): + def get_context_data(self): return { "shadowing_variable": 'NOT SHADOWED' } @@ -30,14 +30,14 @@ class ParentComponent(component.Component): class ParentComponentWithArgs(component.Component): template_name = "parent_with_args_template.html" - def get_context(self, parent_value): + def get_context_data(self, parent_value): return {"inner_parent_value": parent_value} class VariableDisplay(component.Component): template_name = "variable_display.html" - def get_context(self, shadowing_variable=None, new_variable=None): + def get_context_data(self, shadowing_variable=None, new_variable=None): context = {} if shadowing_variable is not None: context['shadowing_variable'] = shadowing_variable @@ -49,7 +49,7 @@ class VariableDisplay(component.Component): class IncrementerComponent(component.Component): template_name = "incrementer.html" - def get_context(self, value=0): + def get_context_data(self, value=0): value = int(value) if hasattr(self, 'call_count'): self.call_count += 1 @@ -65,7 +65,7 @@ class IncrementerComponent(component.Component): class OuterContextComponent(component.Component): template_name = "simple_template.html" - def get_context(self): + def get_context_data(self): return self.outer_context diff --git a/tests/test_dependency_rendering.py b/tests/test_dependency_rendering.py index a0398b85..6154d675 100644 --- a/tests/test_dependency_rendering.py +++ b/tests/test_dependency_rendering.py @@ -19,7 +19,7 @@ class SimpleComponentAlternate(component.Component): class SimpleComponentWithSharedDependency(component.Component): template_name = "simple_template.html" - def context(self, variable, variable2="default"): + def get_context_data(self, variable, variable2="default"): return { "variable": variable, "variable2": variable2, diff --git a/tests/test_templatetags.py b/tests/test_templatetags.py index 4cc8cf73..e1dfe7d1 100644 --- a/tests/test_templatetags.py +++ b/tests/test_templatetags.py @@ -10,7 +10,7 @@ from .testutils import Django30CompatibleSimpleTestCase as SimpleTestCase class SimpleComponent(component.Component): template_name = "simple_template.html" - def get_context(self, variable, variable2="default"): + def get_context_data(self, variable, variable2="default"): return { "variable": variable, "variable2": variable2, @@ -44,14 +44,14 @@ class SlottedComponentNoSlots(component.Component): class SlottedComponentWithContext(component.Component): template_name = "slotted_template.html" - def get_context(self, variable): + def get_context_data(self, variable): return {"variable": variable} class ComponentWithProvidedAndDefaultParameters(component.Component): template_name = "template_with_provided_and_default_parameters.html" - def get_context(self, variable, default_param="default text"): + def get_context_data(self, variable, default_param="default text"): return {"variable": variable, 'default_param': default_param} @@ -417,7 +417,7 @@ class ConditionalSlotTests(SimpleTestCase): class ConditionalComponent(component.Component): template_name = "conditional_template.html" - def get_context(self, branch=None): + def get_context_data(self, branch=None): return {'branch': branch} @classmethod