Skip to content

Jinja #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 42 commits into from
Closed
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
a6d58a1
Fix pattern name URL regex to account for Windows paths with backslas…
reedjones Aug 18, 2023
c9aaf57
Use the correct iframe width with resize buttons. Fix #226 (#225)
jkevingutierrez Aug 18, 2023
a330dd6
Make it possible to navigate the menu with the keyboard. Fix #202 (#207)
chris-lawton Aug 19, 2023
3aa1e38
Add Django 4.2 to the testing matrix (#220)
simkimsia Aug 19, 2023
e60e8ca
Update CHANGELOG
thibaudcolas Aug 19, 2023
115f88a
Upgrade Gr1n/setup-poetry
thibaudcolas Aug 19, 2023
4d79133
Update GitHub Actions
thibaudcolas Aug 19, 2023
bf91533
Comment poetry.lock for caching support in CI
thibaudcolas Aug 19, 2023
5c97dad
Use the officially recommended way to install Poetry in CI
thibaudcolas Aug 19, 2023
04a5ab1
Re-add explicit deploy job permissions
thibaudcolas Aug 19, 2023
c784586
Fix pipx install spec
thibaudcolas Aug 19, 2023
18c8b04
Switch back to separate tox install
thibaudcolas Aug 19, 2023
a67ae22
Skip CodeQL job when unrelated files are changed
thibaudcolas Aug 19, 2023
33bb401
Release v1.0.1
thibaudcolas Aug 19, 2023
035b1c3
Use poetry-core build backend (#232)
mweinelt Sep 29, 2023
09ef4d4
Update CHANGELOG
Sep 29, 2023
34f3072
Make sure /app is always in sys.path (#234)
Oct 23, 2023
a294ccd
Fix pattern matching for templates using dashes (#230)
zerolab Oct 25, 2023
9f57896
Update CHANGELOG
Oct 25, 2023
d16bcc2
Explicitly allow Django 4.2 (#231)
zerolab Oct 25, 2023
76a4690
Update CHANGELOG
Oct 25, 2023
8aa4f26
Bump version to 1.1.0
Oct 25, 2023
a22f346
Fix heading for 1.1.0 tag in CHANGELOG
Oct 25, 2023
0c64a25
Add date for 1.1.0 release
Oct 25, 2023
052b2ad
Update related-projects.md
thibaudcolas Nov 11, 2023
189f3c9
Update pyproject with Django and Poetry newer versions
maribedran Dec 15, 2023
626438a
Fix error accessing pattern template name
maribedran Dec 15, 2023
f6896c5
Wrap rendered pattern HTML with mark_safe to render correctly on temp…
maribedran Dec 15, 2023
351ca10
Merge remote-tracking branch 'torchbox/main' into jinja
maribedran Dec 15, 2023
1612e05
Fix typos discovered by codespell (#238)
cclauss Jan 9, 2024
dfc31df
Upgrade GitHub Actions versions (#237)
cclauss Jan 9, 2024
e41b502
fix: make it work for django5.0 (updated) (#241)
alxbridge Jan 16, 2024
1c46bc7
Improve handling of non-string values for 'override_tag's 'default_ht…
alxbridge Jan 16, 2024
5efd3a0
Updates for version 1.2.0
alxbridge Jan 16, 2024
829f7ac
Add Python 3.12 to the test matrix, drop Django 4.1 (#242)
cclauss Jan 17, 2024
6810527
Merge branch 'main' of https://github.com/torchbox/django-pattern-lib…
CuriousLearner Feb 24, 2024
6a73acb
Update poetry lock file
CuriousLearner Feb 26, 2024
10b90af
fix: linting
CuriousLearner Feb 26, 2024
2592c71
Run isort
CuriousLearner Feb 26, 2024
13e6632
Merge pull request #1 from lincolnloop/upgrade-jinja-django-5
mtrythall Feb 26, 2024
34a6ff9
feat: Rendered code view in Template output (#2)
CuriousLearner Apr 2, 2024
6a4db66
fix: Pass request object to render_string (#3)
CuriousLearner Apr 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 37 additions & 49 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -8,45 +8,41 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: actions/checkout@v4
- run: pipx install "poetry>=1.1.12,<2"
- uses: actions/setup-python@v5
with:
python-version: "3.10"
- uses: Gr1n/setup-poetry@v7
with:
poetry-version: '1.1.12'
python-version: "3.12"
cache: 'poetry'
- run: pip install tox
- run: tox -e lint,py310-dj40
- run: tox -e lint,py312-dj50
test_compatibility:
needs: test
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include:
# Test with all supported Django versions, for all compatible Python versions.
# See https://docs.djangoproject.com/en/4.0/faq/install/#what-python-version-can-i-use-with-django for the official matrix.
# See https://docs.djangoproject.com/en/dev/faq/install/#what-python-version-can-i-use-with-django for the official matrix.
# Additionally test on Django’s main branch with the most recent Python version.
- python: "3.7"
toxenv: py37-dj32
- python: "3.8"
toxenv: py38-dj32,py38-dj40,py38-dj41
toxenv: py38-dj32,py38-dj42
- python: "3.9"
toxenv: py39-dj32,py39-dj40,py39-dj41
toxenv: py39-dj32,py39-dj42
- python: "3.10"
# Skip testing Django 4.0, already tested in previous workflow job.
toxenv: py310-dj32,py310-dj41,py310-djmain
# Tentative support for next Python pre-release. For the correct specifier,
# Check: https://raw.githubusercontent.com/actions/python-versions/main/versions-manifest.json.
- python: "3.11.0-beta.2"
toxenv: py311-dj41
toxenv: py310-dj32,py310-dj42,py310-dj50,py310-djmain
- python: "3.11"
toxenv: py311-dj42,py311-dj50,py311-djmain
- python: "3.12"
toxenv: py312-dj42,py312-djmain
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: actions/checkout@v4
- run: pipx install "poetry>=1.1.12,<2"
- uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
- uses: Gr1n/setup-poetry@v7
with:
poetry-version: '1.1.12'
allow-prereleases: true
- run: pip install tox
- run: tox -q
env:
@@ -55,31 +51,24 @@ jobs:
needs: test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'
- id: node-cache
uses: actions/cache@v2
uses: actions/cache@v3
with:
# Cache node_modules rather than the npm cache, as we rarely update npm packages.
path: node_modules
key: ${{ runner.os }}-node-${{ hashFiles('**/.nvmrc') }}-${{ hashFiles('**/package-lock.json') }}
- if: steps.node-cache.outputs.cache-hit != 'true'
run: npm ci --no-audit
- run: npm run build
- uses: actions/setup-python@v3
with:
python-version: "3.10"
- uses: Gr1n/setup-poetry@v7
with:
poetry-version: '1.1.12'
- uses: actions/cache@v3
- run: pipx install "poetry>=1.1.12,<2"
- uses: actions/setup-python@v5
with:
path: ~/.cache/pip
key: ${{ runner.os }}-python-py310-${{ hashFiles('**/pyproject.toml') }}
restore-keys: |
${{ runner.os }}-python-py310-
- run: poetry config virtualenvs.create false
python-version-file: pyproject.toml
cache: 'poetry'
- run: poetry install
- run: poetry run django-admin runserver --settings=tests.settings.production --pythonpath=. &
# Docs website build.
@@ -95,20 +84,19 @@ jobs:
- run: cat pyproject.toml | awk '{sub(/^version = .+/,"version = \"0.0.0.dev\"")}1' > pyproject.toml.tmp && mv pyproject.toml.tmp pyproject.toml
- run: poetry build
- run: mv dist site
- uses: actions/upload-artifact@v3
- uses: actions/configure-pages@v4
- uses: actions/upload-pages-artifact@v3
with:
name: site
path: site
retention-days: 1
deploy_site:
runs-on: ubuntu-latest
needs: build_site
runs-on: ubuntu-latest
permissions:
pages: write
id-token: write
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
if: github.event_name == 'push' && github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v3
- uses: actions/download-artifact@v3
- uses: JamesIves/github-pages-deploy-action@v4.3.3
with:
branch: gh-pages
folder: site
clean: true
- uses: actions/deploy-pages@v4
10 changes: 10 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -3,9 +3,19 @@ name: "CodeQL"
on:
push:
branches: [main]
paths-ignore:
- '**/*.md'
- '**/*.yml'
- '**/*.html'
- '**/*.scss'
pull_request:
# The branches below must be a subset of the branches above
branches: [main]
paths-ignore:
- '**/*.md'
- '**/*.yml'
- '**/*.html'
- '**/*.scss'
schedule:
- cron: "28 20 * * 5"

1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -34,7 +34,6 @@ var/
*.egg-info/
.installed.cfg
*.egg
poetry.lock
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
50 changes: 49 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,56 @@
# Changelog

## [Unreleased]
## [1.2.1dev2](https://github.com/lincolnloop/django-pattern-library/releases/tag/v1.2.1dev2) - 2024-04-02

### Fixed

- Pass request object to HTML rendered code view ([#3](https://github.com/lincolnloop/django-pattern-library/pull/3))

## [1.2.1dev1](https://github.com/lincolnloop/django-pattern-library/releases/tag/v1.2.1dev1) - 2024-04-02

### Added

- Add HTML rendered code view in `Template output` column ([#2](https://github.com/lincolnloop/django-pattern-library/pull/2))

## [1.2.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.2.0) - 2024-01-16

### Added

- Add support for Django 5.0 ([#241](https://github.com/torchbox/django-pattern-library/pull/241))

### Changed

- From Django >= 4.0, calls to `Node.render()` must always return a string, but this app previously allowed non-string values to be passed in the `default_html` parameter to `override_tag`. Passing a non-string now raises a `TypeError` when using Django >= 4.0, and raises a warning for older versions ([issue #211](https://github.com/torchbox/django-pattern-library/issues/211)).

## [1.1.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.1.0) - 2023-10-25

### Added

- Add support for Django 4.2 ([#231](https://github.com/torchbox/django-pattern-library/pull/231))

### Changed

- Switch to the `poetry-core` build backend ([#232](https://github.com/torchbox/django-pattern-library/pull/232))

### Removed

- Drop support for Python 3.7 ([#231](https://github.com/torchbox/django-pattern-library/pull/231))
- Drop support for Django 4.0 ([#231](https://github.com/torchbox/django-pattern-library/pull/231))

### Fixed

- Ensure the project root is on `sys.path` so tests etc. can be run in by Docker Compose ([#233](https://github.com/torchbox/django-pattern-library/issues/233), [#234](https://github.com/torchbox/django-pattern-library/pull/234))
- Fix URL pattern matching for template with dashes in the file name ([#229](https://github.com/torchbox/django-pattern-library/issues/229), [#230](https://github.com/torchbox/django-pattern-library/pull/230))

## [1.0.1](https://github.com/torchbox/django-pattern-library/releases/tag/v1.0.1) - 2023-08-19

### Fixed

- Disable pointer events on menu chevron to allow clicks ([#202](https://github.com/torchbox/django-pattern-library/issues/202), [#205](https://github.com/torchbox/django-pattern-library/pull/205))
- Improve menu accessibility by using buttons for menu items ([#202](https://github.com/torchbox/django-pattern-library/issues/202), [#207](https://github.com/torchbox/django-pattern-library/pull/207)).
- Fix pattern name URL regex to account for Windows paths with backslash ([#222](https://github.com/torchbox/django-pattern-library/issues/222), [#223](https://github.com/torchbox/django-pattern-library/pull/223))
- Use the correct iframe width with resize buttons ([#226](https://github.com/torchbox/django-pattern-library/issues/226), [#225](https://github.com/torchbox/django-pattern-library/pull/225)).
- Update the project’s test matrix for upcoming Django 4.2 support ([#212](https://github.com/torchbox/django-pattern-library/issues/212),[#220](https://github.com/torchbox/django-pattern-library/pull/220)).

## [1.0.0](https://github.com/torchbox/django-pattern-library/releases/tag/v1.0.0) - 2022-06-10

1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -7,6 +7,7 @@ RUN useradd --create-home dpl && \
chown -R dpl:dpl /venv/ /app/

ENV PATH=/venv/bin:/home/dpl/.local/bin:$PATH \
PYTHONPATH=/app/ \
VIRTUAL_ENV=/venv/ \
DJANGO_SETTINGS_MODULE=tests.settings.dev

2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -7,6 +7,8 @@ services:
PYTHONDONTWRITEBYTECODE: 1
ports:
- "8000:8000"
stdin_open: true
tty: true
volumes:
- type: bind
source: .
2 changes: 1 addition & 1 deletion docs/community/related-projects.md
Original file line number Diff line number Diff line change
@@ -8,7 +8,7 @@ Here are other projects that are related to django-pattern-library, and may be r
- [django-components](https://github.com/EmilStenstrom/django-components/) – Reusable UI components for Django, going further than template partials.
- [django-component-tags](https://github.com/syse-i/django-component-tags) – Create advanced HTML components using Django Tags.
- [slippers](https://github.com/mixxorz/slippers) – Reusable components for Django, without writing a single line of Python.
- (Jinja only, incompatible but interesting) [Template Components (tcom)](https://tcom.scaletti.dev/) – Write server-side components as single Jinja template files. Use them as HTML tags without doing any importing.
- (Jinja only, incompatible but interesting) [JinjaX](https://jinjax.scaletti.dev/) – Write server-side components as single Jinja template files. Use them as HTML tags without doing any importing.

## Alternatives

2 changes: 1 addition & 1 deletion docs/guides/automated-tests.md
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@
Although pattern libraries often start as tools for manual tests during development, they can also be useful for automated UI testing. There are a few benefits to doing UI tests with a pattern library:

- Test the components in isolation. When tests fail, you will know exactly which component has issues, rather than having to inspect whole pages to understand what might have changed.
- Test the components with mock data. One of the issues with UI tests is to have test data for your UIs to render – you can reuse the pattern library data for this purpose (althoug there are [limitations](../guides/multiple-variants.md)).
- Test the components with mock data. One of the issues with UI tests is to have test data for your UIs to render – you can reuse the pattern library data for this purpose (although there are [limitations](../guides/multiple-variants.md)).

## Setting up automated UI tests

2 changes: 1 addition & 1 deletion docs/reference/api.md
Original file line number Diff line number Diff line change
@@ -6,7 +6,7 @@ YAML isn’t everyone’s favorite markup language, but it has the advantage of

Here is what you need to know:

- Use `.yaml` or `.yml` as the file extension for pattern configuration files. If both are present, the `.yaml` file takes precendence.
- Use `.yaml` or `.yml` as the file extension for pattern configuration files. If both are present, the `.yaml` file takes precedence.
- Use Mappings in place of Python Dictionaries.
- Use Sequences in place of Python lists (or iterables like QuerySets).
- The pattern library uses [PyYAML](https://pyyaml.org/wiki/PyYAMLDocumentation) in particular
5 changes: 3 additions & 2 deletions pattern_library/loader_tags.py
Original file line number Diff line number Diff line change
@@ -151,9 +151,10 @@ def do_include(parser, token):
isolated_context=isolated_context,
)


def visit_extends(self, node, frame):
"""This method overrides the jinja extends tag
Is called as part of the compiler CodeGenerator
Is called as part of the compiler CodeGenerator
and adds a line to use the template_new_context as
part of the runtime render to pull in the dpl context
Handles visiting extends
@@ -196,4 +197,4 @@ def template_new_context(

return new_context(
self.environment, self.name, self.blocks, vars, shared, self.globals, locals
)
)
6 changes: 1 addition & 5 deletions pattern_library/management/commands/render_patterns.py
Original file line number Diff line number Diff line change
@@ -5,11 +5,7 @@
from django.test.client import RequestFactory

from pattern_library import get_base_template_names, get_pattern_base_template_name
from pattern_library.utils import (
get_pattern_context,
render_pattern,
get_renderer,
)
from pattern_library.utils import get_pattern_context, get_renderer, render_pattern


class Command(BaseCommand):
36 changes: 30 additions & 6 deletions pattern_library/monkey_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import inspect
import logging
from typing import Optional
import typing
import warnings

import django
from django.template.library import SimpleNode
@@ -11,7 +13,9 @@


def override_tag(
register: django.template.Library, name: str, default_html: Optional[str] = None
register: django.template.Library,
name: str,
default_html: typing.Optional[typing.Any] = UNSPECIFIED,
):
"""
An utility that helps you override original tags for use in your pattern library.
@@ -29,7 +33,7 @@ def node_render(context):
tag_overridden = False
result = ""

# Get overriden tag config.
# Get overridden tag config.
tag_overrides = context.get("__pattern_library_tag_overrides", {})

# Extract values for lookup from the token
@@ -79,6 +83,23 @@ def node_render(context):
# See https://github.com/torchbox/django-pattern-library/issues/166.
return str(result)
elif default_html is not UNSPECIFIED:
# Ensure default_html is a string.
if not isinstance(default_html, str):
# Save the caller for the override tag in case it's needed for error reporting.
trace = inspect.stack()[1]
if django.VERSION < (4, 0):
warnings.warn(
"default_html argument to override_tag should be a string to ensure compatibility "
'with Django >= 4.0 (line %s in "%s")'
% (trace.lineno, trace.filename),
Warning,
)
else:
raise TypeError(
'default_html argument to override_tag must be a string (line %s in "%s")'
% (trace.lineno, trace.filename)
)

# Render provided default;
# if no stub data supplied.
return default_html
@@ -97,23 +118,26 @@ def node_render(context):

return tag_func


# have to export the original jinja visit Extends
# in the case jinja tags are being overriden
jinja_visit_Extends = None


def override_jinja_tags():
"""
Overrides jinja extends and include tags for use in your pattern library.
Call it in your settings to override tags
Call it in your settings to override tags
"""
global jinja_visit_Extends
try:
from jinja2.compiler import CodeGenerator as JinjaCodeGenerator
from jinja2.environment import Template as JinjaTemplate
except ModuleNotFoundError:
ModuleNotFoundError("install jinja2 to override jinja tags")

from .loader_tags import template_new_context, visit_extends

jinja_visit_Extends = JinjaCodeGenerator.visit_Extends
JinjaTemplate.new_context = template_new_context
JinjaCodeGenerator.visit_Extends = visit_extends
JinjaCodeGenerator.visit_Extends = visit_extends
Loading