mirror of
https://github.com/ankitects/anki.git
synced 2025-11-10 22:57:11 -05:00
add template deletion & unit test
This commit is contained in:
parent
4638d3de46
commit
0db95ded74
3 changed files with 45 additions and 48 deletions
32
anki/deck.py
32
anki/deck.py
|
|
@ -762,38 +762,6 @@ where id in %s""" % ids2str(ids), new=new.id, ord=new.ord)
|
|||
self.db.executemany("insert into fsums values (?,?,?)", r)
|
||||
self.db.executemany("update facts set sfld = ? where id = ?", r2)
|
||||
|
||||
# Card models
|
||||
##########################################################################
|
||||
|
||||
def templateUseCount(self, template):
|
||||
"Return the number of cards using template."
|
||||
return self.db.scalar("""
|
||||
select count(id) from cards where
|
||||
tid = :id""", id=template.id)
|
||||
|
||||
def addCardModel(self, model, template):
|
||||
self.modSchema()
|
||||
model.addCardModel(template)
|
||||
|
||||
def deleteCardModel(self, model, template):
|
||||
"Delete all cards that use CARDMODEL from the deck."
|
||||
self.modSchema()
|
||||
cards = self.db.list("select id from cards where tid = :id",
|
||||
id=template.id)
|
||||
self.deleteCards(cards)
|
||||
model.templates.remove(template)
|
||||
model.flush()
|
||||
|
||||
def rebuildCardOrds(self, ids):
|
||||
"Update all card models in IDS. Caller must update model modtime."
|
||||
self.modSchema()
|
||||
strids = ids2str(ids)
|
||||
self.db.execute("""
|
||||
update cards set
|
||||
ord = (select ord from templates where id = tid),
|
||||
mod = :now
|
||||
where tid in %s""" % strids, now=time.time())
|
||||
|
||||
# Q/A generation
|
||||
##########################################################################
|
||||
|
||||
|
|
|
|||
|
|
@ -80,16 +80,6 @@ insert or replace into models values (?, ?, ?, ?, ?, ?, ?)""",
|
|||
def fids(self):
|
||||
return self.deck.db.list("select id from facts where mid = ?", self.id)
|
||||
|
||||
# Templates
|
||||
##################################################
|
||||
|
||||
def newTemplate(self):
|
||||
return defaultTemplate.copy()
|
||||
|
||||
def addTemplate(self, template):
|
||||
self.deck.modSchema()
|
||||
self.templates.append(template)
|
||||
|
||||
# Copying
|
||||
##################################################
|
||||
|
||||
|
|
@ -142,7 +132,7 @@ insert or replace into models values (?, ?, ?, ?, ?, ?, ?)""",
|
|||
t = "%s {%s}\n" % (prefix, t)
|
||||
return t
|
||||
|
||||
# Field basics
|
||||
# Fields
|
||||
##################################################
|
||||
|
||||
def fieldMap(self):
|
||||
|
|
@ -157,9 +147,6 @@ insert or replace into models values (?, ?, ?, ?, ?, ?, ?)""",
|
|||
self.conf['sortf'] = idx
|
||||
self.deck.updateFieldCache(self.fids(), csum=False)
|
||||
|
||||
# Adding/deleting/moving fields
|
||||
##################################################
|
||||
|
||||
def newField(self):
|
||||
return defaultField.copy()
|
||||
|
||||
|
|
@ -171,7 +158,7 @@ insert or replace into models values (?, ?, ?, ?, ?, ?, ?)""",
|
|||
return fields
|
||||
self._transformFields(add)
|
||||
|
||||
def deleteField(self, field):
|
||||
def delField(self, field):
|
||||
idx = self.fields.index(field)
|
||||
self.fields.remove(field)
|
||||
self.flush()
|
||||
|
|
@ -222,3 +209,27 @@ insert or replace into models values (?, ?, ?, ?, ?, ?, ?)""",
|
|||
r.append((joinFields(fn(splitFields(flds))), id))
|
||||
self.deck.db.executemany("update facts set flds = ? where id = ?", r)
|
||||
self.deck.finishProgress()
|
||||
|
||||
# Templates
|
||||
##################################################
|
||||
|
||||
def newTemplate(self):
|
||||
return defaultTemplate.copy()
|
||||
|
||||
def addTemplate(self, template):
|
||||
self.deck.modSchema()
|
||||
self.templates.append(template)
|
||||
|
||||
def delTemplate(self, template):
|
||||
self.deck.modSchema()
|
||||
ord = self.templates.index(template)
|
||||
cids = self.deck.db.list("""
|
||||
select c.id from cards c, facts f where c.fid=f.id and mid = ? and ord = ?""",
|
||||
self.id, ord)
|
||||
self.deck.deleteCards(cids)
|
||||
# shift ordinals
|
||||
self.deck.db.execute("""
|
||||
update cards set ord = ord - 1 where fid in (select id from facts
|
||||
where mid = ?) and ord > ?""", self.id, ord)
|
||||
self.templates.remove(template)
|
||||
self.flush()
|
||||
|
|
|
|||
|
|
@ -45,7 +45,7 @@ def test_fields():
|
|||
m.renameField(f, "bar")
|
||||
assert d.getFact(m.fids()[0])['bar'] == ''
|
||||
# delete back
|
||||
m.deleteField(m.fields[1])
|
||||
m.delField(m.fields[1])
|
||||
assert d.getFact(m.fids()[0])._fields == ["1", ""]
|
||||
# move 0 -> 1
|
||||
m.moveField(m.fields[0], 1)
|
||||
|
|
@ -71,6 +71,24 @@ def test_fields():
|
|||
m.moveField(m.fields[0], 1)
|
||||
assert d.getFact(m.fids()[0])._fields == ["", "2", "1"]
|
||||
|
||||
def test_templates():
|
||||
d = getEmptyDeck()
|
||||
m = d.currentModel()
|
||||
m.templates[1]['actv'] = True
|
||||
m.flush()
|
||||
f = d.newFact()
|
||||
f['Front'] = u'1'
|
||||
f['Back'] = u'2'
|
||||
d.addFact(f)
|
||||
assert d.cardCount() == 2
|
||||
# removing a template should delete its cards
|
||||
m.delTemplate(m.templates[0])
|
||||
assert d.cardCount() == 1
|
||||
# and should have updated the other cards' ordinals
|
||||
c = f.cards()[0]
|
||||
assert c.ord == 0
|
||||
stripHTML(c.q()) == "2"
|
||||
|
||||
def test_modelChange():
|
||||
print "model change"
|
||||
return
|
||||
|
|
|
|||
Loading…
Reference in a new issue