Skip to content

Commit

Permalink
Support "any"-style req for card gen
Browse files Browse the repository at this point in the history
  • Loading branch information
kerrickstaley committed Feb 19, 2017
1 parent f29b98f commit 95e0e6e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
5 changes: 3 additions & 2 deletions genanki/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,9 @@ def sort_field(self, val):
@cached_property
def cards(self):
rv = []
for card_ord, _, required_field_ords in self.model._req:
if all(self.fields[ord_] for ord_ in required_field_ords):
for card_ord, any_or_all, required_field_ords in self.model._req:
op = {'any': any, 'all': all}[any_or_all]
if op(self.fields[ord_] for ord_ in required_field_ords):
rv.append(Card(card_ord))
return rv

Expand Down
12 changes: 11 additions & 1 deletion tests/test_genanki.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def test_Model_req__cn(self):
def test_Model_req__with_hint(self):
assert TEST_MODEL_WITH_HINT._req == [[0, 'any', [0, 1]]]

def test_notes_generate_cards_based_on_req(self):
def test_notes_generate_cards_based_on_req__cn(self):
# has 'Simplified' field, will generate a 'Simplified' card
n1 = genanki.Note(model=TEST_CN_MODEL, fields=['中國', '中国', 'China'])
# no 'Simplified' field, so it won't generate a 'Simplified' card
Expand All @@ -133,6 +133,16 @@ def test_notes_generate_cards_based_on_req(self):
assert len(n2.cards) == 1
assert n2.cards[0].ord == 0

def test_notes_generate_cards_based_on_req__with_hint(self):
# both of these notes will generate one card
n1 = genanki.Note(model=TEST_MODEL_WITH_HINT, fields=['capital of California', '', 'Sacramento'])
n2 = genanki.Note(model=TEST_MODEL_WITH_HINT, fields=['capital of Iowa', 'French for "The Moines"', 'Des Moines'])

assert len(n1.cards) == 1
assert n1.cards[0].ord == 0
assert len(n2.cards) == 1
assert n2.cards[0].ord == 0

def test_Note_with_guid_property(self):
class MyNote(genanki.Note):
@property
Expand Down

0 comments on commit 95e0e6e

Please sign in to comment.