Skip to content

Commit

Permalink
Support Django 3.0 to 5.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ronanboiteau committed Apr 18, 2024
1 parent 9148b6b commit c7eae1a
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 15 deletions.
8 changes: 1 addition & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,10 @@ on: [push]
jobs:
run_tests:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install tox
run: pip install tox
- name: Use tox to run test suite
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

- Add official support for Django 4.0 to 4.2 with:
- Add official support for Django 4.0 to 5.0 with:
- Addition of automated test coverage
- Review of all the built-in template tags and filters (no changes needed)
- Use [tox](https://tox.wiki/) to manage test suite environment
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Django template engine to render untrusted template code
## Requirements

- Python 3.8 to 3.12
- Django 3.2 to 4.2 (officially supported in automated tests, all built-in template tags and filters reviewed)
- Django 3.2 to 5.0

## Available tools

Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[project]
name = "django_safe_template_engine"
version = "1.2.0"
dependencies = ["django >= 3.2, < 5"]
dependencies = ["django >= 3.2"]
requires-python = ">=3.8"
authors = [{ name = "Ronan Boiteau", email = "ronan@boiteau.eu" }]
maintainers = [{ name = "Ronan Boiteau", email = "ronan@boiteau.eu" }]
Expand Down
1 change: 1 addition & 0 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "tests.settings")

import django

django.setup()

pytest.main()
7 changes: 7 additions & 0 deletions src/django_safe_template_engine/trusted_filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django import VERSION
from django.template.defaultfilters import (
add,
addslashes,
Expand Down Expand Up @@ -73,6 +74,12 @@
register.filter(divisibleby, is_safe=False)
register.filter("escape", escape_filter, is_safe=True)
register.filter("escapejs", escapejs_filter)

if VERSION[0] == 5:
from django.template.defaultfilters import escapeseq

register.filter(escapeseq)

register.filter(filesizeformat, is_safe=True)
register.filter(first, is_safe=False)
register.filter(floatformat, is_safe=True)
Expand Down
16 changes: 15 additions & 1 deletion tests/filters/test_trusted_filters.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django import VERSION
from django.template.base import Template
from django.template.context import Context

Expand Down Expand Up @@ -46,13 +47,26 @@ def test_trust_default(self):
# TODO: Write test for dictsort
# TODO: Write test for dictsortreversed
# TODO: Write test for divisibleby
# TODO: Write test for escape

def test_trust_escape(self):
expected = "&lt;b&gt;T&amp;Cs&lt;/b&gt;"
result = self._render("{{ value|escape }}", context={"value": "<b>T&Cs</b>"})
assert result == expected

def test_trust_escapejs(self):
expected = "\\u003Ctest\\u0026test\\u003E"
result = self._render('{{ "<test&test>"|escapejs }}')
assert result == expected

def test_trust_escapeseq(self):
if VERSION[0] == 5:
expected = "T&amp;Cs, &#x27;Test&#x27;"
result = self._render(
'{{ value|escapeseq|join:", " }}',
context={"value": ["T&Cs", "'Test'"]},
)
assert result == expected

# FIXME
# def test_trust_filesizeformat(self):
# expected = '117.7 MB'
Expand Down
14 changes: 10 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
[tox]
envlist =
django32,
django40,
django41,
django42,
{py36, py37, py38, py39}-django30,
{py36, py37, py38, py39}-django31,
{py36, py37, py38, py39, py310}-django32,
{py38, py39, py310}-django40,
{py38, py39, py310, py311}-django41,
{py38, py39, py310, py311, py312}-django42,
{py310, py311, py312}-django50,
lint
isolated_build = true

Expand All @@ -13,10 +16,13 @@ commands =
python -m mypy --config-file mypy.ini src tests
python run_tests.py
deps =
django30: Django>=3.0,<3.1
django31: Django>=3.1,<3.2
django32: Django>=3.2,<4
django40: Django>=4.0,<4.1
django41: Django>=4.1,<4.2
django42: Django>=4.2,<5
django50: Django>=5.0,<5.1
pytest
mypy

Expand Down

0 comments on commit c7eae1a

Please sign in to comment.