diff --git a/anki/collection.py b/anki/collection.py index 421e2e4d3..8f7035c1a 100644 --- a/anki/collection.py +++ b/anki/collection.py @@ -277,11 +277,22 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""", # build map of (nid,ord) so we don't create dupes snids = ids2str(nids) have = {} - for id, nid, ord in self.db.execute( - "select id, nid, ord from cards where nid in "+snids): + dids = {} + for id, nid, ord, did in self.db.execute( + "select id, nid, ord, did from cards where nid in "+snids): + # existing cards if nid not in have: have[nid] = {} have[nid][ord] = id + # and their dids + if nid in dids: + if dids[nid] and dids[nid] != did: + # cards are in two or more different decks; revert to + # model default + dids[nid] = None + else: + # first card or multiple cards in same deck + dids[nid] = did # build cards for each note data = [] ts = maxID(self.db) @@ -292,7 +303,7 @@ crt=?, mod=?, scm=?, dty=?, usn=?, ls=?, conf=?""", "select id, mid, flds from notes where id in "+snids): model = self.models.get(mid) avail = self.models.availOrds(model, flds) - did = model['did'] + did = dids.get(nid) or model['did'] for t in model['tmpls']: doHave = nid in have and t['ord'] in have[nid] # if have ord but empty, add cid to remove list diff --git a/tests/test_cards.py b/tests/test_cards.py index a56157c2d..8621d1ebd 100644 --- a/tests/test_cards.py +++ b/tests/test_cards.py @@ -92,3 +92,36 @@ def test_genrem(): # remHook("remEmptyCards", abort) # f.flush() # assert len(f.cards()) == 1 + +def test_gendeck(): + d = getEmptyDeck() + cloze = d.models.byName("Cloze") + d.models.setCurrent(cloze) + f = d.newNote() + f['Text'] = u'{{c1::one}}' + d.addNote(f) + assert d.cardCount() == 1 + assert f.cards()[0].did == 1 + # set the model to a new default deck + newId = d.decks.id("new") + cloze['did'] = newId + d.models.save(cloze) + # a newly generated card should share the first card's deck + f['Text'] += u'{{c2::two}}' + f.flush() + assert f.cards()[1].did == 1 + # and same with multiple cards + f['Text'] += u'{{c3::three}}' + f.flush() + assert f.cards()[2].did == 1 + # if one of the cards is in a different deck, it should revert to the + # model default + c = f.cards()[1] + c.did = newId + c.flush() + f['Text'] += u'{{c4::four}}' + f.flush() + assert f.cards()[3].did == newId + + +