Skip to content

Commit

Permalink
Wagtail 6 (#66)
Browse files Browse the repository at this point in the history
* Update test matrix, docs
* Wagtail 6.0 upgrade consideration: Deprecated WidgetWithScript base widget class
* Update implementation of ReadonlyUUIDInput
* Drop support for Wagtail < 5.2

---------

Co-authored-by: Katherine Domingo <katherine.domingo@torchbox.com>
  • Loading branch information
katdom13 and Katherine Domingo committed May 17, 2024
1 parent e70d6f2 commit 7bf8b16
Show file tree
Hide file tree
Showing 12 changed files with 118 additions and 64 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ permissions:
contents: read # to fetch code (actions/checkout)

env:
PYTHON_LATEST: "3.11"
PYTHON_LATEST: "3.12"

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0

- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: ${{env.PYTHON_LATEST}}
cache: "pip"
Expand All @@ -32,7 +32,7 @@ jobs:
- name: 🏗️ Build
run: python -Im flit build

- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
path: ./dist

Expand All @@ -47,7 +47,7 @@ jobs:
# Mandatory for trusted publishing
id-token: write
steps:
- uses: actions/download-artifact@v3
- uses: actions/download-artifact@v4

- name: 🚀 Publish package distributions to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
Expand Down
27 changes: 14 additions & 13 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ env:
TOX_TESTENV_PASSENV: FORCE_COLOR
PIP_DISABLE_PIP_VERSION_CHECK: "1"
PIP_NO_PYTHON_VERSION_WARNING: "1"
PYTHON_LATEST: "3.11"
PYTHON_LATEST: "3.12"


jobs:

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
python-version: ${{env.PYTHON_LATEST}}
- uses: pre-commit/action@v3.0.0
Expand All @@ -44,9 +44,9 @@ jobs:
python: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: 🐍 Setup Python ${{ matrix.python }}
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python }}
- name: 📦 Install dependencies
Expand All @@ -57,13 +57,13 @@ jobs:
- name: 🏗️ Build wheel
run: python -Im flit build --format wheel

- name: 🧪 Run tox targets for Python ${{ matrix.python-version }}
- name: 🧪 Run tox targets for Python ${{ matrix.python }}
run: tox --installpkg ./dist/*.whl

- name: ⬆️ Upload coverage data
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-data
name: coverage-data-${{ matrix.python }}
path: .coverage.*
if-no-files-found: ignore
retention-days: 1
Expand All @@ -72,20 +72,21 @@ jobs:
runs-on: ubuntu-latest
needs: tests
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-python@v4
- uses: actions/setup-python@v5
with:
# Use latest Python, so it understands all syntax.
python-version: ${{env.PYTHON_LATEST}}

- run: python -Im pip install --upgrade "coverage[toml]>=7.2,<8.0"

- name: ⬇️ Download coverage data
uses: actions/download-artifact@v3
uses: actions/download-artifact@v4
with:
name: coverage-data
pattern: coverage-data-*
merge-multiple: true

- name: + Combine coverage
run: |
Expand All @@ -95,7 +96,7 @@ jobs:
echo "## Coverage summary" >> $GITHUB_STEP_SUMMARY
python -Im coverage report --format=markdown >> $GITHUB_STEP_SUMMARY
- name: 📈 Upload HTML report if check failed.
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: html-report
path: htmlcov
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ ci:
autofix_prs: false

default_language_version:
python: python3.11
python: python3.12

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
Expand Down
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

## Unreleased

- Add support for Wagtail 5.2
- Add Wagtail 5.2 and Python 3.12 in test matrices @katdom13
- Drop support for Wagtail < 5.2 @katdom13
- Add support for Wagtail 6.0/6.1, Django 5.0 and Python 3.12 @katdom13

## 0.10.0

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,11 @@ tox
To run tests for a specific environment:

```shell
tox -e python3.11-django4.2-wagtail5.2
tox -e python3.12-django5.0-wagtail6.0
```

To run a single test method in a specific environment:

```shell
tox -e python3.11-django4.2-wagtail5.2 -- tests.test.test_blocks.TestBlocks.test_block_with_features
tox -e python3.12-django5.0-wagtail6.0 -- tests.test.test_blocks.TestBlocks.test_block_with_features
```
9 changes: 4 additions & 5 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ classifiers = [
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Framework :: Django",
"Framework :: Django :: 3",
"Framework :: Django :: 3.2",
"Framework :: Django :: 4",
"Framework :: Django :: 4.1",
"Framework :: Django :: 4.2",
"Framework :: Django :: 5.0",
"Framework :: Wagtail",
"Framework :: Wagtail :: 4",
"Framework :: Wagtail :: 5",
"Framework :: Wagtail :: 6",
]

dynamic = ["version"] # will read __version__ from wagtail_footnotes/__init__.py
requires-python = ">=3.8"
dependencies = [
"Wagtail>=4.1",
"Wagtail>=5.2",
"Django>=3.2",
]

Expand All @@ -46,6 +44,7 @@ testing = [
"tox>=4.6.4,<5",
"ruff==0.3.7",
"coverage[toml]>=7.2,<8.0",
"wagtail-modeladmin>=2.0.0",
]

[project.urls]
Expand Down
1 change: 1 addition & 0 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"wagtail.images",
"wagtail.search",
"wagtail.admin",
"wagtail_modeladmin",
"wagtail.sites",
"wagtail",
"taggit",
Expand Down
24 changes: 16 additions & 8 deletions tests/test/test_widgets.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from django import forms
from django.test import TestCase
from wagtail import VERSION as WAGTAIL_VERSION

from wagtail_footnotes.widgets import ReadonlyUUIDInput

Expand All @@ -8,11 +9,18 @@ class TestWidgets(TestCase):
def test_read_only_uuid_input(self):
form = forms.Form()
form.fields["uuid"] = forms.CharField(widget=ReadonlyUUIDInput)
self.assertInHTML(
'<input type="hidden" name="uuid" id="id_uuid">',
form.as_p(),
)
self.assertInHTML(
'<script>setUUID("id_uuid");</script>',
form.as_p(),
)

if WAGTAIL_VERSION >= (6, 0):
self.assertInHTML(
'<input type="hidden" name="uuid" id="id_uuid" data-controller="read-only-uuid">',
form.as_p(),
)
else:
self.assertInHTML(
'<input type="hidden" name="uuid" id="id_uuid">',
form.as_p(),
)
self.assertInHTML(
'<script>setUUID("id_uuid");</script>',
form.as_p(),
)
15 changes: 6 additions & 9 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
min_version = 4.0

envlist =
python{3.8,3.9,3.10}-django{3.2}-wagtail{4.1,5.1,5.2}
python{3.9,3.10,3.11}-django{4.1}-wagtail{4.1,5.1,5.2}
python{3.10,3.11}-django{4.2}-wagtail{5.1,5.2}
python3.12-django4.2-wagtail5.2
python{3.8,3.9,3.10,3.11}-django4.2-wagtail{5.2,6.0,6.1}
python{3.10,3.11,3.12}-django5.0-wagtail{5.2,6.0,6.1}

[gh-actions]
python =
Expand All @@ -25,17 +23,16 @@ pass_env =
NO_COLOR

deps =
coverage[toml]>=7.2,<8.0

django3.2: Django>=3.2,<4.0
django4.1: Django>=4.1,<4.2
django4.2: Django>=4.2,<4.3
django5.0: Django>=5.0,<5.1

wagtail4.1: wagtail>=4.1,<4.2
wagtail5.1: wagtail>=5.1,<5.2
wagtail5.2: wagtail>=5.2,<5.3
wagtail6.0: wagtail>=6.0,<6.1
wagtail6.1: wagtail>=6.1,<6.2


extras = testing
install_command = python -Im pip install -U {opts} {packages}
commands =
python -Im coverage run testmanage.py test --deprecation all {posargs: -v 2}
Expand Down
5 changes: 1 addition & 4 deletions wagtail_footnotes/blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

from django.core.exceptions import ValidationError
from django.utils.safestring import mark_safe
from wagtail import VERSION as WAGTAIL_VERSION
from wagtail.blocks import RichTextBlock
from wagtail.models import Page

Expand Down Expand Up @@ -54,9 +53,7 @@ def replace_tag(match):
return mark_safe(FIND_FOOTNOTE_TAG.sub(replace_tag, html)) # noqa: S308

def render(self, value, context=None):
kwargs = {"value": value} if WAGTAIL_VERSION >= (5, 2) else {}

if not self.get_template(context=context, **kwargs):
if not self.get_template(value=value, context=context):
return self.render_basic(value, context=context)

html = super().render(value, context=context)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class CustomEditorController extends window.StimulusModule.Controller {
connect() {
setUUID(this.element.id);
}
}

window.wagtail.app.register('read-only-uuid', CustomEditorController);
74 changes: 59 additions & 15 deletions wagtail_footnotes/widgets.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,64 @@
from django.forms import HiddenInput
from wagtail.utils.widgets import WidgetWithScript
from wagtail import VERSION as WAGTAIL_VERSION


class ReadonlyUUIDInput(WidgetWithScript, HiddenInput):
"""
This isn't really read-only. It's a hidden input with an an adjacent div
showing the current value; that way we can set the value in JavaScript, but
the user can't easily change it.
"""
if WAGTAIL_VERSION >= (6, 0):
from django.forms import Media
from django.utils.safestring import mark_safe

def render_html(self, name, value, attrs):
"""Render the HTML (non-JS) portion of the field markup"""
hidden = super(WidgetWithScript, self).render(name, value, attrs)
display_value = value[:6] if value is not None else value
shown = f'<div id="{attrs["id"]}_display-value" style="padding-top: 1.2em;">{display_value}</div>'
return shown + hidden
class ReadonlyUUIDInput(HiddenInput):
"""
This isn't really read-only. It's a hidden input with an an adjacent div
showing the current value; that way we can set the value in JavaScript, but
the user can't easily change it.
"""

def render_js_init(self, id_, name, value):
return f'setUUID("{id_}");'
def render(self, name, value, attrs=None, renderer=None):
# no point trying to come up with sensible semantics for when 'id' is missing from attrs,
# so let's make sure it fails early in the process
try:
attrs["id"]
except (KeyError, TypeError) as exc:
raise TypeError(
"ReadonlyUUIDInput cannot be rendered without an 'id' attribute"
) from exc

widget_html = self.render_html(name, value, attrs)

return mark_safe(widget_html) # noqa: S308

def render_html(self, name, value, attrs):
"""Render the HTML (non-JS) portion of the field markup"""
hidden = super().render(name, value, attrs)
display_value = value[:6] if value is not None else value
shown = f'<div id="{attrs["id"]}_display-value" style="padding-top: 1.2em;">{display_value}</div>'
return shown + hidden

def build_attrs(self, *args, **kwargs):
attrs = super().build_attrs(*args, **kwargs)
attrs["data-controller"] = "read-only-uuid"
return attrs

@property
def media(self):
return Media(js=["footnotes/js/read-only-uuid-controller.js"])

else:
from wagtail.utils.widgets import WidgetWithScript

class ReadonlyUUIDInput(WidgetWithScript, HiddenInput):
"""
This isn't really read-only. It's a hidden input with an an adjacent div
showing the current value; that way we can set the value in JavaScript, but
the user can't easily change it.
"""

def render_html(self, name, value, attrs):
"""Render the HTML (non-JS) portion of the field markup"""
hidden = super(WidgetWithScript, self).render(name, value, attrs)
display_value = value[:6] if value is not None else value
shown = f'<div id="{attrs["id"]}_display-value" style="padding-top: 1.2em;">{display_value}</div>'
return shown + hidden

def render_js_init(self, id_, name, value):
return f'setUUID("{id_}");'

0 comments on commit 7bf8b16

Please sign in to comment.