add some group utilities

This commit is contained in:
Damien Elmes 2011-03-17 04:12:17 +09:00
parent f9c3b27e5d
commit 17ee7757de
4 changed files with 66 additions and 13 deletions

View file

@ -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
##########################################################################

View file

@ -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 ""

View file

@ -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,

View file

@ -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]