From: mathieu Date: Fri, 13 May 2016 14:49:40 +0000 (+0200) Subject: Add instruments in autocomplete index X-Git-Tag: 1.6.2^2~15^2~3^2~9 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=083105374e50b3e383618dd5255e831f11b2b1e4;p=telemeta.git Add instruments in autocomplete index --- diff --git a/app/settings.py b/app/settings.py index 04ed3439..bbb1707b 100644 --- a/app/settings.py +++ b/app/settings.py @@ -306,7 +306,11 @@ HAYSTACK_CONNECTIONS = { 'URL': 'http://search:9200/', 'INDEX_NAME': 'haystack', 'INLUDE_SPELLING': True, - 'EXCLUDED_INDEXES': ['telemeta.search_indexes.LocationIndex', 'telemeta.search_indexes.LocationAliasIndex'] + 'EXCLUDED_INDEXES': ['telemeta.search_indexes.LocationIndex', + 'telemeta.search_indexes.LocationAliasIndex', + 'telemeta.search_indexes.InstrumentIndex', + 'telemeta.search_indexes.InstrumentAliasIndex' + ] }, 'autocomplete': { # 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine', diff --git a/env/debug.env b/env/debug.env index f43f7d26..fb6db552 100644 --- a/env/debug.env +++ b/env/debug.env @@ -18,5 +18,5 @@ BROKER_URL=redis://broker:6379/0 # If this is True, all tasks will be executed locally by blocking until the task returns. CELERY_ALWAYS_EAGER=False -REINDEX=False +REINDEX=True DATASTART=False diff --git a/telemeta/models/core.py b/telemeta/models/core.py index 0b12ccfd..f45fc202 100644 --- a/telemeta/models/core.py +++ b/telemeta/models/core.py @@ -44,7 +44,7 @@ from telemeta.models.fields import * from telemeta.util.kdenlive_session import * from telemeta.util.unaccent import unaccent_icmp from xml.dom.minidom import getDOMImplementation - +from dirtyfields import DirtyFieldsMixin PUBLIC_ACCESS_CHOICES = (('none', _('none')), ('metadata', _('metadata')), ('mixed', _('mixed')), ('full', _('full'))) @@ -117,7 +117,7 @@ class EnhancedModel(models.Model): abstract = True -class ModelCore(EnhancedModel): +class ModelCore(EnhancedModel, DirtyFieldsMixin): @classmethod def required_fields(cls): diff --git a/telemeta/models/item.py b/telemeta/models/item.py index 0e68a8e1..132e2b01 100644 --- a/telemeta/models/item.py +++ b/telemeta/models/item.py @@ -30,7 +30,6 @@ from telemeta.models.identifier import * from telemeta.models.resource import * from telemeta.models.enum import * - item_published_code_regex = getattr(settings, 'ITEM_PUBLISHED_CODE_REGEX', '[A-Za-z0-9._-]*') item_unpublished_code_regex = getattr(settings, 'ITEM_UNPUBLISHED_CODE_REGEX', '[A-Za-z0-9._-]*') @@ -120,7 +119,6 @@ class MediaItem(MediaResource): 'publishing_date', 'scientist', 'topic', 'summary', 'contributor', 'public_access'] - def keywords(self): return ContextKeyword.objects.filter(item_relations__item = self) keywords.verbose_name = _('keywords') diff --git a/telemeta/models/resource.py b/telemeta/models/resource.py index c834c8c2..73cbafc2 100644 --- a/telemeta/models/resource.py +++ b/telemeta/models/resource.py @@ -25,9 +25,8 @@ from django.utils.translation import ugettext_lazy as _ from telemeta.models.core import * from telemeta.models.system import * -from dirtyfields import DirtyFieldsMixin -class MediaResource(ModelCore, DirtyFieldsMixin): +class MediaResource(ModelCore): "Base class of all media objects" def public_access_label(self): diff --git a/telemeta/search_indexes.py b/telemeta/search_indexes.py index f77128d1..d3aa0878 100644 --- a/telemeta/search_indexes.py +++ b/telemeta/search_indexes.py @@ -260,4 +260,28 @@ class LocationAliasIndex(indexes.SearchIndex, indexes.Indexable): def index_queryset(self, using=None): l = MediaItem.objects.values('location') - return LocationAlias.objects.filter(location__in=l) \ No newline at end of file + return LocationAlias.objects.filter(location__in=l) + + +class InstrumentIndex(indexes.SearchIndex, indexes.Indexable): + + text = indexes.CharField(document=True, use_template=True) + + def get_model(self): + return Instrument + + def index_queryset(self, using=None): + instrus = MediaItemPerformance.objects.values('instrument') + return Instrument.objects.filter(pk__in=instrus) + + +class InstrumentAliasIndex(indexes.SearchIndex, indexes.Indexable): + + text = indexes.CharField(document=True, use_template=True) + + def get_model(self): + return InstrumentAlias + + def index_queryset(self, using=None): + instrualias = MediaItemPerformance.objects.values('alias') + return InstrumentAlias.objects.filter(pk__in=instrualias) \ No newline at end of file diff --git a/telemeta/templates/search/indexes/telemeta/instrument_text.txt b/telemeta/templates/search/indexes/telemeta/instrument_text.txt new file mode 100644 index 00000000..28701c17 --- /dev/null +++ b/telemeta/templates/search/indexes/telemeta/instrument_text.txt @@ -0,0 +1 @@ +{{ object.name }} \ No newline at end of file diff --git a/telemeta/templates/search/indexes/telemeta/instrumentalias_text.txt b/telemeta/templates/search/indexes/telemeta/instrumentalias_text.txt new file mode 100644 index 00000000..28701c17 --- /dev/null +++ b/telemeta/templates/search/indexes/telemeta/instrumentalias_text.txt @@ -0,0 +1 @@ +{{ object.name }} \ No newline at end of file diff --git a/telemeta/util/search_signals.py b/telemeta/util/search_signals.py index 142b54e5..c9654e02 100644 --- a/telemeta/util/search_signals.py +++ b/telemeta/util/search_signals.py @@ -4,41 +4,71 @@ from telemeta.models import * from telemeta.search_indexes import * from django.db.models import Q -class RealTimeCustomSignal(signals.RealtimeSignalProcessor): - handleModels = (MediaItem,) +class RealTimeCustomSignal(signals.RealtimeSignalProcessor): + handleFields = ('location', 'instrument', 'alias') + handleModels = (MediaItem, MediaItemPerformance, ) def __init__(self, *args, **kwargs): super(RealTimeCustomSignal, self).__init__(*args, **kwargs) - self.update_model = None + self.update_fields = [] - def post_save_mediaitem(self, object): - if object.get_dirty_fields(check_relationship=True).has_key('location'): - id = object.get_dirty_fields(check_relationship=True).get('location') - locs = Location.objects.filter(Q(pk=id)|Q(past_names__pk=id)|Q(descendant_relations__location__pk=id)) - localias = LocationAlias.objects.filter(location__id=id) + def post_save_location(self, object): + id = object.get_dirty_fields(check_relationship=True).get('location') + locs = Location.objects.filter(Q(pk=id) | Q(past_names__pk=id) | Q(descendant_relations__location__pk=id)) + localias = LocationAlias.objects.filter(location__id=id) + for loc in locs: + LocationIndex().remove_object(instance=loc, using='autocomplete') + for loc in localias: + LocationAliasIndex().remove_object(instance=loc, using='autocomplete') + if object.location is not None: + locs = MediaItem.objects.filter(id=object.id).locations() + localias = LocationAlias.objects.filter(location__pk=object.location.id) for loc in locs: - LocationIndex().remove_object(instance=loc, using='autocomplete') + LocationIndex().update_object(instance=loc, using='autocomplete') for loc in localias: - LocationAliasIndex().remove_object(instance=loc, using='autocomplete') - if object.location is not None: - locs = MediaItem.objects.filter(id=object.id).locations() - localias = LocationAlias.objects.filter(location__pk=object.location.id) - for loc in locs: - LocationIndex().update_object(instance=loc, using='autocomplete') - for loc in localias: - LocationAliasIndex().update_object(instance=loc, using='autocomplete') + LocationAliasIndex().update_object(instance=loc, using='autocomplete') + + + def post_save_instrument(self, object): + id = object.get_dirty_fields(check_relationship=True).get('instrument') + if id is not None: + instru = Instrument.objects.get(pk=id) + InstrumentIndex().remove_object(instance=instru, using='autocomplete') + if object.instrument is not None: + newinstru = Instrument.objects.get(id=object.instrument.id) + InstrumentIndex().update_object(instance=newinstru, using='autocomplete') + + def post_save_alias(self, object): + id = object.get_dirty_fields(check_relationship=True).get('alias') + if id is not None: + alias = InstrumentAlias.objects.get(pk=id) + InstrumentAliasIndex().remove_object(instance=alias, using='autocomplete') + if object.alias is not None: + newalias = InstrumentAlias.objects.get(id=object.alias.id) + InstrumentAliasIndex().update_object(instance=newalias, using='autocomplete') def handle_pre_save(self, sender, instance, **kwargs): if sender in self.handleModels and instance.is_dirty(check_relationship=True): - self.update_model = sender + for field in instance.get_dirty_fields(check_relationship=True).keys(): + if field in self.handleFields: + self.update_fields.append(field) + + def handle_pre_delete(self, sender, instance, **kwargs): + InstrumentIndex().remove_object(instance=instance.instrument, using='autocomplete') + InstrumentAliasIndex().remove_object(instance=instance.alias, using='autocomplete') def handle_save(self, sender, instance, **kwargs): - if sender == self.update_model: - getattr(self, "post_save_%s" % str(sender).split('.')[-1][:-2].lower())(instance) - self.update_model = None + import sys + print(sender, self.update_fields) + sys.stdout.flush() + if sender in self.handleModels: + for field in self.update_fields: + getattr(self, "post_save_%s" % field)(instance) + del self.update_fields[:] super(RealTimeCustomSignal, self).handle_save(sender, instance, **kwargs) def setup(self): super(RealTimeCustomSignal, self).setup() - models.signals.pre_save.connect(self.handle_pre_save) \ No newline at end of file + models.signals.pre_save.connect(self.handle_pre_save) + models.signals.pre_delete.connect(self.handle_pre_delete, sender=MediaItemPerformance) diff --git a/telemeta/views/search.py b/telemeta/views/search.py index 475d3eec..89db95c6 100644 --- a/telemeta/views/search.py +++ b/telemeta/views/search.py @@ -166,26 +166,13 @@ class HaystackAdvanceSearch(SavedSearchView): return extra def autocomplete(request): + attribut = request.GET.get('attr', '') sqs = SearchQuerySet().load_all() - if request.GET.get('attr', '') == "instruments": - if request.GET.get('attr', '') == "instruments": - sqs = sqs.filter(instruments__startswith=request.GET.get('q', '')) - objets = [result.instruments for result in sqs] - #instrus = [result.instruments for result in sqs] - #elif request.GET.get('attr', '') == "location": - # sqs = sqs.filter(SQ(location_principal__startswith=request.GET.get('q', ''))|SQ(location_relation__startswith=request.GET.get('q', ''))) - # objets = [result.location_principal+result.location_relation for result in sqs] - suggestions = [] - for chaine in objets : - #for chaine in instrus: - for word in chaine.split('|'): - if word != "" and escapeAccentAndLower(request.GET.get('q', '')) in escapeAccentAndLower(word): - suggestions.append(word) - elif request.GET.get('attr', '') == "code": + if attribut == "code": sqs = sqs.filter(code__contains=request.GET.get('q', '')) suggestions = [result.code for result in sqs] - elif request.GET.get('attr', '') == "collectors": + elif attribut == "collectors": sqs = sqs.filter(collectors__startswith=request.GET.get('q', '')) collecteurs = [result.collectors for result in sqs] suggestions = [] @@ -193,8 +180,14 @@ def autocomplete(request): for word in chaine.split('; '): if word != "" and escapeAccentAndLower(request.GET.get('q', '')) in escapeAccentAndLower(word): suggestions.append(word) - elif request.GET.get('attr', '') == "location": - sqs = SearchQuerySet().using('autocomplete').filter(content__startswith=request.GET.get('q', '')) + elif attribut == "location" or attribut == "instruments": + sqs = SearchQuerySet().using('autocomplete') + + if attribut == "location": + sqs = sqs.models(Location, LocationAlias) + else: + sqs = sqs.models(Instrument, InstrumentAlias) + sqs = sqs.filter(content__startswith=request.GET.get('q', '')) suggestions = [obj.text for obj in sqs] else: suggestions = []