mirror of
https://github.com/ankitects/anki.git
synced 2025-09-24 16:56:36 -04:00
template moving
This commit is contained in:
parent
c8b16a0e0e
commit
de81f0238a
2 changed files with 27 additions and 0 deletions
|
@ -254,6 +254,24 @@ where mid = ?) and ord > ?""", self.id, ord)
|
||||||
for c, t in enumerate(self.templates):
|
for c, t in enumerate(self.templates):
|
||||||
t['ord'] = c
|
t['ord'] = c
|
||||||
|
|
||||||
|
def moveTemplate(self, template, idx):
|
||||||
|
oldidx = self.templates.index(template)
|
||||||
|
if oldidx == idx:
|
||||||
|
return
|
||||||
|
oldidxs = dict([(id(t), t['ord']) for t in self.templates])
|
||||||
|
self.templates.remove(template)
|
||||||
|
self.templates.insert(idx, template)
|
||||||
|
self._updateTemplOrds()
|
||||||
|
# generate change map
|
||||||
|
map = []
|
||||||
|
for t in self.templates:
|
||||||
|
map.append("when ord = %d then %d" % (oldidxs[id(t)], t['ord']))
|
||||||
|
# apply
|
||||||
|
self.flush()
|
||||||
|
self.deck.db.execute("""
|
||||||
|
update cards set ord = (case %s end) where fid in (
|
||||||
|
select id from facts where mid = ?)""" % " ".join(map), self.id)
|
||||||
|
|
||||||
# Model changing
|
# Model changing
|
||||||
##########################################################################
|
##########################################################################
|
||||||
|
|
||||||
|
|
|
@ -81,6 +81,15 @@ def test_templates():
|
||||||
f['Back'] = u'2'
|
f['Back'] = u'2'
|
||||||
d.addFact(f)
|
d.addFact(f)
|
||||||
assert d.cardCount() == 2
|
assert d.cardCount() == 2
|
||||||
|
(c, c2) = f.cards()
|
||||||
|
# first card should have first ord
|
||||||
|
assert c.ord == 0
|
||||||
|
assert c2.ord == 1
|
||||||
|
# switch templates
|
||||||
|
m.moveTemplate(c.template(), 1)
|
||||||
|
c.load(); c2.load()
|
||||||
|
assert c.ord == 1
|
||||||
|
assert c2.ord == 0
|
||||||
# removing a template should delete its cards
|
# removing a template should delete its cards
|
||||||
m.delTemplate(m.templates[0])
|
m.delTemplate(m.templates[0])
|
||||||
assert d.cardCount() == 1
|
assert d.cardCount() == 1
|
||||||
|
|
Loading…
Reference in a new issue