sync facets saving annotations that are used as filters. i.e. keywords fixes #378

This commit is contained in:
j 2012-02-13 20:08:43 +05:30
parent e03a313e77
commit ac5c2ab9c8
2 changed files with 17 additions and 11 deletions

View file

@ -160,6 +160,10 @@ class Annotation(models.Model):
if layer.get('type') == 'event' or layer.get('hasEvents'): if layer.get('type') == 'event' or layer.get('hasEvents'):
update_matching_events(self.id) update_matching_events(self.id)
#update facets if needed
if filter(lambda f: f['id'] == self.layer, settings.CONFIG['filters']):
self.update_layer_facet(self.layer)
def cleanup_undefined_relations(self): def cleanup_undefined_relations(self):
layer = self.get_layer() layer = self.get_layer()
if layer.get('type') == 'place': if layer.get('type') == 'place':

View file

@ -779,21 +779,23 @@ class Item(models.Model):
#update cached values in clips #update cached values in clips
self.clips.all().update(director=s.director, title=s.title) self.clips.all().update(director=s.director, title=s.title)
def update_layer_facet(self, key):
current_values = [a['value']
for a in self.annotations.filter(layer=key).distinct().values('value')]
saved_values = [i.value for i in Facet.objects.filter(item=self, key=key)]
removed_values = filter(lambda i: i not in current_values, saved_values)
if removed_values:
Facet.objects.filter(item=self, key=key, value__in=removed_values).delete()
for value in current_values:
if value not in saved_values:
sortvalue = value
Facet.objects.get_or_create(item=self, key=key, value=value, sortvalue=sortvalue)
def update_layer_facets(self): def update_layer_facets(self):
filters = [f['id'] for f in settings.CONFIG['filters']] filters = [f['id'] for f in settings.CONFIG['filters']]
for layer in settings.CONFIG['layers']: for layer in settings.CONFIG['layers']:
if layer['id'] in filters: if layer['id'] in filters:
key = layer['id'] self.update_layer_facet(layer['id'])
current_values = [a['value']
for a in self.annotations.filter(layer=key).distinct().values('value')]
saved_values = [i.value for i in Facet.objects.filter(item=self, key=key)]
removed_values = filter(lambda i: i not in current_values, saved_values)
if removed_values:
Facet.objects.filter(item=self, key=key, value__in=removed_values).delete()
for value in current_values:
if value not in saved_values:
sortvalue = value
Facet.objects.get_or_create(item=self, key=key, value=value, sortvalue=sortvalue)
def update_facets(self): def update_facets(self):
for key in self.facet_keys + ['title']: for key in self.facet_keys + ['title']: