]> git.parisson.com Git - telemeta.git/commitdiff
Add instruments in autocomplete index
authormathieu <mathieu.boye28@gmail.com>
Fri, 13 May 2016 14:49:40 +0000 (16:49 +0200)
committermathieu <mathieu.boye28@gmail.com>
Fri, 13 May 2016 14:49:40 +0000 (16:49 +0200)
app/settings.py
env/debug.env
telemeta/models/core.py
telemeta/models/item.py
telemeta/models/resource.py
telemeta/search_indexes.py
telemeta/templates/search/indexes/telemeta/instrument_text.txt [new file with mode: 0644]
telemeta/templates/search/indexes/telemeta/instrumentalias_text.txt [new file with mode: 0644]
telemeta/util/search_signals.py
telemeta/views/search.py

index 04ed343937cf661f093fa9855b1217c1c15a4e4c..bbb1707bacd5f82f9814a7c66fffb9b400685283 100644 (file)
@@ -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',
index f43f7d26c85c43fa8e4d859932316f4eab91db5a..fb6db5528d91956548680031ccf49f8e48e1a2ab 100644 (file)
@@ -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
index 0b12ccfdd3efadf74591141c35fcc2fd824b0805..f45fc2023295766991db8c4c30457ad8156ee951 100644 (file)
@@ -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):
index 0e68a8e1f81f73650427722024a2add628d66ca5..132e2b01057e9bfe64acb208ba62e24b40465e44 100644 (file)
@@ -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')
index c834c8c2079e54f3ecc5bf43f52c0d121c3433cf..73cbafc2f5fa4d379a695559919be563b46edb5b 100644 (file)
@@ -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):
index f77128d1ad4a0e1c1f63e0c0ded69597868553be..d3aa087817f7ca923ca064700a4046c7067a08b5 100644 (file)
@@ -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 (file)
index 0000000..28701c1
--- /dev/null
@@ -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 (file)
index 0000000..28701c1
--- /dev/null
@@ -0,0 +1 @@
+{{ object.name }}
\ No newline at end of file
index 142b54e553a77a418a9c020a617c5207fb5dcc1d..c9654e02bf3221b91e10a2f94db91cf1d444e92e 100644 (file)
@@ -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)
index 475d3eecbd37af9e0c88654392cd545c523d2946..89db95c6810fb3fccb364ed890fa1c08262b7df2 100644 (file)
@@ -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 = []