mirror of
https://github.com/ankitects/anki.git
synced 2025-09-19 14:32:22 -04:00
add a dirty flag
when we make changes that need to be cleaned up on exit, we mark the deck dirty so that if we exit without saving, we can clean up on next open
This commit is contained in:
parent
ed75e4bee2
commit
31a548ee42
5 changed files with 25 additions and 15 deletions
|
@ -92,6 +92,7 @@ and gid in %s order by %s limit %d""" % (self.today+1+self.min,
|
||||||
ivl = self._graduatingIvl(card, conf, early)
|
ivl = self._graduatingIvl(card, conf, early)
|
||||||
card.due = self.today + ivl
|
card.due = self.today + ivl
|
||||||
# temporarily suspend it
|
# temporarily suspend it
|
||||||
|
self.deck.setDirty()
|
||||||
card.queue = -3
|
card.queue = -3
|
||||||
|
|
||||||
def _graduatingIvl(self, card, conf, early):
|
def _graduatingIvl(self, card, conf, early):
|
||||||
|
|
26
anki/deck.py
26
anki/deck.py
|
@ -77,6 +77,8 @@ class _Deck(object):
|
||||||
self._stdSched = Scheduler(self)
|
self._stdSched = Scheduler(self)
|
||||||
self.sched = self._stdSched
|
self.sched = self._stdSched
|
||||||
self.media = MediaRegistry(self)
|
self.media = MediaRegistry(self)
|
||||||
|
# check for improper shutdown
|
||||||
|
self.cleanup()
|
||||||
|
|
||||||
def name(self):
|
def name(self):
|
||||||
n = os.path.splitext(os.path.basename(self.path))[0]
|
n = os.path.splitext(os.path.basename(self.path))[0]
|
||||||
|
@ -89,12 +91,13 @@ class _Deck(object):
|
||||||
(self.crt,
|
(self.crt,
|
||||||
self.mod,
|
self.mod,
|
||||||
self.scm,
|
self.scm,
|
||||||
|
self.dty,
|
||||||
self.syncName,
|
self.syncName,
|
||||||
self.lastSync,
|
self.lastSync,
|
||||||
self.qconf,
|
self.qconf,
|
||||||
self.conf,
|
self.conf,
|
||||||
self.data) = self.db.first("""
|
self.data) = self.db.first("""
|
||||||
select crt, mod, scm, syncName, lastSync,
|
select crt, mod, scm, dty, syncName, lastSync,
|
||||||
qconf, conf, data from deck""")
|
qconf, conf, data from deck""")
|
||||||
self.qconf = simplejson.loads(self.qconf)
|
self.qconf = simplejson.loads(self.qconf)
|
||||||
self.conf = simplejson.loads(self.conf)
|
self.conf = simplejson.loads(self.conf)
|
||||||
|
@ -105,9 +108,10 @@ qconf, conf, data from deck""")
|
||||||
self.mod = intTime()
|
self.mod = intTime()
|
||||||
self.db.execute(
|
self.db.execute(
|
||||||
"""update deck set
|
"""update deck set
|
||||||
crt=?, mod=?, scm=?, syncName=?, lastSync=?,
|
crt=?, mod=?, scm=?, dty=?, syncName=?, lastSync=?,
|
||||||
qconf=?, conf=?, data=?""",
|
qconf=?, conf=?, data=?""",
|
||||||
self.crt, self.mod, self.scm, self.syncName, self.lastSync,
|
self.crt, self.mod, self.scm, self.dty,
|
||||||
|
self.syncName, self.lastSync,
|
||||||
simplejson.dumps(self.qconf),
|
simplejson.dumps(self.qconf),
|
||||||
simplejson.dumps(self.conf), simplejson.dumps(self.data))
|
simplejson.dumps(self.conf), simplejson.dumps(self.data))
|
||||||
|
|
||||||
|
@ -122,7 +126,7 @@ qconf=?, conf=?, data=?""",
|
||||||
|
|
||||||
def close(self, save=True):
|
def close(self, save=True):
|
||||||
"Disconnect from DB."
|
"Disconnect from DB."
|
||||||
self.sched.onClose()
|
self.cleanup()
|
||||||
if self.db:
|
if self.db:
|
||||||
if save:
|
if save:
|
||||||
self.save()
|
self.save()
|
||||||
|
@ -142,15 +146,23 @@ qconf=?, conf=?, data=?""",
|
||||||
self.lock()
|
self.lock()
|
||||||
|
|
||||||
def modSchema(self):
|
def modSchema(self):
|
||||||
if not self.schemaDirty():
|
if not self.schemaChanged():
|
||||||
# next sync will be full
|
# next sync will be full
|
||||||
self.emptyTrash()
|
self.emptyTrash()
|
||||||
self.scm = intTime()
|
self.scm = intTime()
|
||||||
|
|
||||||
def schemaDirty(self):
|
def schemaChanged(self):
|
||||||
"True if schema changed since last sync, or syncing off."
|
"True if schema changed since last sync, or syncing off."
|
||||||
return self.scm > self.lastSync
|
return self.scm > self.lastSync
|
||||||
|
|
||||||
|
def setDirty(self):
|
||||||
|
self.dty = True
|
||||||
|
|
||||||
|
def cleanup(self):
|
||||||
|
if self.dty:
|
||||||
|
self.sched.onClose()
|
||||||
|
self.dty = False
|
||||||
|
|
||||||
# Object creation helpers
|
# Object creation helpers
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
|
@ -317,7 +329,7 @@ select id from facts where id not in (select distinct fid from cards)""")
|
||||||
if not ids:
|
if not ids:
|
||||||
return
|
return
|
||||||
sids = ids2str(ids)
|
sids = ids2str(ids)
|
||||||
if self.schemaDirty():
|
if self.schemaChanged():
|
||||||
# immediate delete?
|
# immediate delete?
|
||||||
self.db.execute("delete from cards where id in %s" % sids)
|
self.db.execute("delete from cards where id in %s" % sids)
|
||||||
self.db.execute("delete from revlog where cid in %s" % sids)
|
self.db.execute("delete from revlog where cid in %s" % sids)
|
||||||
|
|
|
@ -76,11 +76,6 @@ order by due""" % self._groupLimit("rev"),
|
||||||
self.db.execute(
|
self.db.execute(
|
||||||
"update cards set queue = type where queue between -3 and -2")
|
"update cards set queue = type where queue between -3 and -2")
|
||||||
|
|
||||||
def _resetSchedBuried(self):
|
|
||||||
"Put temporarily suspended cards back into play."
|
|
||||||
self.db.execute(
|
|
||||||
"update cards set queue = type where queue = -3")
|
|
||||||
|
|
||||||
# Getting the next card
|
# Getting the next card
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
|
@ -590,6 +585,7 @@ queue = 2 %s and due <= :lim order by %s limit %d""" % (
|
||||||
|
|
||||||
def buryFact(self, fid):
|
def buryFact(self, fid):
|
||||||
"Bury all cards for fact until next session."
|
"Bury all cards for fact until next session."
|
||||||
|
self.deck.setDirty()
|
||||||
self.db.execute("update cards set queue = -2 where fid = ?", fid)
|
self.db.execute("update cards set queue = -2 where fid = ?", fid)
|
||||||
|
|
||||||
# Counts
|
# Counts
|
||||||
|
|
|
@ -62,6 +62,7 @@ create table if not exists deck (
|
||||||
mod integer not null,
|
mod integer not null,
|
||||||
scm integer not null,
|
scm integer not null,
|
||||||
ver integer not null,
|
ver integer not null,
|
||||||
|
dty integer not null,
|
||||||
syncName text not null,
|
syncName text not null,
|
||||||
lastSync integer not null,
|
lastSync integer not null,
|
||||||
qconf text not null,
|
qconf text not null,
|
||||||
|
@ -151,7 +152,7 @@ create table if not exists tags (
|
||||||
);
|
);
|
||||||
|
|
||||||
insert or ignore into deck
|
insert or ignore into deck
|
||||||
values(1,0,0,0,%(v)s,'',0,'', '', '');
|
values(1,0,0,0,%(v)s,0,'',0,'', '', '');
|
||||||
""" % ({'v':CURRENT_VERSION}))
|
""" % ({'v':CURRENT_VERSION}))
|
||||||
import anki.deck
|
import anki.deck
|
||||||
import anki.groups
|
import anki.groups
|
||||||
|
@ -356,7 +357,7 @@ def _migrateDeckTbl(db):
|
||||||
db.execute("delete from deck")
|
db.execute("delete from deck")
|
||||||
db.execute("""
|
db.execute("""
|
||||||
insert or replace into deck select id, cast(created as int), :t,
|
insert or replace into deck select id, cast(created as int), :t,
|
||||||
:t, 99, ifnull(syncName, ""), cast(lastSync as int),
|
:t, 99, 0, ifnull(syncName, ""), cast(lastSync as int),
|
||||||
"", "", "" from decks""", t=intTime())
|
"", "", "" from decks""", t=intTime())
|
||||||
# update selective study
|
# update selective study
|
||||||
qconf = anki.deck.defaultQconf.copy()
|
qconf = anki.deck.defaultQconf.copy()
|
||||||
|
|
|
@ -53,7 +53,7 @@ def test_delete():
|
||||||
deck.addFact(f)
|
deck.addFact(f)
|
||||||
cid = f.cards()[0].id
|
cid = f.cards()[0].id
|
||||||
# when the schema is dirty, deletion should be immediate
|
# when the schema is dirty, deletion should be immediate
|
||||||
assert deck.schemaDirty() == True
|
assert deck.schemaChanged() == True
|
||||||
deck.reset()
|
deck.reset()
|
||||||
deck.sched.answerCard(deck.sched.getCard(), 2)
|
deck.sched.answerCard(deck.sched.getCard(), 2)
|
||||||
assert deck.db.scalar("select count() from revlog") == 1
|
assert deck.db.scalar("select count() from revlog") == 1
|
||||||
|
|
Loading…
Reference in a new issue