per-fact empty card deletion, and delete hook

This commit is contained in:
Damien Elmes 2011-12-11 23:48:24 +09:00
parent 8be0e6cccd
commit 0b96e8a1a9
4 changed files with 20 additions and 2 deletions

View file

@ -391,6 +391,10 @@ select id from notes where id in %s and id not in (select nid from cards)""" %
ids2str(nids)) ids2str(nids))
self._remNotes(nids) self._remNotes(nids)
def remEmptyCards(self, ids):
if runFilter("remEmptyCards", len(ids), True):
self.remCards(ids)
# Field checksums and sorting fields # Field checksums and sorting fields
########################################################################## ##########################################################################

View file

@ -348,7 +348,7 @@ select id from notes where mid = ?)""" % " ".join(map),
def _syncTemplates(self, m): def _syncTemplates(self, m):
rem = self.col.genCards(self.nids(m)) rem = self.col.genCards(self.nids(m))
self.col.remCards(rem) self.col.remEmptyCards(rem)
# Model changing # Model changing
########################################################################## ##########################################################################

View file

@ -157,4 +157,5 @@ insert or replace into notes values (?,?,?,?,?,?,?,?,?,?,?,?)""",
def _postFlush(self): def _postFlush(self):
# generate missing cards # generate missing cards
if not self.newlyAdded: if not self.newlyAdded:
self.col.genCards([self.id]) ids = self.col.genCards([self.id])
self.col.remEmptyCards(ids)

View file

@ -5,6 +5,7 @@ from anki.db import DB
from anki.consts import * from anki.consts import *
from anki.utils import hexifyID from anki.utils import hexifyID
from tests.shared import getEmptyDeck from tests.shared import getEmptyDeck
from anki.hooks import addHook, remHook
def test_previewCards(): def test_previewCards():
deck = getEmptyDeck() deck = getEmptyDeck()
@ -75,6 +76,18 @@ def test_genrem():
mm.save(m, templates=True) mm.save(m, templates=True)
assert len(f.cards()) == 1 assert len(f.cards()) == 1
# if we add to the note, a card should be automatically generated # if we add to the note, a card should be automatically generated
f.load()
f['Back'] = "1" f['Back'] = "1"
f.flush() f.flush()
assert len(f.cards()) == 2 assert len(f.cards()) == 2
# deleteion calls a hook to let the user abort the delete. let's abort it:
def abort(val, *args):
return False
addHook("remEmptyCards", abort)
f['Back'] = ""
f.flush()
assert len(f.cards()) == 2
# if there's no filter, or it returns true, the cards get deleted
remHook("remEmptyCards", abort)
f.flush()
assert len(f.cards()) == 1