From d52dc11b4cfbde12475e20bc4c5bfa81f313e1ac Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 16 Dec 2014 14:59:21 +0000 Subject: [PATCH] get entity by name --- pandora/annotation/models.py | 6 +++--- pandora/entity/models.py | 8 ++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index ec903fa53..64bf7455b 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -226,9 +226,9 @@ class Annotation(models.Model): j['languages'] = self.languages.split(',') l = self.get_layer() if l['type'] == 'entity': - qs = Entity.objects.filter(id=ox.fromAZ(self.value)) - if qs.count() > 0: - j['entity'] = qs[0].json(user=user) + entity = Entity.get_by_name(self.value) + if entity > 0: + j['entity'] = entity.json(user=user) else: j['entity'] = {} elif l['type'] == 'event': diff --git a/pandora/entity/models.py b/pandora/entity/models.py index 74ae5ac08..f49768e17 100644 --- a/pandora/entity/models.py +++ b/pandora/entity/models.py @@ -54,6 +54,14 @@ class Entity(models.Model): def get(cls, id): return cls.objects.get(pk=ox.fromAZ(id)) + @classmethod + def get_by_name(cls, name, default=None): + qs = cls.objects.filter(name_find__icontains=u'|%s|'%name) + if qs.count(): + return qs[0] + else: + return default + @classmethod def get_or_create(model, name): qs = model.objects.filter(name_find__icontains=u'|%s|'%name)