refactor filters updates

This commit is contained in:
j 2015-04-15 08:51:34 +01:00
parent 6a5280edd4
commit 6793c165b3

View file

@ -1001,48 +1001,7 @@ class Item(models.Model):
s.save()
def update_layer_facet(self, key):
from entity.models import Entity
current_values = [a['value']
for a in self.annotations.filter(layer=key).distinct().values('value')]
layer = utils.get_by_id(settings.CONFIG['layers'], key)
if layer.get('type') == 'entity':
current_values = [a['name']
for a in Entity.objects.filter(id__in=[ox.fromAZ(i) for i in current_values]).values('name')]
current_values = [ox.decode_html(ox.strip_tags(v.replace('<br>', ' '))) for v in current_values]
current_sortvalues = set([value.lower() for value in current_values])
saved_values = [i.value.lower() for i in Facet.objects.filter(item=self, key=key)]
removed_values = filter(lambda i: i not in current_sortvalues, saved_values)
if removed_values:
q = Q()
for v in removed_values:
q |=Q(value__iexact=v)
Facet.objects.filter(item=self, key=key).filter(q).delete()
for value in current_values:
if value.lower() not in saved_values:
sortvalue = utils.sort_string(value).lower()[:900]
Facet.objects.get_or_create(item=self, key=key, value=value, sortvalue=sortvalue)
saved_values.append(value.lower())
def get_layer_facets(self):
return [k['id']
for k in settings.CONFIG['itemKeys']
if k['type'] == 'layer' and (
k.get('filter') or \
utils.get_by_id(settings.CONFIG['layers'], k['id']).get('type') == 'string')
]
def update_layer_facets(self):
for k in self.get_layer_facets():
self.update_layer_facet(k)
def update_facets(self):
layer_facets = self.get_layer_facets()
for key in self.facet_keys + ['title']:
if key in layer_facets:
continue
def update_facet(self, key):
current_values = self.get(key, [])
if key == 'title':
if current_values:
@ -1084,14 +1043,30 @@ class Item(models.Model):
current_values = list(set(current_values))
current_values = [ox.decode_html(ox.strip_tags(v)) for v in current_values]
current_sortvalues = [value.lower() for value in current_values]
self.update_facet_values(key, current_values)
def update_layer_facet(self, key):
from entity.models import Entity
current_values = [a['value']
for a in self.annotations.filter(layer=key).distinct().values('value')]
layer = utils.get_by_id(settings.CONFIG['layers'], key)
if layer.get('type') == 'entity':
current_values = [a['name']
for a in Entity.objects.filter(id__in=[ox.fromAZ(i) for i in current_values]).values('name')]
current_values = [ox.decode_html(ox.strip_tags(v.replace('<br>', ' '))) for v in current_values]
self.update_facet_values(key, current_values)
def update_facet_values(self, key, current_values):
current_sortvalues = set([value.lower() for value in current_values])
saved_values = [i.value.lower() for i in Facet.objects.filter(item=self, key=key)]
removed_values = filter(lambda i: i not in current_sortvalues, saved_values)
if removed_values:
q = Q()
for v in removed_values:
q |=Q(value__iexact=v)
Facet.objects.filter(item=self, key=key).filter(q).delete()
for value in current_values:
if value.lower() not in saved_values:
sortvalue = value
@ -1099,7 +1074,14 @@ class Item(models.Model):
sortvalue = get_name_sort(value)
sortvalue = utils.sort_string(sortvalue).lower()[:900]
Facet.objects.get_or_create(item=self, key=key, value=value, sortvalue=sortvalue)
self.update_layer_facets()
saved_values.append(value.lower())
def update_facets(self):
for key in set(self.facet_keys + ['title']):
if key in self.layer_facet_keys:
self.update_layer_facet(key)
else:
self.update_facet(key)
def path(self, name=''):
h = self.public_id
@ -1636,6 +1618,7 @@ def delete_item(sender, **kwargs):
pre_delete.connect(delete_item, sender=Item)
Item.facet_keys = []
Item.layer_facet_keys = []
Item.poster_keys = []
for key in settings.CONFIG['itemKeys']:
if 'autocomplete' in key and not 'autocompleteSortKey' in key or \
@ -1646,6 +1629,11 @@ for key in settings.CONFIG['itemKeys']:
Item.facet_keys.append(key['id'])
if key['id'] in ('title', 'director', 'year') or key.get('poster'):
Item.poster_keys.append(key['id'])
if key.get('type') == 'layer' and (
key.get('filter') or \
utils.get_by_id(settings.CONFIG['layers'], key['id']).get('type') == 'string'
):
Item.layer_facet_keys.append(key['id'])
Item.person_keys = []
for key in settings.CONFIG['itemKeys']: