diff --git a/anki/models.py b/anki/models.py index 62b3b82db..b32792c82 100644 --- a/anki/models.py +++ b/anki/models.py @@ -464,7 +464,7 @@ select id from notes where mid = ?)""" % " ".join(map), a = [] b = [] for f in flds: - a.append("1") + a.append("ankiflag") b.append("") data = [1, 1, m['id'], 1, t['ord'], "", joinFields(a)] full = self.col._renderQA(data)['q'] @@ -480,8 +480,8 @@ select id from notes where mid = ?)""" % " ".join(map), tmp = a[:] tmp[i] = "" data[6] = joinFields(tmp) - # if the result is same as empty, field is required - if self.col._renderQA(data)['q'] == empty: + # if no field content appeared, field is required + if "ankiflag" not in self.col._renderQA(data)['q']: req.append(i) if req: return type, req diff --git a/tests/test_models.py b/tests/test_models.py index fde8644c7..56d673935 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -1,7 +1,7 @@ # coding: utf-8 from tests.shared import getEmptyDeck, assertException -from anki.utils import stripHTML +from anki.utils import stripHTML, joinFields def test_modelDelete(): deck = getEmptyDeck() @@ -230,3 +230,29 @@ def test_modelChange(): f.load() assert f['Text'] == "f2" assert len(f.cards()) == 2 + +def test_availOrds(): + d = getEmptyDeck() + m = d.models.current(); mm = d.models + t = m['tmpls'][0] + f = d.newNote() + f['Front'] = "1" + # simple templates + assert mm.availOrds(m, joinFields(f.fields)) == [0] + t['qfmt'] = "{{Back}}" + mm.save(m, templates=True) + assert not mm.availOrds(m, joinFields(f.fields)) + # AND + t['qfmt'] = "{{#Front}}{{#Back}}{{Front}}{{/Back}}{{/Front}}" + mm.save(m, templates=True) + assert not mm.availOrds(m, joinFields(f.fields)) + t['qfmt'] = "{{#Front}}\n{{#Back}}\n{{Front}}\n{{/Back}}\n{{/Front}}" + mm.save(m, templates=True) + assert not mm.availOrds(m, joinFields(f.fields)) + # OR + t['qfmt'] = "{{Front}}\n{{Back}}" + mm.save(m, templates=True) + assert mm.availOrds(m, joinFields(f.fields)) == [0] + t['Front'] = "" + t['Back'] = "1" + assert mm.availOrds(m, joinFields(f.fields)) == [0]