Merge remote-tracking branch 'wjt/get-layers-only-entity-name'

(fixes #2913)
This commit is contained in:
j 2016-04-29 00:11:00 +02:00
commit 34747c0fd7
2 changed files with 17 additions and 19 deletions

View file

@ -138,8 +138,7 @@ class Annotation(models.Model):
findvalue = self.value findvalue = self.value
try: try:
l = self.get_layer() if layer['type'] == 'entity':
if l['type'] == 'entity':
findvalue = self.get_entity().name findvalue = self.get_entity().name
except: except:
pass pass
@ -210,19 +209,15 @@ class Annotation(models.Model):
def _get_entity_json(self, user=None, entity_cache=None): def _get_entity_json(self, user=None, entity_cache=None):
"""When serializing many annotations pointing to the same entity, it is expensive to """When serializing many annotations pointing to the same entity, it is expensive to
repeatedly look up and serialize the same entity. repeatedly look up and serialize the same entity.
TODO: if Entity were a (nullable) foreign key of Annotation, we could just:
prefetch_related('entity', 'entity__user', 'entity__documents')
before serializing the annotations, which would make self.entity.json(user=user) cheap and
all this unnecessary.
""" """
from entity.models import Entity
if entity_cache is not None and self.value in entity_cache: if entity_cache is not None and self.value in entity_cache:
return entity_cache[self.value] return entity_cache[self.value]
entity = self.get_entity() id = ox.fromAZ(self.value)
entity_json = entity.json(user=user) entity = Entity.objects.filter(id=id).only('name').get()
entity_json = entity.json(keys=['id', 'name'])
value = entity.annotation_value() value = entity.annotation_value()
if entity_cache is not None: if entity_cache is not None:
@ -295,10 +290,13 @@ class Annotation(models.Model):
value = getattr(self.item.sort, key) value = getattr(self.item.sort, key)
if value != None: if value != None:
j[key] = value j[key] = value
subtitles = get_by_key(settings.CONFIG['layers'], 'isSubtitles', True)
if subtitles: # Items without any real subtitles are given a dummy 5-second subtitle
if 'id' in j and self.layer == subtitles['id'] and not self.value: # every minute to ensure that they have at least *some* clips. Treat
del j['id'] # them specially. See Item.add_empty_clips
if l.get('isSubtitles') and 'id' in j and not self.value:
del j['id']
return j return j
def __unicode__(self): def __unicode__(self):

View file

@ -168,8 +168,8 @@ class Entity(models.Model):
elif key == 'sortName': elif key == 'sortName':
response[key] = self.name_sort response[key] = self.name_sort
elif key == 'documents': elif key == 'documents':
response[key] = [ox.toAZ(d['id']) response[key] = [ox.toAZ(id_)
for d in self.documents.all().values('id').order_by('documentproperties__index')] for id_, in self.documentproperties.order_by('index').values_list('document_id')]
elif key in self.data: elif key in self.data:
response[key] = self.data[key] response[key] = self.data[key]
return response return response