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

View file

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

View file

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

View file

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