give note types unique name when adding/importing (#325)

This commit is contained in:
Damien Elmes 2013-10-04 09:00:08 +09:00
parent 174914b7dc
commit 9524dbed96
2 changed files with 9 additions and 2 deletions

View file

@ -8,6 +8,7 @@ from anki.utils import intTime, joinFields, splitFields, ids2str,\
from anki.lang import _ from anki.lang import _
from anki.consts import * from anki.consts import *
from anki.hooks import runHook from anki.hooks import runHook
import time
# Models # Models
########################################################################## ##########################################################################
@ -123,8 +124,8 @@ class ModelManager(object):
"Get all models." "Get all models."
return self.models.values() return self.models.values()
def allNames(self): def allNames(self, curm=None):
return [m['name'] for m in self.all()] return [m['name'] for m in self.all() if m!=curm]
def byName(self, name): def byName(self, name):
"Get model with NAME." "Get model with NAME."
@ -165,8 +166,13 @@ select id from cards where nid in (select id from notes where mid = ?)""",
self.setCurrent(m) self.setCurrent(m)
self.save(m) self.save(m)
def ensureNameUnique(self, m):
if m['name'] in self.allNames(m):
m['name'] += "-" + checksum(str(time.time()))[:5]
def update(self, m): def update(self, m):
"Add or update an existing model. Used for syncing and merging." "Add or update an existing model. Used for syncing and merging."
self.ensureNameUnique(m)
self.models[str(m['id'])] = m self.models[str(m['id'])] = m
# mark registry changed, but don't bump mod time # mark registry changed, but don't bump mod time
self.save() self.save()

View file

@ -86,6 +86,7 @@ class Models(QDialog):
txt = getText(_("Name:"), default=m['name'])[0] txt = getText(_("Name:"), default=m['name'])[0]
if txt: if txt:
m['name'] = txt m['name'] = txt
self.mm.ensureNameUnique(m)
self.mm.save(m) self.mm.save(m)
self.updateModelsList() self.updateModelsList()