From beca371b43a3ddd9c7c760cd8cd98b6c41268549 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Wed, 17 Dec 2014 19:20:00 +0000 Subject: [PATCH] handle unknown entities. --- pandora/annotation/tasks.py | 5 ++++- pandora/annotation/views.py | 17 ++++++++++++++--- pandora/entity/models.py | 8 ++------ 3 files changed, 20 insertions(+), 10 deletions(-) diff --git a/pandora/annotation/tasks.py b/pandora/annotation/tasks.py index 77c8afa6..ac32d496 100644 --- a/pandora/annotation/tasks.py +++ b/pandora/annotation/tasks.py @@ -77,7 +77,10 @@ def add_annotations(data): user = User.objects.get(username=data['user']) for a in data['annotations']: if layer['type'] == 'entity': - value = Entity.get_by_name(a['value']).get_id() + try: + value = Entity.get_by_name(a['value']).get_id() + except Entity.DoesNotExist: + continue else: value = a['value'] annotation = models.Annotation( diff --git a/pandora/annotation/views.py b/pandora/annotation/views.py index 65ab3b7b..f8ab7224 100644 --- a/pandora/annotation/views.py +++ b/pandora/annotation/views.py @@ -176,7 +176,12 @@ def addAnnotation(request, data): layer = get_by_id(settings.CONFIG['layers'], layer_id) if layer['canAddAnnotations'].get(request.user.get_profile().get_level()): if layer['type'] == 'entity': - value = Entity.get_by_name(data['value']).get_id() + try: + value = Entity.get_by_name(data['value']).get_id() + except Entity.DoesNotExist: + response = json_response({}) + response['status']['text'] = 'unkown entity' + return render_to_json_response(response) else: value = data['value'] annotation = models.Annotation( @@ -278,10 +283,15 @@ def editAnnotation(request, data): a = get_object_or_404_json(models.Annotation, public_id=data['id']) if a.editable(request.user): layer = get_by_id(settings.CONFIG['layers'], a.layer) - add_changelog(request, data) for key in ('value', 'in', 'out'): if key == 'value' and layer['type'] == 'entity': - value = Entity.get_by_name(data['value']).get_id() + try: + value = Entity.get_by_name(data['value']).get_id() + except Entity.DoesNotExist: + response['data'] = a.json() + response['data']['editable'] = True + response['status']['text'] = 'unkown entity' + return render_to_json_response(response) else: value = data[key] if key in data: @@ -289,6 +299,7 @@ def editAnnotation(request, data): 'in': 'start', 'out': 'end' }.get(key,key), value) + add_changelog(request, data) a.save() #update sort/find tables async update_item.delay(a.id) diff --git a/pandora/entity/models.py b/pandora/entity/models.py index 041ba46a..24c6038e 100644 --- a/pandora/entity/models.py +++ b/pandora/entity/models.py @@ -61,12 +61,8 @@ class Entity(models.Model): 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 + def get_by_name(cls, name): + return cls.objects.get(name_find__icontains=u'|%s|'%name) @classmethod def get_or_create(model, name):