From: mathieu Date: Fri, 13 May 2016 09:23:55 +0000 (+0200) Subject: End of update locations autocomplete (location + current_location + ancestors + alias) X-Git-Tag: 1.6.2^2~15^2~3^2~10 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=ac60079327e94bd73321688793c61191cbb7b959;p=telemeta.git End of update locations autocomplete (location + current_location + ancestors + alias) --- diff --git a/telemeta/util/search_signals.py b/telemeta/util/search_signals.py index 7faa42cf..142b54e5 100644 --- a/telemeta/util/search_signals.py +++ b/telemeta/util/search_signals.py @@ -1,29 +1,42 @@ from django.db import models from haystack import signals from telemeta.models import * +from telemeta.search_indexes import * +from django.db.models import Q class RealTimeCustomSignal(signals.RealtimeSignalProcessor): + handleModels = (MediaItem,) + def __init__(self, *args, **kwargs): super(RealTimeCustomSignal, self).__init__(*args, **kwargs) - self.update_list = [] + self.update_model = None + + 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) + 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().update_object(instance=loc, using='autocomplete') + for loc in localias: + LocationAliasIndex().update_object(instance=loc, using='autocomplete') 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) + if sender in self.handleModels and instance.is_dirty(check_relationship=True): + self.update_model = sender 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() + if sender == self.update_model: + getattr(self, "post_save_%s" % str(sender).split('.')[-1][:-2].lower())(instance) + self.update_model = None super(RealTimeCustomSignal, self).handle_save(sender, instance, **kwargs) def setup(self):