Skip to content

Commit 1ba77dd

Browse files
authored
Feature/wagtail42 chooser (#211)
* Update to version 3.22 Based on #200 (comment), wagtail_generic_chooser is introduced to solve #200. Also, wagtail 4.2 support is added and Django 4.1 * format files * seperate -- from command arg * set max version for wagtail-generic-chooser
1 parent cfa8bd3 commit 1ba77dd

File tree

14 files changed

+87
-46
lines changed

14 files changed

+87
-46
lines changed

.github/workflows/python-test.yml

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ jobs:
3232
- py310-dj40-wt30
3333
- py310-dj40-wt40
3434
- py310-dj40-wt41
35+
- py310-dj40-wt42
36+
- py310-dj41-wt40
37+
- py310-dj41-wt41
38+
- py310-dj41-wt42
3539
- py38-dj32-wt215
3640
- py38-dj32-wt216
3741
- py38-dj32-wt30
@@ -40,7 +44,10 @@ jobs:
4044
- py38-dj40-wt216
4145
- py38-dj40-wt30
4246
- py38-dj40-wt40
43-
- py38-dj40-wt41
47+
- py38-dj40-wt40
48+
- py38-dj41-wt41
49+
- py38-dj41-wt42
50+
- py38-dj41-wt41
4451
- py39-dj32-wt215
4552
- py39-dj32-wt216
4653
- py39-dj32-wt30
@@ -50,6 +57,9 @@ jobs:
5057
- py39-dj40-wt30
5158
- py39-dj40-wt40
5259
- py39-dj40-wt41
60+
- py39-dj41-wt40
61+
- py39-dj41-wt41
62+
- py39-dj41-wt42
5363
include:
5464
- python-version: 3.8
5565
tox_env: py38-dj32-wt215
@@ -69,6 +79,12 @@ jobs:
6979
tox_env: py38-dj40-wt40
7080
- python-version: 3.8
7181
tox_env: py38-dj40-wt41
82+
- python-version: 3.8
83+
tox_env: py38-dj41-wt40
84+
- python-version: 3.8
85+
tox_env: py38-dj41-wt41
86+
- python-version: 3.8
87+
tox_env: py38-dj41-wt42
7288
- python-version: 3.9
7389
tox_env: py39-dj32-wt215
7490
- python-version: 3.9
@@ -87,6 +103,12 @@ jobs:
87103
tox_env: py39-dj40-wt40
88104
- python-version: 3.9
89105
tox_env: py39-dj40-wt41
106+
- python-version: 3.9
107+
tox_env: py39-dj41-wt40
108+
- python-version: 3.9
109+
tox_env: py39-dj41-wt41
110+
- python-version: 3.9
111+
tox_env: py39-dj41-wt42
90112
- python-version: "3.10"
91113
tox_env: py310-dj32-wt215
92114
- python-version: "3.10"
@@ -105,6 +127,12 @@ jobs:
105127
tox_env: py310-dj40-wt40
106128
- python-version: "3.10"
107129
tox_env: py310-dj40-wt41
130+
- python-version: "3.10"
131+
tox_env: py310-dj41-wt40
132+
- python-version: "3.10"
133+
tox_env: py310-dj41-wt41
134+
- python-version: "3.10"
135+
tox_env: py310-dj41-wt42
108136
steps:
109137
- uses: actions/checkout@v2
110138
- name: Set up Python ${{ matrix.python-version }}
@@ -116,7 +144,7 @@ jobs:
116144
python -m pip install --upgrade pip
117145
pip install tox tox-gh-actions
118146
- name: Test with tox
119-
run: tox -e ${{ matrix.tox_env }} --index-url=https://pypi.python.org/simple/
147+
run: tox -e ${{ matrix.tox_env }} -- index-url=https://pypi.python.org/simple/
120148
- name: Prepare artifacts
121149
run: mkdir -p .coverage-data && mv .coverage.* .coverage-data/
122150
- uses: actions/upload-artifact@master

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
*********
22
Changelog
33
*********
4+
3.22
5+
----
6+
* Add Wagtail 4.2 support
7+
* Add Django 4.1 support
8+
* Fix issue with FormChooserBlock
49

510
3.21.0
611
------

setup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@
1818

1919

2020
install_requires = [
21-
"wagtail>=2,<4.2",
21+
"wagtail>=2,<4.3",
2222
"Unidecode>=0.04.14,<2.0",
23+
"wagtail-generic-chooser>=0.5.0,<0.6",
2324
]
2425

2526
documentation_extras = [
@@ -68,6 +69,7 @@
6869
"Framework :: Django :: 2.2",
6970
"Framework :: Django :: 3.2",
7071
"Framework :: Django :: 4",
72+
"Framework :: Django :: 4.1",
7173
"Framework :: Wagtail",
7274
"Framework :: Wagtail :: 2",
7375
"Framework :: Wagtail :: 3",

tests/blocks/test_form_chooser_block.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,11 @@ def test_value_for_form(self):
1414
block = FormChooserBlock()
1515

1616
self.assertEqual(block.value_for_form(self.form.pk), self.form.pk)
17-
self.assertEqual(block.value_for_form(self.form), self.form.pk)
17+
self.assertEqual(block.value_for_form(self.form), self.form)
1818

1919
def test_value_from_form(self):
2020
block = FormChooserBlock()
2121

22-
# possibly a bug in wagtail as not choosing a value and submitting
23-
# raises invalid literal for int() with base 10: ''
24-
self.assertIsNone(block.value_from_form(""))
25-
2622
self.assertTrue(
2723
isinstance(block.value_from_form(self.form.pk), self.form.__class__)
2824
)

tests/hooks/test_process_form.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
from django.contrib.auth.models import AnonymousUser
22
from django.test import override_settings
33
from django.test.client import Client
4-
5-
try:
6-
from mock import patch
7-
except ModuleNotFoundError:
8-
from unittest.mock import patch
9-
104
from wagtail.core.models import Page
115

126
from wagtailstreamforms.models import Form
137
from wagtailstreamforms.wagtail_hooks import process_form
148

159
from ..test_case import AppTestCase
1610

11+
try:
12+
from mock import patch
13+
except ModuleNotFoundError:
14+
from unittest.mock import patch
15+
1716

1817
class TestHook(AppTestCase):
1918
fixtures = ["test.json"]

tests/migrations/0001_initial.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88

99
class Migration(migrations.Migration):
10-
1110
initial = True
1211

1312
dependencies = [("wagtailstreamforms", "0001_initial")]

tox.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
[tox]
22
envlist =
33
flake8
4-
py{38,39,310}-dj{32}-wt{215,216,30,40,41}
5-
py{38,39,310}-dj{40}-wt{216,30,40,41}
4+
py{38,39,310}-dj{32}-wt{215,216,30,40,41,42}
5+
py{38,39,310}-dj{40,41}-wt{216,30,40,41,42}
66

77
[gh-actions]
88
python =
@@ -17,11 +17,13 @@ deps =
1717
mock
1818
dj32: Django>=3.2,<3.3
1919
dj40: Django>=4.0,<4.1
20+
dj41: Django>=4.1,<4.2
2021
wt215: wagtail>=2.15,<2.16
2122
wt216: wagtail>=2.16,<2.17
2223
wt30: wagtail>=3.0,<4.0
2324
wt40: wagtail>=4.0,<4.1
2425
wt41: wagtail>=4.1,<4.2
26+
wt42: wagtail>=4.2,<4.3
2527

2628
commands =
2729
coverage run manage.py test

wagtailstreamforms/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22

33
# major.minor.patch.release.number
44
# release must be one of alpha, beta, rc, or final
5-
VERSION = (3, 21, 0, "final", 1)
5+
VERSION = (3, 22, 0, "final", 1)
66

77
__version__ = get_version(VERSION)

wagtailstreamforms/blocks.py

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import uuid
22

3-
from django import forms
3+
from django.utils.functional import cached_property
44
from django.utils.safestring import mark_safe
55
from django.utils.translation import gettext_lazy as _
66
from wagtail.core import blocks
77

88
from wagtailstreamforms.models import Form
9+
from wagtailstreamforms.wagtail_hooks import WagtailStreamFormsChooser
910

1011

1112
class InfoBlock(blocks.CharBlock):
@@ -18,27 +19,16 @@ def render_form(self, value, prefix="", errors=None):
1819

1920

2021
class FormChooserBlock(blocks.ChooserBlock):
21-
target_model = Form
22-
widget = forms.Select
23-
24-
def value_for_form(self, value):
25-
if isinstance(value, self.target_model):
26-
return value.pk
27-
return value
28-
29-
def value_from_form(self, value):
30-
if value == "":
31-
return None
32-
return super().value_from_form(value)
33-
34-
def to_python(self, value):
35-
if value is None:
36-
return value
37-
else:
38-
try:
39-
return self.target_model.objects.get(pk=value)
40-
except self.target_model.DoesNotExist:
41-
return None
22+
@cached_property
23+
def target_model(self):
24+
return Form
25+
26+
@cached_property
27+
def widget(self):
28+
return WagtailStreamFormsChooser()
29+
30+
def get_form_state(self, value):
31+
return self.widget.get_value_data(value)
4232

4333

4434
class WagtailFormBlock(blocks.StructBlock):
@@ -75,7 +65,6 @@ def get_context(self, value, parent_context=None):
7565
form_reference = value.get("form_reference")
7666

7767
if form:
78-
7968
# check the context for an invalid form submitted to the page.
8069
# Use that instead if it has the same unique form_reference number
8170
invalid_form_reference = context.get("invalid_stream_form_reference")

wagtailstreamforms/migrations/0001_initial.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111

1212
class Migration(migrations.Migration):
13-
1413
initial = True
1514

1615
dependencies = [("wagtailcore", "0040_page_draft_title")]

0 commit comments

Comments
 (0)