conditional card generation

This commit is contained in:
Damien Elmes 2008-10-12 03:42:52 +09:00
parent 2a3f653d0e
commit 225e31f470

View file

@ -745,13 +745,17 @@ priority != 0 and due < :now and spaceUntil > :now""",
# validate
fact.assertValid()
fact.assertUnique(self.s)
# and associated cards
# check we have card models available
cms = self.availableCardModels(fact)
print cms
if not cms:
return []
# proceed
n = 0
cards = []
self.s.save(fact)
self.flushMod()
for cardModel in fact.model.cardModels:
if cardModel.active:
for cardModel in cms:
card = anki.cards.Card(fact, cardModel)
self.flushMod()
self.updatePriority(card)
@ -762,6 +766,25 @@ priority != 0 and due < :now and spaceUntil > :now""",
self._countsDirty = True
return cards
def availableCardModels(self, fact):
"List of active card models that aren't empty for FACT."
models = []
for cardModel in fact.model.cardModels:
if cardModel.active:
ok = True
for format in [cardModel.qformat, cardModel.aformat]:
empty = {}
for k in fact.keys():
empty[k] = u""
try:
if format % fact == format % empty:
ok = False
except (KeyError, TypeError, ValueError):
ok = False
if ok:
models.append(cardModel)
return models
def addMissingCards(self, fact):
"Caller must flush first, flushMod after, and rebuild priorities."
for cardModel in fact.model.cardModels: