more fixes for schema being accidentally modified on model add

This commit is contained in:
Damien Elmes 2011-11-27 13:50:49 +09:00
parent 478e4c9212
commit ddd890ee75
3 changed files with 13 additions and 15 deletions

View file

@ -45,10 +45,6 @@ elif sys.version_info[1] < 5:
if sys.getfilesystemencoding().lower() in ("ascii", "ansi_x3.4-1968"): if sys.getfilesystemencoding().lower() in ("ascii", "ansi_x3.4-1968"):
raise Exception("Anki requires a UTF-8 locale.") raise Exception("Anki requires a UTF-8 locale.")
import os
if not os.path.exists(os.path.expanduser("~/.no-warranty")):
raise Exception("Don't use this without reading the forum thread")
version = "1.99" version = "1.99"
from anki.storage import Collection from anki.storage import Collection
open = Collection open = Collection

View file

@ -68,7 +68,7 @@ class ModelManager(object):
def save(self, m=None, gencards=False): def save(self, m=None, gencards=False):
"Mark M modified if provided, and schedule registry flush." "Mark M modified if provided, and schedule registry flush."
if m: if m and m['id']:
m['mod'] = intTime() m['mod'] = intTime()
m['usn'] = self.col.usn() m['usn'] = self.col.usn()
self._updateRequired(m) self._updateRequired(m)
@ -124,7 +124,8 @@ class ModelManager(object):
m['flds'] = [] m['flds'] = []
m['tmpls'] = [] m['tmpls'] = []
m['tags'] = [] m['tags'] = []
return self._add(m) m['id'] = None
return m
def rem(self, m): def rem(self, m):
"Delete model, and all its cards/notes." "Delete model, and all its cards/notes."
@ -141,11 +142,11 @@ select id from cards where nid in (select id from notes where mid = ?)""",
if current: if current:
self.setCurrent(self.models.values()[0]) self.setCurrent(self.models.values()[0])
def _add(self, m): def add(self, m):
self._setID(m) self._setID(m)
self.update(m) self.update(m)
self.setCurrent(m) self.setCurrent(m)
return m self.save(m)
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."
@ -186,7 +187,8 @@ select id from cards where nid in (select id from notes where mid = ?)""",
"Copy, save and return." "Copy, save and return."
m2 = copy.deepcopy(m) m2 = copy.deepcopy(m)
m2['name'] = _("%s copy") % m2['name'] m2['name'] = _("%s copy") % m2['name']
return self._add(m2) self.add(m2)
return m2
# Fields # Fields
################################################## ##################################################
@ -271,9 +273,8 @@ select id from cards where nid in (select id from notes where mid = ?)""",
f['ord'] = c f['ord'] = c
def _transformFields(self, m, fn): def _transformFields(self, m, fn):
if not self.col.db.scalar( # model hasn't been added yet?
"select 1 from notes where mid = ? limit 1", m['id']): if not m['id']:
# don't bump schema for a new model
return return
self.col.modSchema() self.col.modSchema()
r = [] r = []
@ -294,6 +295,7 @@ select id from cards where nid in (select id from notes where mid = ?)""",
def addTemplate(self, m, template): def addTemplate(self, m, template):
"Note: should col.genCards() afterwards." "Note: should col.genCards() afterwards."
if m['id']:
self.col.modSchema() self.col.modSchema()
m['tmpls'].append(template) m['tmpls'].append(template)
self._updateTemplOrds(m) self._updateTemplOrds(m)

View file

@ -20,7 +20,7 @@ def addBasicModel(col):
t['qfmt'] = "{{" + _("Front") + "}}" t['qfmt'] = "{{" + _("Front") + "}}"
t['afmt'] = "{{" + _("Back") + "}}" t['afmt'] = "{{" + _("Back") + "}}"
mm.addTemplate(m, t) mm.addTemplate(m, t)
mm.save(m) mm.add(m)
return m return m
models.append((_("Basic"), addBasicModel)) models.append((_("Basic"), addBasicModel))
@ -43,7 +43,7 @@ def addClozeModel(col):
t['afmt'] = ("{{cloze:%d:" + _("Text") + "}}") % n t['afmt'] = ("{{cloze:%d:" + _("Text") + "}}") % n
t['afmt'] += "<br>{{" + _("Notes") + "}}" t['afmt'] += "<br>{{" + _("Notes") + "}}"
mm.addTemplate(m, t) mm.addTemplate(m, t)
mm.save(m) mm.add(m)
return m return m
models.append((_("Cloze"), addClozeModel)) models.append((_("Cloze"), addClozeModel))