From 8abed26dc903e113cbad58edfad659718548c842 Mon Sep 17 00:00:00 2001 From: j <0x006A@0x2620.org> Date: Thu, 23 Apr 2015 17:20:00 +0530 Subject: [PATCH] make sure alternative names are unique too, also fixes #2754 --- pandora/entity/models.py | 21 +++++++++++++++++---- static/js/entitiesDialog.js | 2 ++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/pandora/entity/models.py b/pandora/entity/models.py index be606da7a..52a4da9ea 100644 --- a/pandora/entity/models.py +++ b/pandora/entity/models.py @@ -114,15 +114,28 @@ class Entity(models.Model): if not data['name']: data['name'] = "Unnamed" name = data['name'] - num = 1 + n = 1 while Entity.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=self.id).count() > 0: - num += 1 - name = data['name'] + ' [%d]' % num + n += 1 + name = data['name'] + ' [%d]' % n self.name = name elif key == 'type': self.type = data[key] elif key == 'alternativeNames': - self.alternativeNames = tuple([ox.escape_html(v) for v in data[key]]) + used_names = [self.name.lower()] + names = [] + for v in data[key]: + name = ox.decode_html(v) + name = re.sub(' \[\d+\]$', '', name).strip() + name_ = name + n = 1 + while name in used_names or \ + Entity.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=self.id).count() > 0: + n += 1 + name = name_ + ' [%d]' % n + names.append(name) + used_names.append(name.lower()) + self.alternativeNames = tuple(ox.escape_html(n) for n in names) else: #FIXME: more data validation if isinstance(data[key], basestring): diff --git a/static/js/entitiesDialog.js b/static/js/entitiesDialog.js index 54506abd1..a5e618952 100644 --- a/static/js/entitiesDialog.js +++ b/static/js/entitiesDialog.js @@ -416,6 +416,8 @@ pandora.ui.entitiesDialog = function(options) { if (key.id == 'name') { $input.value(result.data.name); $list.reloadList(true); + } else if (key.id == 'alternativeNames') { + $input.value(result.data.alternativeNames); } renderEntity(); });