From bbe2973952d3beb72e13162aaa075f998df61495 Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Sat, 22 Oct 2011 03:58:54 +0900 Subject: [PATCH] add newly created clozes too --- anki/deck.py | 1 + anki/facts.py | 18 ++++++++++-------- tests/test_models.py | 9 +++++++-- 3 files changed, 18 insertions(+), 10 deletions(-) diff --git a/anki/deck.py b/anki/deck.py index f4590c610..93d44dd82 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -306,6 +306,7 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""", if t['ord'] in avail: data.append((ts, fid, t['gid'] or gid, t['ord'], ts, fid)) + ts += 1 # bulk update self.db.executemany(""" insert into cards values (?,?,?,?,?,-1,0,0,?,0,0,0,0,0,0,0,"")""", diff --git a/anki/facts.py b/anki/facts.py index 4232cac73..15885cd06 100644 --- a/anki/facts.py +++ b/anki/facts.py @@ -46,7 +46,7 @@ from facts where id = ?""", self.id) def flush(self, mod=None): if self.model()['cloze']: - self._clozeFlush() + self._clozePreFlush() self.mod = mod if mod else intTime() self.usn = self.deck.usn() sfld = stripHTML(self.fields[self.deck.models.sortIdx(self._model)]) @@ -59,6 +59,8 @@ insert or replace into facts values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", self.id = res.lastrowid self.updateFieldChecksums() self.deck.tags.register(self.tags) + if self.model()['cloze']: + self._clozePostFlush() def joinedFields(self): return joinFields(self.fields) @@ -183,7 +185,9 @@ insert or replace into facts values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", # Flushing cloze facts ################################################## - def _clozeFlush(self): + def _clozePreFlush(self): + self.newlyAdded = not self.deck.db.scalar( + "select 1 from cards where fid = ?", self.id) tmpls = self.deck.findTemplates(self) ok = [] for t in tmpls: @@ -194,10 +198,8 @@ insert or replace into facts values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""", ids2str(ok), self.id): # there are; abort, as the UI should have handled this raise Exception("UI should have deleted cloze") + + def _clozePostFlush(self): # generate missing cards - # for t in tmpls: - # if not self.deck.db.scalar( - # "select 1 from cards where fid = ? and ord = ?", - # self.id, t['ord']): - # add.append(t) - # have = self.deck.db.scalar( + if not self.newlyAdded: + self.deck.genCards([self.id]) diff --git a/tests/test_models.py b/tests/test_models.py index a3412589f..aa7b6a6a1 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -165,10 +165,15 @@ def test_cloze(): f['Text'] = "hello {{c1::foo}}" assert d.addFact(f) == 1 # deleting a cloze should fail; the ui should clean up invalid cards + cnt = d.cardCount() f['Text'] = "hello" assertException(Exception, lambda: f.flush()) - - + f['Text'] = "hello {{c1::foo}}" + f.flush() + # if we add another cloze, a card should be generated + f['Text'] = "{{c2::hello}} {{c1::foo}}" + f.flush() + assert d.cardCount() == cnt + 1 def test_modelChange(): deck = getEmptyDeck()