get entity by name

This commit is contained in:
j 2014-12-16 14:59:21 +00:00
parent 8c9b92479d
commit d52dc11b4c
2 changed files with 11 additions and 3 deletions

View file

@ -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':

View file

@ -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)