handle unknown entities.

This commit is contained in:
j 2014-12-17 19:20:00 +00:00
parent a9297b778d
commit beca371b43
3 changed files with 20 additions and 10 deletions

View file

@ -77,7 +77,10 @@ def add_annotations(data):
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': 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: else:
value = a['value'] value = a['value']
annotation = models.Annotation( annotation = models.Annotation(

View file

@ -176,7 +176,12 @@ def addAnnotation(request, data):
layer = get_by_id(settings.CONFIG['layers'], layer_id) 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': 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: else:
value = data['value'] value = data['value']
annotation = models.Annotation( annotation = models.Annotation(
@ -278,10 +283,15 @@ def editAnnotation(request, data):
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) layer = get_by_id(settings.CONFIG['layers'], a.layer)
add_changelog(request, data)
for key in ('value', 'in', 'out'): for key in ('value', 'in', 'out'):
if key == 'value' and layer['type'] == 'entity': 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: else:
value = data[key] value = data[key]
if key in data: if key in data:
@ -289,6 +299,7 @@ def editAnnotation(request, data):
'in': 'start', 'in': 'start',
'out': 'end' 'out': 'end'
}.get(key,key), value) }.get(key,key), value)
add_changelog(request, data)
a.save() a.save()
#update sort/find tables async #update sort/find tables async
update_item.delay(a.id) update_item.delay(a.id)

View file

@ -61,12 +61,8 @@ class Entity(models.Model):
return cls.objects.get(pk=ox.fromAZ(id)) return cls.objects.get(pk=ox.fromAZ(id))
@classmethod @classmethod
def get_by_name(cls, name, default=None): def get_by_name(cls, name):
qs = cls.objects.filter(name_find__icontains=u'|%s|'%name) return cls.objects.get(name_find__icontains=u'|%s|'%name)
if qs.count():
return qs[0]
else:
return default
@classmethod @classmethod
def get_or_create(model, name): def get_or_create(model, name):