resolve entity to name, get_by_name for add/edit annotation api calls

This commit is contained in:
j 2014-12-16 16:39:45 +00:00
parent d061bbe33f
commit 901bd37e90
3 changed files with 22 additions and 5 deletions

View file

@ -229,6 +229,7 @@ class Annotation(models.Model):
try: try:
entity = Entity.get(self.value) entity = Entity.get(self.value)
j['entity'] = entity.json(user=user) j['entity'] = entity.json(user=user)
j['value'] = entity.name
except: except:
j['entity'] = {} j['entity'] = {}
elif l['type'] == 'event': elif l['type'] == 'event':

View file

@ -67,6 +67,7 @@ def update_matches(id, type):
@task(ignore_results=False, queue='default') @task(ignore_results=False, queue='default')
def add_annotations(data): def add_annotations(data):
from item.models import Item from item.models import Item
from entity.models import Entity
from user.models import User from user.models import User
item = Item.objects.get(public_id=data['item']) item = Item.objects.get(public_id=data['item'])
layer_id = data['layer'] layer_id = data['layer']
@ -75,12 +76,16 @@ def add_annotations(data):
return False return False
user = User.objects.get(username=data['user']) user = User.objects.get(username=data['user'])
for a in data['annotations']: 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( annotation = models.Annotation(
item=item, item=item,
layer=layer_id, layer=layer_id,
user=user, user=user,
start=float(a['in']), end=float(a['out']), start=float(a['in']), end=float(a['out']),
value=a['value']) value=value)
annotation.save() annotation.save()
#update facets if needed #update facets if needed
if layer_id in item.facet_keys: if layer_id in item.facet_keys:

View file

@ -13,6 +13,8 @@ from ox.django.api import actions
from item import utils from item import utils
from item.models import Item from item.models import Item
from item.utils import get_by_id
from entity.models import Entity
import models import models
from tasks import update_item, add_annotations 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']) item = get_object_or_404_json(Item, public_id=data['item'])
layer_id = data['layer'] 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['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( annotation = models.Annotation(
item=item, item=item,
layer=layer_id, layer=layer_id,
user=request.user, user=request.user,
start=float(data['in']), end=float(data['out']), start=float(data['in']), end=float(data['out']),
value=data['value']) value=value)
annotation.save() annotation.save()
update_item.delay(annotation.id) update_item.delay(annotation.id)
response = json_response(annotation.json()) response = json_response(annotation.json())
@ -216,7 +222,7 @@ def addAnnotations(request, data):
item = get_object_or_404_json(Item, public_id=data['item']) item = get_object_or_404_json(Item, public_id=data['item'])
layer_id = data['layer'] 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) \ if item.editable(request.user) \
and layer['canAddAnnotations'].get(request.user.get_profile().get_level()): and layer['canAddAnnotations'].get(request.user.get_profile().get_level()):
response = json_response() response = json_response()
@ -268,13 +274,18 @@ def editAnnotation(request, data):
response = json_response({}) response = json_response({})
a = get_object_or_404_json(models.Annotation, public_id=data['id']) a = get_object_or_404_json(models.Annotation, public_id=data['id'])
if a.editable(request.user): if a.editable(request.user):
layer = get_by_id(settings.CONFIG['layers'], a.layer)
a.log() a.log()
for key in ('value', 'in', 'out'): 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: if key in data:
setattr(a, { setattr(a, {
'in': 'start', 'in': 'start',
'out': 'end' 'out': 'end'
}.get(key,key), data[key]) }.get(key,key), value)
a.save() a.save()
#update sort/find tables async #update sort/find tables async
update_item.delay(a.id) update_item.delay(a.id)