kopia lustrzana https://github.com/wagtail/wagtail
No longer possible to index an abstract model
Also a bit of cleanup to centralise all "can this model be indexed?" logicpull/716/head
rodzic
7bfb1bb544
commit
ae4381c9c4
|
@ -1,8 +1,7 @@
|
|||
from django.db import models
|
||||
from django.db.models.query import QuerySet
|
||||
from django.core.exceptions import ImproperlyConfigured
|
||||
|
||||
from wagtail.wagtailsearch.index import Indexed
|
||||
from wagtail.wagtailsearch.index import class_is_indexed
|
||||
from wagtail.wagtailsearch.utils import normalise_query_string
|
||||
|
||||
|
||||
|
@ -10,13 +9,6 @@ class BaseSearch(object):
|
|||
def __init__(self, params):
|
||||
pass
|
||||
|
||||
def object_can_be_indexed(self, obj):
|
||||
# Object must be a decendant of Indexed and be a django model
|
||||
if not isinstance(obj, Indexed) or not isinstance(obj, models.Model):
|
||||
return False
|
||||
|
||||
return True
|
||||
|
||||
def reset_index(self):
|
||||
return NotImplemented
|
||||
|
||||
|
@ -47,8 +39,8 @@ class BaseSearch(object):
|
|||
model = model_or_queryset
|
||||
queryset = model_or_queryset.objects.all()
|
||||
|
||||
# Model must be a descendant of Indexed and be a django model
|
||||
if not issubclass(model, Indexed) or not issubclass(model, models.Model):
|
||||
# Model must be a class that is in the index
|
||||
if not class_is_indexed(model):
|
||||
return []
|
||||
|
||||
# Normalise query string
|
||||
|
|
|
@ -18,7 +18,7 @@ from elasticsearch import Elasticsearch, NotFoundError, RequestError
|
|||
from elasticsearch.helpers import bulk
|
||||
|
||||
from wagtail.wagtailsearch.backends.base import BaseSearch
|
||||
from wagtail.wagtailsearch.index import Indexed, SearchField, FilterField
|
||||
from wagtail.wagtailsearch.index import Indexed, SearchField, FilterField, class_is_indexed
|
||||
|
||||
|
||||
class ElasticSearchMapping(object):
|
||||
|
@ -563,7 +563,7 @@ class ElasticSearch(BaseSearch):
|
|||
|
||||
def add(self, obj):
|
||||
# Make sure the object can be indexed
|
||||
if not self.object_can_be_indexed(obj):
|
||||
if not class_is_indexed(obj.__class__):
|
||||
return
|
||||
|
||||
# Get mapping
|
||||
|
@ -573,6 +573,9 @@ class ElasticSearch(BaseSearch):
|
|||
self.es.index(self.es_index, mapping.get_document_type(), mapping.get_document(obj), id=mapping.get_document_id(obj))
|
||||
|
||||
def add_bulk(self, model, obj_list):
|
||||
if not class_is_indexed(model):
|
||||
return
|
||||
|
||||
# Get mapping
|
||||
mapping = ElasticSearchMapping(model)
|
||||
doc_type = mapping.get_document_type()
|
||||
|
@ -580,10 +583,6 @@ class ElasticSearch(BaseSearch):
|
|||
# Create list of actions
|
||||
actions = []
|
||||
for obj in obj_list:
|
||||
# Object must be a decendant of Indexed and be a django model
|
||||
if not self.object_can_be_indexed(obj):
|
||||
continue
|
||||
|
||||
# Create the action
|
||||
action = {
|
||||
'_index': self.es_index,
|
||||
|
@ -597,8 +596,8 @@ class ElasticSearch(BaseSearch):
|
|||
bulk(self.es, actions)
|
||||
|
||||
def delete(self, obj):
|
||||
# Object must be a decendant of Indexed and be a django model
|
||||
if not isinstance(obj, Indexed) or not isinstance(obj, models.Model):
|
||||
# Make sure the object can be indexed
|
||||
if not class_is_indexed(obj.__class__):
|
||||
return
|
||||
|
||||
# Get mapping
|
||||
|
|
|
@ -72,6 +72,17 @@ class Indexed(object):
|
|||
search_fields = ()
|
||||
|
||||
|
||||
def get_indexed_models():
|
||||
return [
|
||||
model for model in models.get_models()
|
||||
if issubclass(model, Indexed) and not model._meta.abstract
|
||||
]
|
||||
|
||||
|
||||
def class_is_indexed(cls):
|
||||
return issubclass(cls, Indexed) and issubclass(cls, models.Model) and not cls._meta.abstract
|
||||
|
||||
|
||||
class BaseField(object):
|
||||
suffix = ''
|
||||
|
||||
|
|
|
@ -1,22 +1,18 @@
|
|||
from optparse import make_option
|
||||
|
||||
from django.core.management.base import BaseCommand
|
||||
from django.db import models
|
||||
from django.conf import settings
|
||||
|
||||
from wagtail.wagtailsearch.index import Indexed
|
||||
from wagtail.wagtailsearch.index import Indexed, get_indexed_models
|
||||
from wagtail.wagtailsearch.backends import get_search_backend
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
def get_object_list(self):
|
||||
# Get list of indexed models
|
||||
indexed_models = [model for model in models.get_models() if issubclass(model, Indexed)]
|
||||
|
||||
# Return list of (model_name, queryset) tuples
|
||||
return [
|
||||
(model, model.get_indexed_objects())
|
||||
for model in indexed_models
|
||||
for model in get_indexed_models()
|
||||
]
|
||||
|
||||
def update_backend(self, backend_name, object_list):
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
from django.db.models.signals import post_save, post_delete
|
||||
from django.db import models
|
||||
|
||||
from wagtail.wagtailsearch.index import Indexed
|
||||
from wagtail.wagtailsearch.index import Indexed, get_indexed_models
|
||||
from wagtail.wagtailsearch.backends import get_search_backends
|
||||
|
||||
|
||||
|
@ -34,10 +33,7 @@ def post_delete_signal_handler(instance, **kwargs):
|
|||
|
||||
|
||||
def register_signal_handlers():
|
||||
# Get list of models that should be indexed
|
||||
indexed_models = [model for model in models.get_models() if issubclass(model, Indexed)]
|
||||
|
||||
# Loop through list and register signal handlers for each one
|
||||
for model in indexed_models:
|
||||
for model in get_indexed_models():
|
||||
post_save.connect(post_save_signal_handler, sender=model)
|
||||
post_delete.connect(post_delete_signal_handler, sender=model)
|
||||
|
|
Ładowanie…
Reference in New Issue