diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index 31a65332..ec903fa5 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -13,6 +13,7 @@ from django.db.models.signals import pre_delete import ox from clip.models import Clip +from entity.models import Entity from changelog.models import Changelog from item.utils import sort_string, get_by_key @@ -224,18 +225,24 @@ class Annotation(models.Model): if self.languages: j['languages'] = self.languages.split(',') l = self.get_layer() - if l['type'] == 'place': - qs = self.places.all() + if l['type'] == 'entity': + qs = Entity.objects.filter(id=ox.fromAZ(self.value)) if qs.count() > 0: - j['place'] = qs[0].json(user=user) + j['entity'] = qs[0].json(user=user) else: - j['place'] = {} + j['entity'] = {} elif l['type'] == 'event': qs = self.events.all() if qs.count() > 0: j['event'] = qs[0].json(user=user) else: j['event'] = {} + elif l['type'] == 'place': + qs = self.places.all() + if qs.count() > 0: + j['place'] = qs[0].json(user=user) + else: + j['place'] = {} if layer or (keys and 'layer' in keys): j['layer'] = self.layer diff --git a/pandora/entity/models.py b/pandora/entity/models.py index 04182b4d..74ae5ac0 100644 --- a/pandora/entity/models.py +++ b/pandora/entity/models.py @@ -10,6 +10,7 @@ from django.db import models from django.db.models import Max from django.contrib.auth.models import User from django.db.models.signals import pre_delete +from django.conf import settings import ox from ox.django import fields @@ -44,7 +45,7 @@ class Entity(models.Model): self.name_sort = ox.sort_string(self.name or u'')[:255].lower() self.name_find = '||' + self.name + '||'.join(self.alternativeNames) + '||' super(Entity, self).save(*args, **kwargs) - #self.update_matches() + self.update_matches() def __unicode__(self): return self.get_id() @@ -134,7 +135,11 @@ class Entity(models.Model): url = unquote(urls[0]) if url != urls[0]: urls.append(url) - matches = self.items.count() + entity_layers = [l['id'] for l in settings.CONFIG['layers'] if l['type'] == 'entity'] + if entity_layers: + matches = annotation.models.Annotation.objects.filter(layer__in=entity_layers, value=self.get_id()).count() + else: + matches = 0 for url in urls: matches += annotation.models.Annotation.objects.filter(value__contains=url).count() matches += item.models.Item.objects.filter(data__contains=url).count() diff --git a/pandora/entity/views.py b/pandora/entity/views.py index c5cdd227..42968c01 100644 --- a/pandora/entity/views.py +++ b/pandora/entity/views.py @@ -49,7 +49,7 @@ def addEntity(request, data): existing_names.append(name) if not exists: data['name'] = ox.escape_html(data['name']) - entity = models.Entity(name=data['name']) + entity = models.Entity(name=data['name'], type=data['type']) entity.user = request.user for key in ('type', 'alternativeNames'): if key in data and data[key]: