add back card deletion count

cheaper to look up now that there's an index on notes.mid
This commit is contained in:
Damien Elmes 2020-05-15 15:28:07 +10:00
parent 00db72e365
commit f650e5557f
2 changed files with 31 additions and 10 deletions

View file

@ -443,6 +443,15 @@ class ModelManager:
self.reposition_template(m, template, idx) self.reposition_template(m, template, idx)
self.save(m) self.save(m)
def template_use_count(self, ntid: int, ord: int) -> int:
return self.col.db.scalar(
"""
select count() from cards, notes where cards.nid = notes.id
and notes.mid = ? and cards.ord = ?""",
ntid,
ord,
)
# Model changing # Model changing
########################################################################## ##########################################################################
# - maps are ord->ord, and there should not be duplicate targets # - maps are ord->ord, and there should not be duplicate targets

View file

@ -34,8 +34,6 @@ from aqt.utils import (
) )
from aqt.webview import AnkiWebView from aqt.webview import AnkiWebView
# fixme: card count when removing
class CardLayout(QDialog): class CardLayout(QDialog):
def __init__( def __init__(
@ -497,15 +495,29 @@ class CardLayout(QDialog):
def onRemove(self): def onRemove(self):
if len(self.templates) < 2: if len(self.templates) < 2:
return showInfo(_("At least one card type is required.")) return showInfo(_("At least one card type is required."))
template = self.current_template()
msg = _("Delete the '%(a)s' card type, and its %(b)s?") % dict(
a=template["name"], b=_("cards")
)
if not askUser(msg):
return
if not self.change_tracker.mark_schema(): def get_count():
return return self.mm.template_use_count(self.model["id"], self.ord)
def on_done(fut):
card_cnt = fut.result()
template = self.current_template()
cards = ngettext("%d card", "%d cards", card_cnt) % card_cnt
msg = _("Delete the '%(a)s' card type, and its %(b)s?") % dict(
a=template["name"], b=cards
)
if not askUser(msg):
return
if not self.change_tracker.mark_schema():
return
self.onRemoveInner(template)
self.mw.taskman.with_progress(get_count, on_done)
def onRemoveInner(self, template) -> None:
self.mm.remove_template(self.model, template) self.mm.remove_template(self.model, template)
# ensure current ordinal is within bounds # ensure current ordinal is within bounds