diff --git a/anki/deck.py b/anki/deck.py index 579006e19..3e95a6873 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -24,7 +24,7 @@ defaultQconf = { 'newPerDay': 20, 'newToday': [0, 0], # currentDay, count 'newTodayOrder': NEW_TODAY_ORD, - 'newOrder': 1, + 'newOrder': NEW_CARDS_DUE, 'newSpread': NEW_CARDS_DISTRIBUTE, 'revOrder': REV_CARDS_RANDOM, 'collapseTime': 1200, @@ -36,8 +36,7 @@ defaultQconf = { defaultConf = { 'currentModelId': 1, 'currentGroupId': 1, - 'nextFid': 1, - 'nextCid': 1, + 'nextPos': 1, 'nextGid': 2, 'nextGcid': 2, 'mediaURL': "", @@ -200,10 +199,11 @@ qconf=?, conf=?, data=?""", # Utils ########################################################################## - def nextID(self, type): + def nextID(self, type, inc=True): type = "next"+type.capitalize() id = self.conf.get(type, 1) - self.conf[type] = id+1 + if inc: + self.conf[type] = id+1 return id def reset(self): @@ -231,18 +231,16 @@ qconf=?, conf=?, data=?""", def addFact(self, fact): "Add a fact to the deck. Return number of new cards." - # check we have card models available + # check we have card models available, then save cms = self.findTemplates(fact) if not cms: return 0 - # flush the fact - fact.id = self.nextID("fid") fact.flush() # randomize? if self.randomNew(): - due = random.randrange(1, fact.id) + due = self._randPos() else: - due = fact.id + due = self.nextID("pos") # add cards ncards = 0 for template in cms: @@ -250,6 +248,9 @@ qconf=?, conf=?, data=?""", ncards += 1 return ncards + def _randPos(self): + return random.randrange(1, self.nextID("pos", inc=False)) + def delFacts(self, ids): self.delCards(self.db.list("select id from cards where fid in "+ ids2str(ids))) @@ -296,7 +297,7 @@ qconf=?, conf=?, data=?""", # if this fact has existing new cards, use their due time due = self.db.scalar( "select due from cards where fid = ? and queue = 0", fact.id) - due = due or random.randrange(1, self.conf['nextFid']) + due = due or self._randPos() else: due = fact.id for template in self.findTemplates(fact, checkActive=False): @@ -331,7 +332,6 @@ qconf=?, conf=?, data=?""", def _newCard(self, fact, template, due, flush=True): "Create a new card." card = anki.cards.Card(self) - card.id = self.nextID("cid") card.fid = fact.id card.ord = template['ord'] card.gid = self.defaultGroup(template['gid'] or fact.gid) diff --git a/anki/storage.py b/anki/storage.py index 92d470842..abc657f27 100644 --- a/anki/storage.py +++ b/anki/storage.py @@ -572,8 +572,7 @@ update cards set due = cast( if deck.randomNew(): deck.sched.randomizeCards() # update insertion id - deck.conf['nextFid'] = deck.db.scalar("select max(id) from facts")+1 - deck.conf['nextCid'] = deck.db.scalar("select max(id) from cards")+1 + deck.conf['nextPos'] = deck.db.scalar("select max(id) from facts")+1 deck.save() # optimize and finish diff --git a/tests/test_cards.py b/tests/test_cards.py index 92604e41b..89658851a 100644 --- a/tests/test_cards.py +++ b/tests/test_cards.py @@ -18,7 +18,7 @@ def test_genCards(): assert deck.cardCount() == 2 assert cards[0].due == f.id # should work on random mode too - deck.qconf['newCardOrder'] = NEW_CARDS_RANDOM + deck.qconf['newOrder'] = NEW_CARDS_RANDOM f = deck.newFact() f['Front'] = u'1' f['Back'] = u'2' @@ -76,5 +76,4 @@ def test_misc(): c = f.cards()[0] id = d.conf['currentModelId'] assert c.cssClass() == "cm%s-0" % hexifyID(id) - assert c.fact().id == 1 assert c.template()['ord'] == 0 diff --git a/tests/test_find.py b/tests/test_find.py index 7e0f8fb87..02fd42915 100644 --- a/tests/test_find.py +++ b/tests/test_find.py @@ -8,6 +8,7 @@ def test_findCards(): f['Front'] = u'dog' f['Back'] = u'cat' f.tags.append(u"monkey") + f1id = f.id deck.addFact(f) firstCardId = f.cards()[0].id f = deck.newFact() @@ -15,6 +16,7 @@ def test_findCards(): f['Back'] = u'sheep' f.tags.append(u"sheep goat horse") deck.addFact(f) + f2id = f.id f = deck.newFact() f['Front'] = u'cat' f['Back'] = u'sheep' @@ -34,11 +36,11 @@ def test_findCards(): assert len(deck.findCards("tag:monkey")) == 1 assert len(deck.findCards("tag:sheep -tag:monkey")) == 1 assert len(deck.findCards("-tag:sheep")) == 4 - deck.addTags(deck.db.list("select id from cards"), "foo bar") + deck.addTags(deck.db.list("select id from facts"), "foo bar") assert (len(deck.findCards("tag:foo")) == len(deck.findCards("tag:bar")) == 5) - deck.delTags(deck.db.list("select id from cards"), "foo") + deck.delTags(deck.db.list("select id from facts"), "foo") assert len(deck.findCards("tag:foo")) == 0 assert len(deck.findCards("tag:bar")) == 5 # text searches @@ -67,7 +69,7 @@ def test_findCards(): # fids assert deck.findCards("fid:54321") == [] assert len(deck.findCards("fid:%d"%f.id)) == 2 - assert len(deck.findCards("fid:3,2")) == 2 + assert len(deck.findCards("fid:%d,%d" % (f1id, f2id))) == 2 # templates assert len(deck.findCards("card:foo")) == 0 assert len(deck.findCards("card:forward")) == 4 diff --git a/tests/test_sched.py b/tests/test_sched.py index bf592808c..14ff614e3 100644 --- a/tests/test_sched.py +++ b/tests/test_sched.py @@ -103,7 +103,7 @@ def test_learn(): d.sched.answerCard(c, 1) # it should by due in 30 seconds t = round(c.due - time.time()) - assert t >= 30 and t <= 40 + assert t >= 25 and t <= 40 # and have 1 cycle, but still a zero grade assert c.grade == 0 assert c.cycles == 1 @@ -736,7 +736,7 @@ def test_reorder(): f2 = d.newFact() f2['Front'] = u"two" d.addFact(f2) - assert f2.cards()[0].due == f2.id + assert f2.cards()[0].due == 2 found=False # 50/50 chance of being reordered for i in range(20): @@ -746,7 +746,7 @@ def test_reorder(): break assert found d.sched.orderCards() - assert f.cards()[0].due == f.id + assert f.cards()[0].due == 1 # shifting f3 = d.newFact() f3['Front'] = u"three"