give facts a group, so that cards can have their group reset if necessary

This commit is contained in:
Damien Elmes 2011-03-17 03:20:38 +09:00
parent a702e03df0
commit f9c3b27e5d
4 changed files with 17 additions and 14 deletions

View file

@ -193,7 +193,7 @@ qconf=?, conf=?, data=?""",
"Return a new fact with the current model."
return anki.facts.Fact(self, self.currentModel())
def addFact(self, fact, gid=1):
def addFact(self, fact):
"Add a fact to the deck. Return number of new cards."
# check we have card models available
cms = self.findTemplates(fact)
@ -212,7 +212,7 @@ qconf=?, conf=?, data=?""",
# add cards
ncards = 0
for template in cms:
self._newCard(fact, template, due, gid)
self._newCard(fact, template, due)
ncards += 1
return ncards
@ -254,7 +254,7 @@ select id from facts where id not in (select distinct fid from cards)""")
ok.append(template)
return ok
def genCards(self, fact, templates, gid):
def genCards(self, fact, templates):
"Generate cards for templates if cards not empty. Return cards."
cards = []
# if random mode, determine insertion point
@ -273,7 +273,7 @@ select id from facts where id not in (select distinct fid from cards)""")
"select 1 from cards where fid = ? and ord = ?",
fact.id, template['ord']):
# create
cards.append(self._newCard(fact, template, due, gid))
cards.append(self._newCard(fact, template, due))
return cards
# type 0 - when previewing in add dialog, only non-empty & active
@ -291,16 +291,16 @@ select id from facts where id not in (select distinct fid from cards)""")
return []
cards = []
for template in cms:
cards.append(self._newCard(fact, template, 1, 1, flush=False))
cards.append(self._newCard(fact, template, 1, flush=False))
return cards
def _newCard(self, fact, template, due, gid, flush=True):
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 = template['gid'] or gid
card.gid = template['gid'] or fact.gid
card.due = due
if flush:
card.flush()

View file

@ -18,6 +18,7 @@ class Fact(object):
else:
self.id = None
self._model = model
self.gid = 1
self.mid = model.id
self.crt = intTime()
self.mod = self.crt
@ -28,12 +29,13 @@ class Fact(object):
def load(self):
(self.mid,
self.gid,
self.crt,
self.mod,
self.tags,
self._fields,
self.data) = self.deck.db.first("""
select mid, crt, mod, tags, flds, data from facts where id = ?""", self.id)
select mid, gid, crt, mod, tags, flds, data from facts where id = ?""", self.id)
self._fields = splitFields(self._fields)
self._model = self.deck.getModel(self.mid)
@ -42,8 +44,8 @@ select mid, crt, mod, tags, flds, data from facts where id = ?""", self.id)
# facts table
sfld = self._fields[self._model.sortIdx()]
res = self.deck.db.execute("""
insert or replace into facts values (?, ?, ?, ?, ?, ?, ?, ?)""",
self.id, self.mid, self.crt,
insert or replace into facts values (?, ?, ?, ?, ?, ?, ?, ?, ?)""",
self.id, self.mid, self.gid, self.crt,
self.mod, self.tags, self.joinedFields(),
sfld, self.data)
self.id = res.lastrowid

View file

@ -90,6 +90,7 @@ create table if not exists cards (
create table if not exists facts (
id integer primary key,
mid integer not null,
gid integer not null,
crt integer not null,
mod integer not null,
tags text not null,
@ -270,7 +271,7 @@ end)
""")
# pull facts into memory, so we can merge them with fields efficiently
facts = db.all("""
select id, modelId, cast(created as int), cast(modified as int), tags
select id, modelId, 1, cast(created as int), cast(modified as int), tags
from facts order by created""")
# build field hash
fields = {}
@ -296,7 +297,7 @@ from facts order by created""")
# and put the facts into the new table
db.execute("drop table facts")
_addSchema(db, False)
db.executemany("insert into facts values (?,?,?,?,?,?,'','')", data)
db.executemany("insert into facts values (?,?,?,?,?,?,?,'','')", data)
db.execute("drop table fields")
# media

View file

@ -9,7 +9,7 @@ def test_genCards():
f['Front'] = u'1'
f['Back'] = u'2'
deck.addFact(f)
cards = deck.genCards(f, f.model().templates, 1)
cards = deck.genCards(f, f.model().templates)
assert len(cards) == 1
assert cards[0].ord == 1
assert deck.cardCount() == 2
@ -20,7 +20,7 @@ def test_genCards():
f['Front'] = u'1'
f['Back'] = u'2'
deck.addFact(f)
cards = deck.genCards(f, f.model().templates, 1)
cards = deck.genCards(f, f.model().templates)
assert deck.cardCount() == 4
c = deck.db.list("select due from cards where fid = ?", f.id)
assert c[0] == c[1]