From 17ee7757def3d8dab1953333dc6d215dcc84308e Mon Sep 17 00:00:00 2001 From: Damien Elmes Date: Thu, 17 Mar 2011 04:12:17 +0900 Subject: [PATCH] add some group utilities --- anki/deck.py | 31 +++++++++++++++++++++++++++++++ anki/sched.py | 9 +++------ anki/storage.py | 14 +++++++------- tests/test_deck.py | 25 +++++++++++++++++++++++++ 4 files changed, 66 insertions(+), 13 deletions(-) diff --git a/anki/deck.py b/anki/deck.py index 7784bbc72..d5bd8ecd6 100644 --- a/anki/deck.py +++ b/anki/deck.py @@ -593,6 +593,37 @@ update facts set tags = :t, mod = :n where id = :id""", [fix(row) for row in res def delTags(self, ids, tags): self.addTags(ids, tags, False) + # Groups + ########################################################################## + + def groups(self): + "A list of all group names." + return self.db.list("select name from groups") + + def groupId(self, name): + "Return the id for NAME, creating if necessary." + id = self.db.scalar("select id from groups where name = ?", name) + if not id: + id = self.db.execute("insert into groups values (?,?,?,?)", + self.nextID("gid"), intTime(), name, + 1).lastrowid + return id + + def delGroup(self, gid): + self.db.scalar("delete from groups where id = ?", gid) + + def groupConf(self, gid): + return simplejson.loads( + self.db.scalar(""" +select conf from gconf where id = (select gcid from groups where id = ?)""", + gid)) + + def activeGroups(self, type): + return self.qconf[type+"Groups"] + + def setActiveGroups(self, type, list): + self.qconf[type+"Groups"] = list + # Finding cards ########################################################################## diff --git a/anki/sched.py b/anki/sched.py index 2a071db1e..fe83b08c6 100644 --- a/anki/sched.py +++ b/anki/sched.py @@ -55,8 +55,7 @@ class Scheduler(object): card.flushSched() def counts(self): - # FIXME: should learn count include new cards due today, or be separate? - return (self.learnCount, self.revCount) + return (self.learnCount, self.revCount, self.newCount) def timeToday(self): "Time spent learning today, in seconds." @@ -507,9 +506,7 @@ insert into revlog values ( def confForCard(self, card): id = self.groupConfs[card.gid] if id not in self.confCache: - self.confCache[id] = simplejson.loads( - self.db.scalar("select conf from gconf where id = :id", - id=id)) + self.confCache[id] = self.deck.groupConf(id) return self.confCache[id] def resetSchedBuried(self): @@ -518,7 +515,7 @@ insert into revlog values ( "update cards set queue = type where queue = -3") def groupLimit(self, type): - l = self.deck.qconf[type+"Groups"] + l = self.deck.activeGroups(type) if not l: # everything return "" diff --git a/anki/storage.py b/anki/storage.py index 2da5209a4..37a66956b 100644 --- a/anki/storage.py +++ b/anki/storage.py @@ -115,13 +115,6 @@ create table if not exists models ( css text not null ); -create table if not exists gconf ( - id integer primary key, - mod integer not null, - name text not null, - conf text not null -); - create table if not exists groups ( id integer primary key, mod integer not null, @@ -129,6 +122,13 @@ create table if not exists groups ( gcid integer not null ); +create table if not exists gconf ( + id integer primary key, + mod integer not null, + name text not null, + conf text not null +); + create table if not exists media ( file text primary key, mod integer not null, diff --git a/tests/test_deck.py b/tests/test_deck.py index 0cacd6709..81b5c7a66 100644 --- a/tests/test_deck.py +++ b/tests/test_deck.py @@ -123,3 +123,28 @@ def test_upgrade(): deck = Deck(dst) # now's a good time to test the integrity check too deck.fixIntegrity() + +def test_groups(): + deck = getEmptyDeck() + # we start with a standard group + assert len(deck.groups()) == 1 + # it should have an id of 1 + assert deck.groupId(deck.groups()[0]) == 1 + # create a new group + assert deck.groupId("new group") == 2 + assert len(deck.groups()) == 2 + # should get the same id + assert deck.groupId("new group") == 2 + # deleting a group should not recycle ids + deck.delGroup(2) + assert len(deck.groups()) == 1 + assert deck.groupId("another group") == 3 + # the newly created group should have a default schedule + conf = deck.groupConf(3) + assert conf == deck.groupConf(1) + # by default, everything should be shown + assert not deck.activeGroups('rev') + assert not deck.activeGroups('new') + # set new cards to only 'another group' + deck.setActiveGroups('new', [3]) + assert deck.activeGroups('new') == [3]