Skip to content

Commit

Permalink
Add missing docs and tests for API endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaudcolas committed Jan 25, 2022
1 parent e3cd9a4 commit 36fbd49
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
25 changes: 25 additions & 0 deletions docs/recipes/api-rendering.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# API rendering

For additional flexibility, django-pattern-library supports rendering patterns via an API endpoint.
This can be useful when implementing a custom UI while still using the pattern library’s Django rendering features.

The API endpoint is available at `api/v1/render-pattern`. It accepts POST requests with a JSON payload containing the following fields:

- `template_name` – the path of the template to render
- `config` – the configuration for the template, with the same data structure as the configuration files (`context` and `tags`).

Here is an example, with curl:

```bash
echo '{"template_name": "patterns/molecules/button/button.html", "config": {"context": {"target_page": {"title": "API"}}, "tags": {"pageurl":{"target_page":{"raw": "/hello-api"}}}}}' | curl -d @- http://localhost:8000/api/v1/render-pattern
```

The response will be the pattern’s rendered HTML:

```html
<a href="/hello-api" class="button">
API
</a>
```

Note compared to iframe rendering, this API always renders the pattern’s HTML standalone, never within a base template.
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ nav:
- 'Inclusion tags': 'recipes/inclusion-tags.md'
- 'Looping for tags': 'recipes/looping-for-tags.md'
- 'Pagination': 'recipes/pagination.md'
- 'API rendering': 'recipes/api-rendering.md'
- 'Reference':
- 'API and settings': 'reference/api.md'
- 'Concepts': 'reference/concepts.md'
Expand Down
5 changes: 2 additions & 3 deletions pattern_library/monkey_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ def node_render(context):
tag_overridden = False
result = ""

# Load pattern's config
current_template_name = parser.origin.template_name
# Get overriden tag config.
tag_overrides = context.get("__pattern_library_tag_overrides", {})

# Extract values for lookup from the token
Expand Down Expand Up @@ -87,7 +86,7 @@ def node_render(context):
logger.warning(
'No default or stub data defined for the "%s" tag in the "%s" template',
tag_name,
current_template_name,
parser.origin.template_name,
)

return original_node_render(context)
Expand Down
29 changes: 29 additions & 0 deletions tests/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,32 @@ def test_fragment_extended_from_variable(self):
),
"base content - extended content",
)


class APIViewsTestCase(SimpleTestCase):
def test_renders_with_tag_overrides(self):
api_endpoint = reverse("pattern_library:render_pattern_api")
response = self.client.post(
api_endpoint,
content_type="application/json",
data={
"template_name": "patterns/molecules/button/button.html",
"config": {
"context": {"target_page": {"title": "API"}},
"tags": {"pageurl": {"target_page": {"raw": "/hello-api"}}},
},
},
)
self.assertContains(response, '/hello-api')

def test_404(self):
api_endpoint = reverse("pattern_library:render_pattern_api")
response = self.client.post(
api_endpoint,
content_type="application/json",
data={
"template_name": "doesnotexist.html",
"config": {},
},
)
self.assertEqual(response.status_code, 404)

0 comments on commit 36fbd49

Please sign in to comment.