make sure alternative names are unique too, also fixes #2754

This commit is contained in:
j 2015-04-23 17:20:00 +05:30
parent 5455ec8875
commit 8abed26dc9
2 changed files with 19 additions and 4 deletions

View file

@ -114,15 +114,28 @@ class Entity(models.Model):
if not data['name']: if not data['name']:
data['name'] = "Unnamed" data['name'] = "Unnamed"
name = data['name'] name = data['name']
num = 1 n = 1
while Entity.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=self.id).count() > 0: while Entity.objects.filter(name_find__icontains=u'|%s|'%name).exclude(id=self.id).count() > 0:
num += 1 n += 1
name = data['name'] + ' [%d]' % num name = data['name'] + ' [%d]' % n
self.name = name self.name = name
elif key == 'type': elif key == 'type':
self.type = data[key] self.type = data[key]
elif key == 'alternativeNames': 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: else:
#FIXME: more data validation #FIXME: more data validation
if isinstance(data[key], basestring): if isinstance(data[key], basestring):

View file

@ -416,6 +416,8 @@ pandora.ui.entitiesDialog = function(options) {
if (key.id == 'name') { if (key.id == 'name') {
$input.value(result.data.name); $input.value(result.data.name);
$list.reloadList(true); $list.reloadList(true);
} else if (key.id == 'alternativeNames') {
$input.value(result.data.alternativeNames);
} }
renderEntity(); renderEntity();
}); });