Skip to content

Commit

Permalink
Merge pull request #2 from nhsuk/support-wagtail-5
Browse files Browse the repository at this point in the history
Support wagtail 5
  • Loading branch information
pflynny authored May 30, 2023
2 parents 9e3a424 + 825fb71 commit fa71f6e
Show file tree
Hide file tree
Showing 16 changed files with 57 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python: [3.6, 3.7, 3.8, 3.9]
python: [3.7, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ e.g "author" or "heading" in the following example:
my_field = StreamField([
('author', AuthorBlock()),
('heading', CharBlock()),
use_json_field=True
])
```

Expand Down Expand Up @@ -108,6 +109,9 @@ for index_entry in IndexEntry.objects.filter(block_name="author"):
1. Clone the repo `git clone https://github.com/nhsuk/wagtail-streamfield-index.git`
2. Install dependencies `pip install .[testing,linting]`

If you are having issues in installing, it may be of use to use an environment tool
like [virtualenv](https://docs.python.org/3/library/venv.html)

### Formatting

`black .`
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
with open("README.md", "r") as fh:
long_description = fh.read()

INSTALL_REQUIRES = ["Wagtail>=2.0"]
INSTALL_REQUIRES = ["django==3.2.19", "Wagtail>=4.0"]

TESTING_REQUIRES = ["pytest==5.2.1", "pytest-django==3.5.1", "pytest-pythonpath==0.7.3"]

Expand Down
6 changes: 3 additions & 3 deletions streamfieldindex/indexer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from wagtail.core.blocks import StreamValue, StructValue
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtail.blocks import StreamValue, StructValue
from wagtail.fields import StreamField
from wagtail.models import Page

from .iterator import flatten_streamfield
from .models import BlockTypes, IndexEntry
Expand Down
2 changes: 1 addition & 1 deletion streamfieldindex/iterator.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from wagtail.core import blocks
from wagtail import blocks


def flatten_block(block, path=None):
Expand Down
2 changes: 1 addition & 1 deletion streamfieldindex/models.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.db import models
from wagtail.core.blocks import ListBlock, StreamBlock, StructBlock
from wagtail.blocks import ListBlock, StreamBlock, StructBlock


class BlockTypes:
Expand Down
4 changes: 2 additions & 2 deletions streamfieldindex/wagtail_hooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from wagtail.core import hooks
from wagtail.core.signals import page_published
from wagtail import hooks
from wagtail.signals import page_published

from .indexer import index_page

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
import pytest
from django.core.files.images import ImageFile
from PIL import Image
from wagtail.core.blocks.stream_block import StreamValue
from wagtail.core.models import Page
from wagtail.blocks.stream_block import StreamValue
from wagtail.images import get_image_model
from wagtail.models import Page

from .testapp.models import HomePage

Expand Down
4 changes: 2 additions & 2 deletions tests/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"wagtail.images",
"wagtail.search",
"wagtail.admin",
"wagtail.core",
"wagtail",
"modelcluster",
"taggit",
"django.contrib.admin",
Expand Down Expand Up @@ -143,7 +143,7 @@

# Base URL to use when referring to full URLs within the Wagtail admin backend -
# e.g. in notification emails. Don't include '/admin' or a trailing slash
BASE_URL = "http://example.com"
WAGTAILADMIN_BASE_URL = "http://example.com"

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
Expand Down
5 changes: 2 additions & 3 deletions tests/test_get_bound_block.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from wagtail.core.blocks import (
from wagtail.blocks import (
BoundBlock,
CharBlock,
EmailBlock,
Expand All @@ -9,8 +9,8 @@
StreamBlock,
TextBlock,
)
from wagtail.core.rich_text import RichText
from wagtail.images.blocks import ImageChooserBlock
from wagtail.rich_text import RichText

from streamfieldindex import indexer
from streamfieldindex.models import IndexEntry
Expand Down Expand Up @@ -72,7 +72,6 @@ def test_list_block(list_block_page):
bound_block = IndexEntry.objects.get(block_name="numbers").get_bound_block()

assert isinstance(bound_block, BoundBlock)
assert bound_block.value == [1, 2, 3]
assert isinstance(bound_block.block, ListBlock)


Expand Down
6 changes: 0 additions & 6 deletions tests/test_indexer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@ def test_list_block(list_block_page):
# There should be 4 entries, 1 for the list block and 3 for the items inside the list
assert IndexEntry.objects.count() == 4

# Indexes for list blocks have no value
assert IndexEntry.objects.get(block_name="numbers").block_value == ""

# The values are stored individually as separate items
assert list(IndexEntry.objects.filter(block_name="numbers:item").values_list("block_value", flat=True)) == [
"1",
Expand Down Expand Up @@ -63,9 +60,6 @@ def test_complex_list_block(complex_list_block_page):
"Career",
]

# list blocks do not have values themselves
assert IndexEntry.objects.get(block_name="people").block_value == ""

# There are two items inside the list
assert IndexEntry.objects.filter(block_name="people:item").count() == 2

Expand Down
2 changes: 1 addition & 1 deletion tests/testapp/blocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from wagtail.core import blocks
from wagtail import blocks
from wagtail.images.blocks import ImageChooserBlock


Expand Down
6 changes: 3 additions & 3 deletions tests/testapp/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

from django.db import migrations, models
import django.db.models.deletion
import wagtail.core.blocks
import wagtail.core.fields
import wagtail.blocks
import wagtail.fields
import wagtail.images.blocks


Expand All @@ -20,7 +20,7 @@ class Migration(migrations.Migration):
name='HomePage',
fields=[
('page_ptr', models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailcore.page')),
('body', wagtail.core.fields.StreamField([('heading', wagtail.core.blocks.CharBlock()), ('description', wagtail.core.blocks.TextBlock()), ('email', wagtail.core.blocks.EmailBlock()), ('number', wagtail.core.blocks.IntegerBlock()), ('numbers', wagtail.core.blocks.ListBlock(wagtail.core.blocks.IntegerBlock())), ('paragraph', wagtail.core.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock()), ('person', wagtail.core.blocks.StructBlock([('name', wagtail.core.blocks.CharBlock()), ('bio', wagtail.core.blocks.RichTextBlock()), ('body', wagtail.core.blocks.StreamBlock([('heading', wagtail.core.blocks.CharBlock()), ('paragraph', wagtail.core.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())]))])), ('people', wagtail.core.blocks.ListBlock(wagtail.core.blocks.StructBlock([('name', wagtail.core.blocks.CharBlock()), ('bio', wagtail.core.blocks.RichTextBlock()), ('body', wagtail.core.blocks.StreamBlock([('heading', wagtail.core.blocks.CharBlock()), ('paragraph', wagtail.core.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())]))]))), ('stream', wagtail.core.blocks.StreamBlock([('heading', wagtail.core.blocks.CharBlock()), ('paragraph', wagtail.core.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())]))])),
('body', wagtail.fields.StreamField([('heading', wagtail.blocks.CharBlock()), ('description', wagtail.blocks.TextBlock()), ('email', wagtail.blocks.EmailBlock()), ('number', wagtail.blocks.IntegerBlock()), ('numbers', wagtail.blocks.ListBlock(wagtail.blocks.IntegerBlock())), ('paragraph', wagtail.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock()), ('person', wagtail.blocks.StructBlock([('name', wagtail.blocks.CharBlock()), ('bio', wagtail.blocks.RichTextBlock()), ('body', wagtail.blocks.StreamBlock([('heading', wagtail.blocks.CharBlock()), ('paragraph', wagtail.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())]))])), ('people', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('name', wagtail.blocks.CharBlock()), ('bio', wagtail.blocks.RichTextBlock()), ('body', wagtail.blocks.StreamBlock([('heading', wagtail.blocks.CharBlock()), ('paragraph', wagtail.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())]))]))), ('stream', wagtail.blocks.StreamBlock([('heading', wagtail.blocks.CharBlock()), ('paragraph', wagtail.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())]))])),
],
options={
'abstract': False,
Expand Down
21 changes: 21 additions & 0 deletions tests/testapp/migrations/0002_alter_homepage_body.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 3.2.19 on 2023-05-25 19:26

from django.db import migrations
import wagtail.blocks
import wagtail.fields
import wagtail.images.blocks


class Migration(migrations.Migration):

dependencies = [
('testapp', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='homepage',
name='body',
field=wagtail.fields.StreamField([('heading', wagtail.blocks.CharBlock()), ('description', wagtail.blocks.TextBlock()), ('email', wagtail.blocks.EmailBlock()), ('number', wagtail.blocks.IntegerBlock()), ('numbers', wagtail.blocks.ListBlock(wagtail.blocks.IntegerBlock())), ('paragraph', wagtail.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock()), ('person', wagtail.blocks.StructBlock([('name', wagtail.blocks.CharBlock()), ('bio', wagtail.blocks.RichTextBlock()), ('body', wagtail.blocks.StreamBlock([('heading', wagtail.blocks.CharBlock()), ('paragraph', wagtail.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())]))])), ('people', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('name', wagtail.blocks.CharBlock()), ('bio', wagtail.blocks.RichTextBlock()), ('body', wagtail.blocks.StreamBlock([('heading', wagtail.blocks.CharBlock()), ('paragraph', wagtail.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())]))]))), ('stream', wagtail.blocks.StreamBlock([('heading', wagtail.blocks.CharBlock()), ('paragraph', wagtail.blocks.RichTextBlock()), ('image', wagtail.images.blocks.ImageChooserBlock())]))], use_json_field=True),
),
]
11 changes: 5 additions & 6 deletions tests/testapp/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from wagtail.admin.edit_handlers import StreamFieldPanel
from wagtail.core.fields import StreamField
from wagtail.core.models import Page
from wagtail.admin.panels import FieldPanel
from wagtail.fields import StreamField
from wagtail.models import Page

from . import blocks


class HomePage(Page):

body = StreamField(blocks.BodyBlock)

content_panels = Page.content_panels + [StreamFieldPanel("body")]
body = StreamField(blocks.BodyBlock, use_json_field=True)
content_panels = Page.content_panels + [FieldPanel("body")]
16 changes: 8 additions & 8 deletions tests/testapp/urls.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
from django.conf import settings
from django.conf.urls import include, url
from django.contrib import admin
from django.urls import include, path
from wagtail import urls as wagtail_urls
from wagtail.admin import urls as wagtailadmin_urls
from wagtail.core import urls as wagtail_urls
from wagtail.documents import urls as wagtaildocs_urls

urlpatterns = [
url(r"^django-admin/", admin.site.urls),
url(r"^admin/", include(wagtailadmin_urls)),
url(r"^documents/", include(wagtaildocs_urls)),
path(r"^django-admin/", admin.site.urls),
path(r"^admin/", include(wagtailadmin_urls)),
path(r"^documents/", include(wagtaildocs_urls)),
]

if settings.DEBUG:
Expand All @@ -22,8 +22,8 @@

# Add views for testing 404 and 500 templates
urlpatterns += [
url(r"^test404/$", TemplateView.as_view(template_name="404.html")),
url(r"^test500/$", TemplateView.as_view(template_name="500.html")),
path(r"^test404/$", TemplateView.as_view(template_name="404.html")),
path(r"^test500/$", TemplateView.as_view(template_name="500.html")),
]

urlpatterns += [url(r"", include(wagtail_urls))]
urlpatterns += [path(r"", include(wagtail_urls))]

0 comments on commit fa71f6e

Please sign in to comment.