Skip to content

Commit 688b0bd

Browse files
author
Andy Babic
committed
Amends from PR feedback
1 parent 597aea9 commit 688b0bd

File tree

4 files changed

+25
-9
lines changed

4 files changed

+25
-9
lines changed

docs/guides/defining-template-context.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Let's assume you have the following template:
4040
{% endif %}
4141
```
4242

43-
You might define a `yaml` file similar to this to provide fake data:
43+
You might define a YAML file similar to this to provide fake data:
4444

4545
```yaml
4646
name: My example pattern
@@ -56,12 +56,12 @@ context:
5656
link: /page2
5757
```
5858
59-
You can define a list or a dict or anything that [`PyYAML`](http://pyyaml.org/wiki/PyYAMLDocumentation) allows you to create in `yaml` format without creating a custom objects.
59+
You can define a list or a dict or anything that [`PyYAML`](http://pyyaml.org/wiki/PyYAMLDocumentation) allows you to create in YAML format without creating a custom objects.
6060

6161

6262
## Modifying template contexts with Python
6363

64-
While most objects can be faked with `yaml`, Django has a few common constructs that are difficult to replicate. For example: `Form` and `Paginator` instances. To help with this, django-pattern library allows you to register any number of 'context modifiers'. Context modifiers are simply Python functions that accept the `context` dictionary generated from the `yaml` file, and can make additions or updates to it as necessary. For convenience, they also receive the current `HttpRequest` as `request`.
64+
While most objects can be faked with YAML, Django has a few common constructs that are difficult to replicate. For example: `Form` and `Paginator` instances. To help with this, django-pattern-library allows you to register any number of 'context modifiers'. Context modifiers are simply Python functions that accept the `context` dictionary generated from the YAML file, and can make additions or updates to it as necessary. For convenience, they also receive the current `HttpRequest` as `request`.
6565

6666
Context modifiers can easily be registered using the `register_context_modifier` decorator. Here is a simple example:
6767

pattern_library/__init__.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
from .context_modifiers import register_context_modifier # NOQA
1+
from .context_modifiers import register_context_modifier
22

33
default_app_config = 'pattern_library.apps.PatternLibraryAppConfig'
44

5+
__all__ = [
6+
'DEFAULT_SETTINGS',
7+
'get_setting',
8+
'get_pattern_template_suffix',
9+
'get_pattern_base_template_name',
10+
'get_base_template_names',
11+
'get_sections',
12+
'get_pattern_context_var_name',
13+
'register_context_modifier',
14+
]
15+
516
DEFAULT_SETTINGS = {
617
# PATTERN_BASE_TEMPLATE_NAME is the template that fragments will be wrapped with.
718
# It should include any required CSS and JS and output

pattern_library/context_modifiers.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
GENERIC_CM_KEY = "__generic__"
1010
ORDER_ATTR_NAME = "__cm_order"
1111

12+
__all__ = [
13+
"ContextModifierRegistry",
14+
"register_context_modifier"
15+
]
16+
1217

1318
class ContextModifierRegistry(defaultdict):
1419
def __init__(self):
@@ -24,11 +29,11 @@ def register(self, func: Callable, template: str = None, order: int = 0) -> None
2429
)
2530
if not accepts_kwarg(func, "context"):
2631
raise ImproperlyConfigured(
27-
f"Context modifiers must accept a 'context' argument. {func} does not."
32+
f"Context modifiers must accept a 'context' keyword argument. {func} does not."
2833
)
2934
if not accepts_kwarg(func, "request"):
3035
raise ImproperlyConfigured(
31-
f"Context modifiers must accept a 'request' argument. {func} does not."
36+
f"Context modifiers must accept a 'request' keyword argument. {func} does not."
3237
)
3338

3439
key = template or GENERIC_CM_KEY

tests/tests/test_context_modifiers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,15 @@ def test_validation(self):
4646
with self.assertRaisesRegex(ImproperlyConfigured, "must be callables"):
4747
registry.register(0, template=atom_template)
4848
with self.assertRaisesRegex(
49-
ImproperlyConfigured, "must accept a 'request' argument"
49+
ImproperlyConfigured, "must accept a 'request' keyword argument"
5050
):
5151
registry.register(accepts_context_only, template=atom_template)
5252
with self.assertRaisesRegex(
53-
ImproperlyConfigured, "must accept a 'context' argument"
53+
ImproperlyConfigured, "must accept a 'context' keyword argument"
5454
):
5555
registry.register(accepts_request_only, template=atom_template)
5656

57-
def test_registred_without_ordering(self):
57+
def test_registered_without_ordering(self):
5858
registry.register(modifier_1, template=atom_template)
5959
registry.register(modifier_2, template=atom_template)
6060
registry.register(modifier_3, template=atom_template)

0 commit comments

Comments
 (0)