diff --git a/AnkiServer/apps/rest_app.py b/AnkiServer/apps/rest_app.py index b47298d..a4fca4d 100644 --- a/AnkiServer/apps/rest_app.py +++ b/AnkiServer/apps/rest_app.py @@ -485,6 +485,23 @@ class CollectionHandler(RestHandlerBase): card_ids = req.data['ids'] col.sched.unsuspendCards(card_ids) + def cards_recent_ease(self, col, req): + """Returns the most recent ease for each card.""" + + # TODO: Use sqlalchemy to build this query! + sql = "SELECT r.cid, r.ease, r.id FROM revlog AS r INNER JOIN (SELECT cid, MAX(id) AS id FROM revlog GROUP BY cid) AS q ON r.cid = q.cid AND r.id = q.id" + where = [] + if req.data.has_key('ids'): + where.append('ids IN (' + (','.join(["'%s'" % x for x in req.data['ids']])) + ')') + if len(where) > 0: + sql += ' WHERE ' + ' AND '.join(where) + + result = [] + for r in col.db.all(sql): + result.append({'id': r[0], 'ease': r[1], 'timestamp': int(r[2] / 1000)}) + + return result + stats_reports = { 'today': 'todayStats', 'due': 'dueGraph', diff --git a/tests/test_rest_app.py b/tests/test_rest_app.py index 75ce633..25730da 100644 --- a/tests/test_rest_app.py +++ b/tests/test_rest_app.py @@ -408,6 +408,24 @@ class CollectionHandlerTest(CollectionTestBase): card = self.collection.sched.getCard() self.assertEqual(card.id, card_id) + def test_cards_recent_ease(self): + self.add_default_note() + card_id = self.collection.findCards('')[0] + + # answer the card + self.collection.reset() + card = self.collection.sched.getCard() + card.startTimer() + # answer multiple times to see that we only get the latest! + self.collection.sched.answerCard(card, 1) + self.collection.sched.answerCard(card, 3) + self.collection.sched.answerCard(card, 2) + + # pull the latest revision + ret = self.execute('cards_recent_ease', {}) + self.assertEqual(ret[0]['id'], card_id) + self.assertEqual(ret[0]['ease'], 2) + class ImportExportHandlerTest(CollectionTestBase): export_rows = [ ['Card front 1', 'Card back 1', 'Tag1 Tag2'],