empty_filtered_deck()

This commit is contained in:
Damien Elmes 2020-09-03 17:43:07 +10:00
parent 56ceb6ba76
commit f87fa762be
5 changed files with 44 additions and 38 deletions

View File

@ -1064,42 +1064,21 @@ select id from cards where did in %s and queue = {QUEUE_TYPE_REV} and due <= ? l
# Filtered deck handling # Filtered deck handling
########################################################################## ##########################################################################
_restoreQueueWhenEmptyingSnippet = f""" def rebuild_filtered_deck(self, deck_id: int) -> int:
queue = (case when queue < 0 then queue return self.col.backend.rebuild_filtered_deck(deck_id)
when type in (1,{CARD_TYPE_RELEARNING}) then
(case when (case when odue then odue else due end) > 1000000000 then 1 else def empty_filtered_deck(self, deck_id: int) -> None:
{QUEUE_TYPE_DAY_LEARN_RELEARN} end) self.col.backend.empty_filtered_deck(deck_id)
else
type
end)
"""
def rebuildDyn(self, did: Optional[int] = None) -> Optional[int]: def rebuildDyn(self, did: Optional[int] = None) -> Optional[int]:
"Rebuild a filtered deck."
did = did or self.col.decks.selected() did = did or self.col.decks.selected()
count = self.col.backend.rebuild_filtered_deck(did) or None count = self.rebuild_filtered_deck(did) or None
if not count: if not count:
return None return None
# and change to our new deck # and change to our new deck
self.col.decks.select(did) self.col.decks.select(did)
return count return count
def emptyDyn(self, did: Optional[int], lim: Optional[str] = None) -> None:
if lim is None:
self.col.backend.empty_filtered_deck(did)
return
self.col.db.execute(
"""
update cards set did = odid, %s,
due = (case when odue>0 then odue else due end), odue = 0, odid = 0, usn = ? where %s"""
% (self._restoreQueueWhenEmptyingSnippet, lim),
self.col.usn(),
)
def remFromDyn(self, cids: List[int]) -> None:
self.emptyDyn(None, "id in %s and odid" % ids2str(cids))
def _removeFromFiltered(self, card: Card) -> None: def _removeFromFiltered(self, card: Card) -> None:
if card.odid: if card.odid:
card.did = card.odid card.did = card.odid
@ -1121,6 +1100,33 @@ due = (case when odue>0 then odue else due end), odue = 0, odid = 0, usn = ? whe
else: else:
card.queue = card.type card.queue = card.type
# legacy
def emptyDyn(self, did: Optional[int], lim: Optional[str] = None) -> None:
if lim is None:
self.empty_filtered_deck(did)
return
queue = f"""
queue = (case when queue < 0 then queue
when type in (1,{CARD_TYPE_RELEARNING}) then
(case when (case when odue then odue else due end) > 1000000000 then 1 else
{QUEUE_TYPE_DAY_LEARN_RELEARN} end)
else
type
end)
"""
self.col.db.execute(
"""
update cards set did = odid, %s,
due = (case when odue>0 then odue else due end), odue = 0, odid = 0, usn = ? where %s"""
% (queue, lim),
self.col.usn(),
)
def remFromDyn(self, cids: List[int]) -> None:
self.emptyDyn(None, "id in %s and odid" % ids2str(cids))
# Leeches # Leeches
########################################################################## ##########################################################################

View File

@ -682,7 +682,7 @@ def test_cram_rem():
assert c.type == CARD_TYPE_LRN and c.queue == QUEUE_TYPE_LRN assert c.type == CARD_TYPE_LRN and c.queue == QUEUE_TYPE_LRN
assert c.due != oldDue assert c.due != oldDue
# if we terminate cramming prematurely it should be set back to new # if we terminate cramming prematurely it should be set back to new
col.sched.emptyDyn(did) col.sched.empty_filtered_deck(did)
c.load() c.load()
assert c.type == CARD_TYPE_NEW and c.queue == QUEUE_TYPE_NEW assert c.type == CARD_TYPE_NEW and c.queue == QUEUE_TYPE_NEW
assert c.due == oldDue assert c.due == oldDue
@ -734,7 +734,7 @@ def test_cram_resched():
col.reset() col.reset()
c = col.sched.getCard() c = col.sched.getCard()
col.sched.answerCard(c, 1) col.sched.answerCard(c, 1)
col.sched.emptyDyn(did) col.sched.empty_filtered_deck(did)
c.load() c.load()
assert c.ivl == 100 assert c.ivl == 100
assert c.due == col.sched.today + 25 assert c.due == col.sched.today + 25
@ -746,7 +746,7 @@ def test_cram_resched():
c = col.sched.getCard() c = col.sched.getCard()
col.sched.answerCard(c, 1) col.sched.answerCard(c, 1)
col.sched.answerCard(c, 3) col.sched.answerCard(c, 3)
col.sched.emptyDyn(did) col.sched.empty_filtered_deck(did)
c.load() c.load()
assert c.ivl == 100 assert c.ivl == 100
assert c.due == col.sched.today + 25 assert c.due == col.sched.today + 25
@ -758,7 +758,7 @@ def test_cram_resched():
col.reset() col.reset()
c = col.sched.getCard() c = col.sched.getCard()
col.sched.answerCard(c, 3) col.sched.answerCard(c, 3)
col.sched.emptyDyn(did) col.sched.empty_filtered_deck(did)
c.load() c.load()
assert c.ivl == 100 assert c.ivl == 100
assert c.due == -25 assert c.due == -25
@ -770,7 +770,7 @@ def test_cram_resched():
col.reset() col.reset()
c = col.sched.getCard() c = col.sched.getCard()
col.sched.answerCard(c, 1) col.sched.answerCard(c, 1)
col.sched.emptyDyn(did) col.sched.empty_filtered_deck(did)
c.load() c.load()
assert c.ivl == 100 assert c.ivl == 100
assert c.due == -25 assert c.due == -25

View File

@ -773,7 +773,7 @@ def test_filt_keep_lrn_state():
assert c.due - intTime() > 60 * 60 assert c.due - intTime() > 60 * 60
# emptying the deck preserves learning state # emptying the deck preserves learning state
col.sched.emptyDyn(did) col.sched.empty_filtered_deck(did)
c.load() c.load()
assert c.type == CARD_TYPE_LRN and c.queue == QUEUE_TYPE_LRN assert c.type == CARD_TYPE_LRN and c.queue == QUEUE_TYPE_LRN
assert c.left == 1001 assert c.left == 1001
@ -823,7 +823,7 @@ def test_preview():
assert c.id == orig.id assert c.id == orig.id
# emptying the filtered deck should restore card # emptying the filtered deck should restore card
col.sched.emptyDyn(did) col.sched.empty_filtered_deck(did)
c.load() c.load()
assert c.queue == QUEUE_TYPE_NEW assert c.queue == QUEUE_TYPE_NEW
assert c.reps == 0 assert c.reps == 0
@ -1255,7 +1255,7 @@ def test_negativeDueFilter():
# into and out of filtered deck # into and out of filtered deck
did = col.decks.newDyn("Cram") did = col.decks.newDyn("Cram")
col.sched.rebuildDyn(did) col.sched.rebuildDyn(did)
col.sched.emptyDyn(did) col.sched.empty_filtered_deck(did)
col.reset() col.reset()
c.load() c.load()

View File

@ -145,7 +145,7 @@ class CustomStudy(QDialog):
return QDialog.accept(self) return QDialog.accept(self)
else: else:
# safe to empty # safe to empty
self.mw.col.sched.emptyDyn(cur["id"]) self.mw.col.sched.empty_filtered_deck(cur["id"])
# reuse; don't delete as it may have children # reuse; don't delete as it may have children
dyn = cur dyn = cur
self.mw.col.decks.select(cur["id"]) self.mw.col.decks.select(cur["id"])

View File

@ -79,7 +79,7 @@ class Overview:
self.mw.col.sched.rebuildDyn() self.mw.col.sched.rebuildDyn()
self.mw.reset() self.mw.reset()
elif url == "empty": elif url == "empty":
self.mw.col.sched.emptyDyn(self.mw.col.decks.selected()) self.mw.col.sched.empty_filtered_deck(self.mw.col.decks.selected())
self.mw.reset() self.mw.reset()
elif url == "decks": elif url == "decks":
self.mw.moveToState("deckBrowser") self.mw.moveToState("deckBrowser")
@ -112,7 +112,7 @@ class Overview:
def onEmptyKey(self): def onEmptyKey(self):
if self._filteredDeck(): if self._filteredDeck():
self.mw.col.sched.emptyDyn(self.mw.col.decks.selected()) self.mw.col.sched.empty_filtered_deck(self.mw.col.decks.selected())
self.mw.reset() self.mw.reset()
def onCustomStudyKey(self): def onCustomStudyKey(self):