tree grouping; add column to groups so they can remember tags

This commit is contained in:
Damien Elmes 2011-03-28 20:02:28 +09:00
parent e547b0586a
commit 2d00163323
4 changed files with 43 additions and 21 deletions

View file

@ -2,7 +2,8 @@
# Copyright: Damien Elmes <anki@ichi2.net> # Copyright: Damien Elmes <anki@ichi2.net>
# License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html # License: GNU GPL, version 3 or later; http://www.gnu.org/copyleft/gpl.html
import time, os, random, re, stat, simplejson, datetime, copy import time, os, random, re, stat, simplejson, datetime, copy, itertools
import operator
from anki.lang import _, ngettext from anki.lang import _, ngettext
from anki.utils import parseTags, tidyHTML, ids2str, hexifyID, \ from anki.utils import parseTags, tidyHTML, ids2str, hexifyID, \
@ -554,15 +555,34 @@ update facts set tags = :t, mod = :n where id = :id""", [fix(row) for row in res
def groups(self): def groups(self):
"A list of all group names." "A list of all group names."
return self.db.list("select name from groups") return self.db.list("select name from groups order by name")
def groupsTree(self):
return self._groupChildren(self.groups())
def _groupChildren(self, grps):
tree = []
for (head, tail) in itertools.groupby([x.split("::", 1) for x in grps],
key=operator.itemgetter(0)):
tail = list(tail)
l = [c[1] for c in tail if len(c) > 1]
children = self._groupChildren(l)
tree.append((head, children))
return tuple(tree)
for g in grps:
names = g.split("::")
if g == last:
top[-1][1].append(g)
pass
def groupId(self, name): def groupId(self, name):
"Return the id for NAME, creating if necessary." "Return the id for NAME, creating if necessary."
id = self.db.scalar("select id from groups where name = ?", name) id = self.db.scalar("select id from groups where name = ?", name)
if not id: if not id:
id = self.db.execute("insert into groups values (?,?,?,?)", id = self.db.execute(
self.nextID("gid"), intTime(), name, "insert into groups values (?,?,?,?, ?)",
1).lastrowid self.nextID("gid"), intTime(), name, 1,
simplejson.dumps(anki.groups.defaultData)).lastrowid
return id return id
def delGroup(self, gid): def delGroup(self, gid):

View file

@ -33,16 +33,7 @@ defaultConf = {
'maxTaken': 60, 'maxTaken': 60,
} }
class GroupConfig(object): defaultData = {
def __init__(self, name): 'activeTags': None,
self.name = name 'inactiveTags': None,
self.id = None }
self.config = defaultConf
def load(self):
self.config = simplejson.loads(self._config)
return self
def save(self):
self._config = simplejson.dumps(self.config)
self.modified = intTime()

View file

@ -123,7 +123,8 @@ create table if not exists groups (
id integer primary key, id integer primary key,
mod integer not null, mod integer not null,
name text not null, name text not null,
gcid integer not null gcid integer not null,
data text not null
); );
create table if not exists gconf ( create table if not exists gconf (
@ -162,8 +163,9 @@ values(1,0,0,0,%(v)s,0,'',0,'', '', '');
intTime(), _("Default Config"), intTime(), _("Default Config"),
simplejson.dumps(anki.groups.defaultConf)) simplejson.dumps(anki.groups.defaultConf))
db.execute( db.execute(
"insert or ignore into groups values (1, ?, ?, 1)", "insert or ignore into groups values (1, ?, ?, 1, ?)",
intTime(), _("Default Group")) intTime(), _("Default Group"), simplejson.dumps(
anki.groups.defaultData))
if setDeckConf: if setDeckConf:
db.execute("update deck set qconf = ?, conf = ?, data = ?", db.execute("update deck set qconf = ?, conf = ?, data = ?",
simplejson.dumps(anki.deck.defaultQconf), simplejson.dumps(anki.deck.defaultQconf),

View file

@ -177,3 +177,12 @@ def test_selective():
assert deck.db.scalar("select count() from cards where gid = 3") == 3 assert deck.db.scalar("select count() from cards where gid = 3") == 3
deck.setGroupForTags(["one"], [], 2) deck.setGroupForTags(["one"], [], 2)
assert deck.db.scalar("select count() from cards where gid = 2") == 2 assert deck.db.scalar("select count() from cards where gid = 2") == 2
def test_groups():
d = getEmptyDeck()
tree = d._groupChildren(
["a", "b", "c::1", "c::2", "d", "d::1", "d::1::2::3"])
assert tree[0] == ("a", tuple())
assert tree[1] == ("b", tuple())
assert tree[2] == ("c", (("1", tuple()), ("2", tuple())))
assert tree[3] == ('d', (('1', (('2', (('3', ()),)),)),))