Implemented 'cards_recent_ease' to pull a list of the most recent answers to a list of cards.

This commit is contained in:
David Snopek 2013-07-27 13:41:20 +01:00
parent e8b9204b7b
commit 442037c211
2 changed files with 35 additions and 0 deletions

View File

@ -485,6 +485,23 @@ class CollectionHandler(RestHandlerBase):
card_ids = req.data['ids'] card_ids = req.data['ids']
col.sched.unsuspendCards(card_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 = { stats_reports = {
'today': 'todayStats', 'today': 'todayStats',
'due': 'dueGraph', 'due': 'dueGraph',

View File

@ -408,6 +408,24 @@ class CollectionHandlerTest(CollectionTestBase):
card = self.collection.sched.getCard() card = self.collection.sched.getCard()
self.assertEqual(card.id, card_id) 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): class ImportExportHandlerTest(CollectionTestBase):
export_rows = [ export_rows = [
['Card front 1', 'Card back 1', 'Tag1 Tag2'], ['Card front 1', 'Card back 1', 'Tag1 Tag2'],