Skip to content

Commit

Permalink
Feature/wagtail42 chooser (#211)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
VdeJong authored Mar 17, 2023
1 parent cfa8bd3 commit 1ba77dd
Show file tree
Hide file tree
Showing 14 changed files with 87 additions and 46 deletions.
32 changes: 30 additions & 2 deletions .github/workflows/python-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ jobs:
- py310-dj40-wt30
- py310-dj40-wt40
- py310-dj40-wt41
- py310-dj40-wt42
- py310-dj41-wt40
- py310-dj41-wt41
- py310-dj41-wt42
- py38-dj32-wt215
- py38-dj32-wt216
- py38-dj32-wt30
Expand All @@ -40,7 +44,10 @@ jobs:
- py38-dj40-wt216
- py38-dj40-wt30
- py38-dj40-wt40
- py38-dj40-wt41
- py38-dj40-wt40
- py38-dj41-wt41
- py38-dj41-wt42
- py38-dj41-wt41
- py39-dj32-wt215
- py39-dj32-wt216
- py39-dj32-wt30
Expand All @@ -50,6 +57,9 @@ jobs:
- py39-dj40-wt30
- py39-dj40-wt40
- py39-dj40-wt41
- py39-dj41-wt40
- py39-dj41-wt41
- py39-dj41-wt42
include:
- python-version: 3.8
tox_env: py38-dj32-wt215
Expand All @@ -69,6 +79,12 @@ jobs:
tox_env: py38-dj40-wt40
- python-version: 3.8
tox_env: py38-dj40-wt41
- python-version: 3.8
tox_env: py38-dj41-wt40
- python-version: 3.8
tox_env: py38-dj41-wt41
- python-version: 3.8
tox_env: py38-dj41-wt42
- python-version: 3.9
tox_env: py39-dj32-wt215
- python-version: 3.9
Expand All @@ -87,6 +103,12 @@ jobs:
tox_env: py39-dj40-wt40
- python-version: 3.9
tox_env: py39-dj40-wt41
- python-version: 3.9
tox_env: py39-dj41-wt40
- python-version: 3.9
tox_env: py39-dj41-wt41
- python-version: 3.9
tox_env: py39-dj41-wt42
- python-version: "3.10"
tox_env: py310-dj32-wt215
- python-version: "3.10"
Expand All @@ -105,6 +127,12 @@ jobs:
tox_env: py310-dj40-wt40
- python-version: "3.10"
tox_env: py310-dj40-wt41
- python-version: "3.10"
tox_env: py310-dj41-wt40
- python-version: "3.10"
tox_env: py310-dj41-wt41
- python-version: "3.10"
tox_env: py310-dj41-wt42
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand All @@ -116,7 +144,7 @@ jobs:
python -m pip install --upgrade pip
pip install tox tox-gh-actions
- name: Test with tox
run: tox -e ${{ matrix.tox_env }} --index-url=https://pypi.python.org/simple/
run: tox -e ${{ matrix.tox_env }} -- index-url=https://pypi.python.org/simple/
- name: Prepare artifacts
run: mkdir -p .coverage-data && mv .coverage.* .coverage-data/
- uses: actions/upload-artifact@master
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
*********
Changelog
*********
3.22
----
* Add Wagtail 4.2 support
* Add Django 4.1 support
* Fix issue with FormChooserBlock

3.21.0
------
Expand Down
4 changes: 3 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@


install_requires = [
"wagtail>=2,<4.2",
"wagtail>=2,<4.3",
"Unidecode>=0.04.14,<2.0",
"wagtail-generic-chooser>=0.5.0,<0.6",
]

documentation_extras = [
Expand Down Expand Up @@ -68,6 +69,7 @@
"Framework :: Django :: 2.2",
"Framework :: Django :: 3.2",
"Framework :: Django :: 4",
"Framework :: Django :: 4.1",
"Framework :: Wagtail",
"Framework :: Wagtail :: 2",
"Framework :: Wagtail :: 3",
Expand Down
6 changes: 1 addition & 5 deletions tests/blocks/test_form_chooser_block.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,11 @@ def test_value_for_form(self):
block = FormChooserBlock()

self.assertEqual(block.value_for_form(self.form.pk), self.form.pk)
self.assertEqual(block.value_for_form(self.form), self.form.pk)
self.assertEqual(block.value_for_form(self.form), self.form)

def test_value_from_form(self):
block = FormChooserBlock()

# possibly a bug in wagtail as not choosing a value and submitting
# raises invalid literal for int() with base 10: ''
self.assertIsNone(block.value_from_form(""))

self.assertTrue(
isinstance(block.value_from_form(self.form.pk), self.form.__class__)
)
Expand Down
11 changes: 5 additions & 6 deletions tests/hooks/test_process_form.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
from django.contrib.auth.models import AnonymousUser
from django.test import override_settings
from django.test.client import Client

try:
from mock import patch
except ModuleNotFoundError:
from unittest.mock import patch

from wagtail.core.models import Page

from wagtailstreamforms.models import Form
from wagtailstreamforms.wagtail_hooks import process_form

from ..test_case import AppTestCase

try:
from mock import patch
except ModuleNotFoundError:
from unittest.mock import patch


class TestHook(AppTestCase):
fixtures = ["test.json"]
Expand Down
1 change: 0 additions & 1 deletion tests/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = [("wagtailstreamforms", "0001_initial")]
Expand Down
6 changes: 4 additions & 2 deletions tox.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
[tox]
envlist =
flake8
py{38,39,310}-dj{32}-wt{215,216,30,40,41}
py{38,39,310}-dj{40}-wt{216,30,40,41}
py{38,39,310}-dj{32}-wt{215,216,30,40,41,42}
py{38,39,310}-dj{40,41}-wt{216,30,40,41,42}

[gh-actions]
python =
Expand All @@ -17,11 +17,13 @@ deps =
mock
dj32: Django>=3.2,<3.3
dj40: Django>=4.0,<4.1
dj41: Django>=4.1,<4.2
wt215: wagtail>=2.15,<2.16
wt216: wagtail>=2.16,<2.17
wt30: wagtail>=3.0,<4.0
wt40: wagtail>=4.0,<4.1
wt41: wagtail>=4.1,<4.2
wt42: wagtail>=4.2,<4.3

commands =
coverage run manage.py test
Expand Down
2 changes: 1 addition & 1 deletion wagtailstreamforms/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@

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

__version__ = get_version(VERSION)
35 changes: 12 additions & 23 deletions wagtailstreamforms/blocks.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import uuid

from django import forms
from django.utils.functional import cached_property
from django.utils.safestring import mark_safe
from django.utils.translation import gettext_lazy as _
from wagtail.core import blocks

from wagtailstreamforms.models import Form
from wagtailstreamforms.wagtail_hooks import WagtailStreamFormsChooser


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


class FormChooserBlock(blocks.ChooserBlock):
target_model = Form
widget = forms.Select

def value_for_form(self, value):
if isinstance(value, self.target_model):
return value.pk
return value

def value_from_form(self, value):
if value == "":
return None
return super().value_from_form(value)

def to_python(self, value):
if value is None:
return value
else:
try:
return self.target_model.objects.get(pk=value)
except self.target_model.DoesNotExist:
return None
@cached_property
def target_model(self):
return Form

@cached_property
def widget(self):
return WagtailStreamFormsChooser()

def get_form_state(self, value):
return self.widget.get_value_data(value)


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

if form:

# check the context for an invalid form submitted to the page.
# Use that instead if it has the same unique form_reference number
invalid_form_reference = context.get("invalid_stream_form_reference")
Expand Down
1 change: 0 additions & 1 deletion wagtailstreamforms/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


class Migration(migrations.Migration):

initial = True

dependencies = [("wagtailcore", "0040_page_draft_title")]
Expand Down
1 change: 0 additions & 1 deletion wagtailstreamforms/migrations/0002_form_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class Migration(migrations.Migration):

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

operations = [
Expand Down
1 change: 0 additions & 1 deletion wagtailstreamforms/streamfield.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ def __init__(self, local_blocks=None, **kwargs):
self._child_blocks = self.base_blocks.copy()

for name, field_class in get_fields().items():

# ensure the field is a subclass of BaseField.
if not issubclass(field_class, BaseField):
raise ImproperlyConfigured(
Expand Down
1 change: 0 additions & 1 deletion wagtailstreamforms/views/copy.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ def copy(self, request, *args, **kwargs):
form = CopyForm(request.POST)

if form.is_valid():

copied = self.object.copy()
copied.title = form.cleaned_data["title"]
copied.slug = form.cleaned_data["slug"]
Expand Down
27 changes: 26 additions & 1 deletion wagtailstreamforms/wagtail_hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
from django.template.response import TemplateResponse
from django.urls import include, path, reverse
from django.utils.translation import gettext_lazy as _
from generic_chooser.views import ModelChooserViewSet
from generic_chooser.widgets import AdminChooser
from wagtail.admin import messages as wagtail_messages
from wagtail.contrib.modeladmin.helpers import AdminURLHelper, ButtonHelper
from wagtail.contrib.modeladmin.options import ModelAdmin, modeladmin_register
Expand Down Expand Up @@ -85,7 +87,7 @@ def get_buttons_for_obj(
)
)

# users that can do any form actions can vies submissions
# users that can do any form actions can view submissions
buttons.append(
self.button(
pk,
Expand Down Expand Up @@ -245,3 +247,26 @@ def process_form(page, request, *args, **kwargs):
return TemplateResponse(
request, page.get_template(request, *args, **kwargs), context
)


class WagtailStreamFormsChooserViewSet(ModelChooserViewSet):
icon = "form"
model = Form
page_title = _("Choose a form")
per_page = 10


class WagtailStreamFormsChooser(AdminChooser):
choose_one_text = _("Choose a form")
choose_another_text = _("Choose another form")
link_to_chosen_text = _("Edit this form")
model = Form
choose_modal_url_name = "wagtailstreamforms_chooser:choose"
icon = "form"


@hooks.register("register_admin_viewset")
def register_wagtailstreamforms_chooser_viewset():
return WagtailStreamFormsChooserViewSet(
"wagtailstreamforms_chooser", url_prefix="wagtailstreamforms-chooser"
)

0 comments on commit 1ba77dd

Please sign in to comment.