0
0
mirror of https://github.com/wagtail/wagtail.git synced 2024-12-01 11:41:20 +01:00

Merge pull request #686 from takeflight/feature/register-signals-in-appconfigs

Use AppConfig.ready to register signal handlers
This commit is contained in:
Karl Hobley 2014-10-14 18:02:42 +01:00
commit 8380096f2c
10 changed files with 54 additions and 46 deletions

View File

@ -28,14 +28,18 @@ Firstly, add ``"wagtail.contrib.wagtailfrontendcache"`` to your INSTALLED_APPS:
"wagtail.contrib.wagtailfrontendcache"
]
.. versionchanged:: 0.8
The ``wagtailfrontendcache`` module provides a set of signal handlers which will automatically purge the cache whenever a page is published or deleted. You should register these somewhere at the top of your ``urls.py`` file:
Signal handlers are now automatically registered in Django 1.7 and upwards
The ``wagtailfrontendcache`` module provides a set of signal handlers which will automatically purge the cache whenever a page is published or deleted.
If you are using Django version 1.7 or newer, these signal handlers are automatically registered when the ``wagtail.contrib.wagtailfrontendcache`` app is loaded. Otherwise, they must be registered as your application starts up. This can be done by placing the following code in your ``urls.py``:
.. code-block:: python
# urls.py
from wagtail.contrib.wagtailfrontendcache.signal_handlers import register_signal_handlers
register_signal_handlers()

View File

@ -25,23 +25,21 @@ If the search index is kept separate from the database (when using Elasticsearch
Signal handlers
---------------
.. versionchanged:: 0.8
Signal handlers are now automatically registered in Django 1.7 and upwards
Wagtailsearch provides some signal handlers which bind to the save/delete signals of all indexed models. This would automatically add and delete them from all backends you have registered in ``WAGTAILSEARCH_BACKENDS``.
To register the signal handlers, add the following code somewhere it would be executed at startup. We reccommend adding this to your projects ``urls.py``:
If you are using Django version 1.7 or newer, these signal handlers are automatically registered when the ``wagtail.wagtailsearch`` app is loaded. Otherwise, they must be registered as your application starts up. This can be done by placing the following code in your ``urls.py``:
.. code-block: python
.. code-block:: python
# urls.py
from wagtail.wagtailsearch.signal_handlers import register_signal_handlers
register_signal_handlers()
.. note::
If your project was made with the ``wagtail start`` command, this will already be set up for you.
The ``update_index`` command
----------------------------

View File

@ -273,18 +273,6 @@ Other Django Settings Used by Wagtail
For information on what these settings do, see `Django Settings <https://docs.djangoproject.com/en/dev/ref/settings/>`__.
Search Signal Handlers
----------------------
.. code-block:: python
from wagtail.wagtailsearch import register_signal_handlers as wagtailsearch_register_signal_handlers
wagtailsearch_register_signal_handlers()
This loads Wagtail's search signal handlers, which need to be loaded very early in the Django life cycle. While not technically a urlconf, this is a convenient place to load them. Calling this function registers signal handlers to watch for when indexed models get saved or deleted. This allows wagtailsearch to update ElasticSearch automatically.
URL Patterns
------------
@ -297,8 +285,6 @@ URL Patterns
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailsearch.urls import frontend as wagtailsearch_frontend_urls
admin.autodiscover()
urlpatterns = patterns('',
url(r'^django-admin/', include(admin.site.urls)),
@ -571,13 +557,6 @@ urls.py
from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailsearch.urls import frontend as wagtailsearch_frontend_urls
admin.autodiscover()
# Signal handlers
from wagtail.wagtailsearch import register_signal_handlers as wagtailsearch_register_signal_handlers
wagtailsearch_register_signal_handlers()
urlpatterns = patterns('',
url(r'^django-admin/', include(admin.site.urls)),

33
docs/releases/0.8.rst Normal file
View File

@ -0,0 +1,33 @@
=========================
Wagtail 0.8 release notes
=========================
.. contents::
:local:
:depth: 1
What's new
==========
Minor features
~~~~~~~~~~~~~~
* Signal handlers for ``wagtail.wagtailsearch`` and ``wagtail.contrib.wagtailfrontendcache`` are automatically registered.
Bug fixes
~~~~~~~~~
*
Upgrade considerations
======================
Automatic registration of signal handlers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Signal handlers for the ``wagtailsearch`` core app and ``wagtailfrontendcache`` contrib app are automatically registered. Calls to ``register_signal_handlers`` from your ``urls.py`` can be removed.

View File

@ -1,7 +1,11 @@
from django.apps import AppConfig
from wagtail.contrib.wagtailfrontendcache.signal_handlers import register_signal_handlers
class WagtailFrontendCacheAppConfig(AppConfig):
name = 'wagtail.contrib.wagtailfrontendcache'
label = 'wagtailfrontendcache'
verbose_name = "Wagtail frontend cache"
def ready(self):
register_signal_handlers()

View File

@ -1,6 +1,5 @@
from django.db import models
from wagtail.wagtailcore.models import Page
from wagtail.wagtailcore.signals import page_published, page_unpublished
from wagtail.contrib.wagtailfrontendcache.utils import purge_page_from_cache
@ -16,6 +15,7 @@ def page_unpublished_signal_handler(instance, **kwargs):
def register_signal_handlers():
# Get list of models that are page types
Page = models.get_model('wagtailcore', 'Page')
indexed_models = [model for model in models.get_models() if issubclass(model, Page)]
# Loop through list and register signal handlers for each one

View File

@ -11,14 +11,6 @@ from wagtail.wagtaildocs import urls as wagtaildocs_urls
from wagtail.wagtailcore import urls as wagtail_urls
admin.autodiscover()
# Register search signal handlers
from wagtail.wagtailsearch.signal_handlers import register_signal_handlers as wagtailsearch_register_signal_handlers
wagtailsearch_register_signal_handlers()
urlpatterns = patterns('',
url(r'^django-admin/', include(admin.site.urls)),

View File

@ -438,9 +438,6 @@ class TestIssue613(TestCase, WagtailTestUtils):
self.search_backend = self.get_elasticsearch_backend()
self.login()
from wagtail.wagtailsearch.signal_handlers import register_signal_handlers
register_signal_handlers()
def add_document(self, **params):
# Build a fake file
fake_file = ContentFile(b("A boring example document"))

View File

@ -989,9 +989,6 @@ class TestIssue613(TestCase, WagtailTestUtils):
self.search_backend = self.get_elasticsearch_backend()
self.login()
from wagtail.wagtailsearch.signal_handlers import register_signal_handlers
register_signal_handlers()
def add_image(self, **params):
post_data = {
'title': "Test image",

View File

@ -1,7 +1,11 @@
from django.apps import AppConfig
from wagtail.wagtailsearch.signal_handlers import register_signal_handlers
class WagtailSearchAppConfig(AppConfig):
name = 'wagtail.wagtailsearch'
label = 'wagtailsearch'
verbose_name = "Wagtail search"
def ready(self):
register_signal_handlers()