From 901bd37e90785def7b9d2d9d9dc1b56ef1eb4a0f Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Tue, 16 Dec 2014 16:39:45 +0000 Subject: [PATCH] resolve entity to name, get_by_name for add/edit annotation api calls --- pandora/annotation/models.py | 1 + pandora/annotation/tasks.py | 7 ++++++- pandora/annotation/views.py | 19 +++++++++++++++---- 3 files changed, 22 insertions(+), 5 deletions(-) diff --git a/pandora/annotation/models.py b/pandora/annotation/models.py index 24446ca9..a0be08ea 100644 --- a/pandora/annotation/models.py +++ b/pandora/annotation/models.py @@ -229,6 +229,7 @@ class Annotation(models.Model): try: entity = Entity.get(self.value) j['entity'] = entity.json(user=user) + j['value'] = entity.name except: j['entity'] = {} elif l['type'] == 'event': diff --git a/pandora/annotation/tasks.py b/pandora/annotation/tasks.py index 7c8cc5aa..77c8afa6 100644 --- a/pandora/annotation/tasks.py +++ b/pandora/annotation/tasks.py @@ -67,6 +67,7 @@ def update_matches(id, type): @task(ignore_results=False, queue='default') def add_annotations(data): from item.models import Item + from entity.models import Entity from user.models import User item = Item.objects.get(public_id=data['item']) layer_id = data['layer'] @@ -75,12 +76,16 @@ def add_annotations(data): return False 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() + else: + value = a['value'] annotation = models.Annotation( item=item, layer=layer_id, user=user, start=float(a['in']), end=float(a['out']), - value=a['value']) + value=value) annotation.save() #update facets if needed if layer_id in item.facet_keys: diff --git a/pandora/annotation/views.py b/pandora/annotation/views.py index 964949c4..f02ce807 100644 --- a/pandora/annotation/views.py +++ b/pandora/annotation/views.py @@ -13,6 +13,8 @@ from ox.django.api import actions from item import utils from item.models import Item +from item.utils import get_by_id +from entity.models import Entity import models from tasks import update_item, add_annotations @@ -170,14 +172,18 @@ def addAnnotation(request, data): item = get_object_or_404_json(Item, public_id=data['item']) layer_id = data['layer'] - layer = filter(lambda l: l['id'] == layer_id, settings.CONFIG['layers'])[0] + 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() + else: + value = data['value'] annotation = models.Annotation( item=item, layer=layer_id, user=request.user, start=float(data['in']), end=float(data['out']), - value=data['value']) + value=value) annotation.save() update_item.delay(annotation.id) response = json_response(annotation.json()) @@ -216,7 +222,7 @@ def addAnnotations(request, data): item = get_object_or_404_json(Item, public_id=data['item']) layer_id = data['layer'] - layer = filter(lambda l: l['id'] == layer_id, settings.CONFIG['layers'])[0] + layer = get_by_id(settings.CONFIG['layers'], layer_id) if item.editable(request.user) \ and layer['canAddAnnotations'].get(request.user.get_profile().get_level()): response = json_response() @@ -268,13 +274,18 @@ def editAnnotation(request, data): response = json_response({}) 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) a.log() for key in ('value', 'in', 'out'): + if key == 'value' and layer['type'] == 'entity': + value = Entity.get_by_name(data['value']).get_id() + else: + value = data[key] if key in data: setattr(a, { 'in': 'start', 'out': 'end' - }.get(key,key), data[key]) + }.get(key,key), value) a.save() #update sort/find tables async update_item.delay(a.id)