From: mathieu Date: Thu, 12 May 2016 15:40:17 +0000 (+0200) Subject: Add aliases + start custom signal for detect change location X-Git-Tag: 1.6.2^2~15^2~3^2~11 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=e8d115c32124642cb1a4b2150d41c844c9991bdf;p=telemeta.git Add aliases + start custom signal for detect change location --- diff --git a/app/settings.py b/app/settings.py index 2b0c90c8..04ed3439 100644 --- a/app/settings.py +++ b/app/settings.py @@ -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/' diff --git a/setup.py b/setup.py index 43d83d62..4b17e6f0 100644 --- 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 diff --git a/telemeta/models/resource.py b/telemeta/models/resource.py index 3f64b7ea..c834c8c2 100644 --- a/telemeta/models/resource.py +++ b/telemeta/models/resource.py @@ -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): diff --git a/telemeta/search_indexes.py b/telemeta/search_indexes.py index b8fb98c1..f77128d1 100644 --- a/telemeta/search_indexes.py +++ b/telemeta/search_indexes.py @@ -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 index 00000000..fb2c54be --- /dev/null +++ b/telemeta/templates/search/indexes/telemeta/locationalias_text.txt @@ -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 index 00000000..7faa42cf --- /dev/null +++ b/telemeta/util/search_signals.py @@ -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