From a1d602f154ff9f2bd845dcf9746b7ac146abaa64 Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Sun, 15 Dec 2019 22:54:19 -0800 Subject: [PATCH 1/2] update requireds saves reqs as lists instead of tuples. The goal of this change is to be consistent with JSON. Indeed, the dictionnary is saved as JSON, which has list and does not has tuple. The request was made in https://github.com/dae/anki/pull/361#issuecomment-565915191 --- anki/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/anki/models.py b/anki/models.py index 08d366f3d..57808b101 100644 --- a/anki/models.py +++ b/anki/models.py @@ -498,7 +498,7 @@ select id from notes where mid = ?)""" % " ".join(map), flds = [f['name'] for f in m['flds']] for t in m['tmpls']: ret = self._reqForTemplate(m, flds, t) - req.append((t['ord'], ret[0], ret[1])) + req.append([t['ord'], ret[0], ret[1]]) m['req'] = req def _reqForTemplate(self, m, flds, t): From e96ca691d047ba2ed42fd0acafda10e30dfa231c Mon Sep 17 00:00:00 2001 From: Arthur Milchior Date: Sun, 15 Dec 2019 10:40:38 -0800 Subject: [PATCH 2/2] test req --- tests/test_models.py | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/tests/test_models.py b/tests/test_models.py index a3bebfd97..b1cfbce5f 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,6 +1,7 @@ # coding: utf-8 from tests.shared import getEmptyCol +from anki.consts import MODEL_CLOZE from anki.utils import stripHTML, joinFields import anki.template @@ -328,3 +329,28 @@ def test_availOrds(): t['Front'] = "" t['Back'] = "1" assert mm.availOrds(m, joinFields(f.fields)) == [0] + +def test_req(): + def reqSize(model): + if model['type'] == MODEL_CLOZE: + return + assert (len(model['tmpls']) == len(model['req'])) + + d = getEmptyCol() + mm = d.models + basic = mm.byName("Basic") + assert 'req' in basic + reqSize(basic) + assert basic['req'][0] == [0, 'all', [0]] + opt = mm.byName("Basic (optional reversed card)") + reqSize(opt) + assert opt['req'][0] == [0, 'all', [0]] + assert opt['req'][1] == [1, 'all', [1, 2]] + #testing any + opt['tmpls'][1]['qfmt'] = "{{Back}}{{Add Reverse}}" + mm.save(opt, templates=True) + assert opt['req'][1] == [1, 'any', [1, 2]] + #testing None + opt['tmpls'][1]['qfmt'] = "{{^Add Reverse}}{{Back}}{{/Add Reverse}}" + mm.save(opt, templates=True) + assert opt['req'][1] == [1, 'none', []]