Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft: Feature: Signals to update vector index on page publish #30

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/quick-start.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,5 @@ python manage.py update_vector_indexes
```

To skip the prompt, use the `--noinput` flag.

Indexes can also be set to update automatically when a page is published, by setting `WAGTAIL_VECTOR_INDEX_UPDATE_ON_PUBLISH` to true inside your settings file.
11 changes: 11 additions & 0 deletions src/wagtail_vector_index/apps.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,18 @@
from django.apps import AppConfig
from django.conf import settings


class WagtailVectorIndexAppConfig(AppConfig):
label = "wagtail_vector_index"
name = "wagtail_vector_index"
verbose_name = "Wagtail Vector Index"

def ready(self):
update_on_publish = getattr(
settings, "WAGTAIL_VECTOR_INDEX_UPDATE_ON_PUBLISH", False
)
if update_on_publish:
# Register signals to update indexes on publish
from wagtail_vector_index import signals

signals.register_signal_handlers()
23 changes: 23 additions & 0 deletions src/wagtail_vector_index/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.apps import apps
from wagtail.signals import page_published


def run_command_on_publish(sender, **kwargs):
# Check page is live as not to leak draft content
if kwargs["instance"].live:
if hasattr(kwargs["instance"], "get_vector_index"):
index = kwargs["instance"].get_vector_index()
index.rebuild_index()


def register_signal_handlers():
from wagtail_vector_index.models import VectorIndexedMixin

indexes = [
model
for model in apps.get_models()
if issubclass(model, VectorIndexedMixin) and not model._meta.abstract
]

for index in indexes:
page_published.connect(run_command_on_publish, sender=index)
2 changes: 2 additions & 0 deletions tests/testapp/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,5 @@
},
},
}

WAGTAIL_VECTOR_INDEX_UPDATE_ON_PUBLISH = True # Defaults to false