Skip to content

Commit

Permalink
Implement ability to set review order of notes (#80)
Browse files Browse the repository at this point in the history
Allow passing `due` value when constructing a `Note`. Notes with lower `due` value will appear sooner when reviewing.
  • Loading branch information
z1lc authored May 28, 2021
1 parent 10a356b commit 6ceffc1
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
4 changes: 2 additions & 2 deletions genanki/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ def __init__(self, ord, suspend=False):
self.ord = ord
self.suspend = suspend

def write_to_db(self, cursor, timestamp: float, deck_id, note_id, id_gen):
def write_to_db(self, cursor, timestamp: float, deck_id, note_id, id_gen, due=0):
queue = -1 if self.suspend else 0
cursor.execute('INSERT INTO cards VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);', (
next(id_gen), # id
Expand All @@ -14,7 +14,7 @@ def write_to_db(self, cursor, timestamp: float, deck_id, note_id, id_gen):
-1, # usn
0, # type (=0 for non-Cloze)
queue, # queue
0, # due
due, # due
0, # ivl
0, # factor
0, # reps
Expand Down
5 changes: 3 additions & 2 deletions genanki/note.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ def insert(self, i, tag):
class Note:
_INVALID_HTML_TAG_RE = re.compile(r'<(?!/?[a-z0-9]+(?: .*|/?)>)(?:.|\n)*?>')

def __init__(self, model=None, fields=None, sort_field=None, tags=None, guid=None):
def __init__(self, model=None, fields=None, sort_field=None, tags=None, guid=None, due=0):
self.model = model
self.fields = fields
self.sort_field = sort_field
self.tags = tags or []
self.due = due
try:
self.guid = guid
except AttributeError:
Expand Down Expand Up @@ -165,7 +166,7 @@ def write_to_db(self, cursor, timestamp: float, deck_id, id_gen):

note_id = cursor.lastrowid
for card in self.cards:
card.write_to_db(cursor, timestamp, deck_id, note_id, id_gen)
card.write_to_db(cursor, timestamp, deck_id, note_id, id_gen, self.due)

def _format_fields(self):
return '\x1f'.join(self.fields)
Expand Down
45 changes: 45 additions & 0 deletions tests/test_genanki.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,48 @@ def test_model_with_sort_field_index(self):

anki_note = self.col.getNote(self.col.findNotes('')[0])
assert anki_note.model()['sortf'] == CUSTOM_SORT_FIELD_INDEX

def test_notes_with_due1(self):
deck = genanki.Deck(4145273926, 'foodeck')
deck.add_note(genanki.Note(
TEST_MODEL,
['Capital of Washington', 'Olympia'],
due=1))
deck.add_note(genanki.Note(
TEST_MODEL,
['Capital of Oregon', 'Salem'],
due=2))

self.import_package(genanki.Package(deck))

self.col.decks.select(self.col.decks.id('foodeck'))
self.col.sched.reset()
next_card = self.col.sched.getCard()
next_note = self.col.getNote(next_card.nid)

# Next card is the one with lowest due value.
assert next_note.fields == ['Capital of Washington', 'Olympia']

def test_notes_with_due2(self):
# Same as test_notes_with_due1, but we switch the due values
# for the two notes.
deck = genanki.Deck(4145273927, 'foodeck')
deck.add_note(genanki.Note(
TEST_MODEL,
['Capital of Washington', 'Olympia'],
due=2))
deck.add_note(genanki.Note(
TEST_MODEL,
['Capital of Oregon', 'Salem'],
due=1))

self.import_package(genanki.Package(deck))

self.col.decks.select(self.col.decks.id('foodeck'))
self.col.sched.reset()
next_card = self.col.sched.getCard()
next_note = self.col.getNote(next_card.nid)

# Next card changes to "Capital of Oregon", because it has lower
# due value.
assert next_note.fields == ['Capital of Oregon', 'Salem']

0 comments on commit 6ceffc1

Please sign in to comment.