]> git.parisson.com Git - telemeta.git/commitdiff
Add aliases + start custom signal for detect change location
authormathieu <mathieu.boye28@gmail.com>
Thu, 12 May 2016 15:40:17 +0000 (17:40 +0200)
committermathieu <mathieu.boye28@gmail.com>
Thu, 12 May 2016 15:40:17 +0000 (17:40 +0200)
app/settings.py
setup.py
telemeta/models/resource.py
telemeta/search_indexes.py
telemeta/templates/search/indexes/telemeta/locationalias_text.txt [new file with mode: 0644]
telemeta/util/search_signals.py [new file with mode: 0644]

index 2b0c90c88e2fe3fe8cceeb7c786775c9eb7595d3..04ed343937cf661f093fa9855b1217c1c15a4e4c 100644 (file)
@@ -306,7 +306,7 @@ HAYSTACK_CONNECTIONS = {
         'URL': 'http://search:9200/',
         'INDEX_NAME': 'haystack',
         'INLUDE_SPELLING': True,
-        'EXCLUDED_INDEXES': ['telemeta.search_indexes.LocationIndex']
+        'EXCLUDED_INDEXES': ['telemeta.search_indexes.LocationIndex', 'telemeta.search_indexes.LocationAliasIndex']
     },
     'autocomplete': {
         # 'ENGINE': 'haystack.backends.elasticsearch_backend.ElasticsearchSearchEngine',
@@ -321,7 +321,8 @@ HAYSTACK_CONNECTIONS = {
                              ]
     },
 }
-HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
+#HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'
+HAYSTACK_SIGNAL_PROCESSOR = 'telemeta.util.search_signals.RealTimeCustomSignal'
 HAYSTACK_SEARCH_RESULTS_PER_PAGE = 50
 
 BOWER_COMPONENTS_ROOT = '/srv/bower/'
index 43d83d62a1a6b9c9b620e4f308894eb22ccdaeab..4b17e6f0ffceccd2592c497085f8a84210d2b2c8 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -77,6 +77,7 @@ setup(
         'django-environ',
         'redis',
         'Werkzeug',
+        'django-dirtyfields'
     ],
   tests_require=['pytest-django', 'pytest-cov', 'factory-boy'],
   # Provide a test command through django-setuptest
index 3f64b7ea2ed427c9d080f2d2497677f811066d18..c834c8c2079e54f3ecc5bf43f52c0d121c3433cf 100644 (file)
@@ -25,9 +25,9 @@
 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):
+class MediaResource(ModelCore, DirtyFieldsMixin):
     "Base class of all media objects"
 
     def public_access_label(self):
index b8fb98c146f39abb82aae390f24cf91a263276a7..f77128d1ad4a0e1c1f63e0c0ded69597868553be 100644 (file)
@@ -19,6 +19,7 @@
 
 from haystack import indexes
 from telemeta.models import *
+from haystack.query import SearchQuerySet
 
 class KeywordField(indexes.CharField):
     field_type = 'keyword'
@@ -249,3 +250,14 @@ class LocationIndex(indexes.SearchIndex, indexes.Indexable):
     def index_queryset(self, using=None):
         return MediaItem.objects.all().locations()
 
+
+class LocationAliasIndex(indexes.SearchIndex, indexes.Indexable):
+
+    text = indexes.CharField(document=True, use_template=True)
+
+    def get_model(self):
+        return LocationAlias
+
+    def index_queryset(self, using=None):
+        l = MediaItem.objects.values('location')
+        return LocationAlias.objects.filter(location__in=l)
\ No newline at end of file
diff --git a/telemeta/templates/search/indexes/telemeta/locationalias_text.txt b/telemeta/templates/search/indexes/telemeta/locationalias_text.txt
new file mode 100644 (file)
index 0000000..fb2c54b
--- /dev/null
@@ -0,0 +1 @@
+{{ object.alias }}
\ No newline at end of file
diff --git a/telemeta/util/search_signals.py b/telemeta/util/search_signals.py
new file mode 100644 (file)
index 0000000..7faa42c
--- /dev/null
@@ -0,0 +1,31 @@
+from django.db import models
+from haystack import signals
+from telemeta.models import *
+
+class RealTimeCustomSignal(signals.RealtimeSignalProcessor):
+
+    def __init__(self, *args, **kwargs):
+        super(RealTimeCustomSignal, self).__init__(*args, **kwargs)
+        self.update_list = []
+
+    def handle_pre_save(self, sender, instance, **kwargs):
+        import sys
+        if(sender == MediaItem):
+             print(sender, instance.is_dirty(check_relationship=True), instance.get_dirty_fields(check_relationship=True))
+        sys.stdout.flush()
+        if sender == MediaItem and instance.is_dirty(check_relationship=True) and instance.get_dirty_fields(check_relationship=True).has_key('location'):
+            self.update_list.append(Location)
+
+    def handle_save(self, sender, instance, **kwargs):
+        import sys
+        if sender == MediaItem and sender in self.update_list:
+            print("OKOK")
+            sys.stdout.flush()
+        del self.update_list[:]
+        print("okk")
+        sys.stdout.flush()
+        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