Skip to main content

Indexing for Wagtail streamfields

Project description

wagtail-streamfield-index

Indexing of streamfield data in Wagtail

Installation

Install the package

pip install wagtail-streamfield-index

Add to your installed apps

INSTALLED_APPS = [
    ...
    "streamfieldindex",
    ...
]

Run python manage.py streamfieldindex to index all pages.

After indexing, your database will contain one IndexEntry for every block found in a page model.

The index will keep itself up-to-date every time a page is saved.

ModelAdmin

If you would like to see a list of indexed blocks in your wagtail admin interface, you can register the modeladmin

Make sure modeladmin app is installed:

INSTALLED_APPS = [
    ...
    "wagtail.contrib.modeladmin",
    ...
]

Register the IndexEntryAdmin in a wagtail_hooks.py file:

from wagtail.contrib.modeladmin.options import modeladmin_register
from streamfieldindex.modeladmin import IndexEntryAdmin

modeladmin_register(IndexEntryAdmin)

IndexEntry model

from streamfieldindex.models import IndexEntry

Fields

block_name The name that you gave to your block in the streamfield definition. e.g "author" or "heading" in the following example:

my_field = StreamField([
    ('author', AuthorBlock()),
    ('heading', CharBlock()),
])

For items inside list blocks, the block_name is set to the list block name with ":item" appended, since individual items inside a list block do not have a name.

block_value The string value of the block, in the form that it is stored in the streamfield. StructBlock, StreamBlock and ListBlocks have an empty string as the block_value since you can inspect the contents of those blocks by looking at their sub-blocks. If you have a complex block type such as an ImageChooser block, see the get_bound_block() method

block_path A slash-delimited path to the location of the block within the streamfield. E.g. if block_path = 5/author/title

  1. 5. The 6th block in the streamfield.
  2. author. The 6th block is named author.
  3. title. The title sub-block of author.

field_name The name of the field where the block was found.

page The page where the block was found.

Methods

get_bound_block() Returns a Wagtail BoundBlock instance of the block. See the Wagtail docs for explaination of BoundBlocks.

Example usage

If you had an author block and wanted to find all usage of that block:

from streamfieldindex.models import IndexEntry

for index_entry in IndexEntry.objects.filter(block_name="author"):
    print(index_entry.page.id) # Print the page ID where the block is found
    print(index_entry.field_name) # Print the field where the block is found
    print(index_entry.block_path) # Print a slash-separated path to the block inside the field

Contributing

Getting started

  1. Clone the repo git clone https://github.com/nhsuk/wagtail-streamfield-index.git
  2. Install dependencies pip install .[testing,linting]

Formatting

black .

Linting

flake8 .

Tests

pytest

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

wagtail-streamfield-index-0.0.4.zip (23.0 kB view details)

Uploaded Source

File details

Details for the file wagtail-streamfield-index-0.0.4.zip.

File metadata

  • Download URL: wagtail-streamfield-index-0.0.4.zip
  • Upload date:
  • Size: 23.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.7

File hashes

Hashes for wagtail-streamfield-index-0.0.4.zip
Algorithm Hash digest
SHA256 b2c96f5e8e1e5c4b283247f39f9c70d63b41a554df48fba1bd6754c58c9bfa89
MD5 e9e8041129689bdc0c63be30c593ae36
BLAKE2b-256 a066d5efe1f510b7bdbffa6f586b3eea6b1021399c6825264c1d2d36f9eae81c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page