From e582d8284bd5927a0afe41aba041ceec8fd444f4 Mon Sep 17 00:00:00 2001 From: David Snopek Date: Tue, 23 Jul 2013 00:33:53 +0100 Subject: [PATCH] * Added the ability to suspend/unsuspend cards. * Added the ability to add/remove tags. --- AnkiServer/apps/rest_app.py | 40 ++++++++++++++++++++++++++++++++ tests/test_rest_app.py | 46 +++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+) diff --git a/AnkiServer/apps/rest_app.py b/AnkiServer/apps/rest_app.py index 3fe7131..cc8edc9 100644 --- a/AnkiServer/apps/rest_app.py +++ b/AnkiServer/apps/rest_app.py @@ -412,6 +412,16 @@ class CollectionHandler(RestHandlerBase): col.sched.answerCard(card, ease) + @noReturnValue + def suspend_cards(self, col, req): + card_ids = req.data['ids'] + col.sched.suspendCards(card_ids) + + @noReturnValue + def unsuspend_cards(self, col, req): + card_ids = req.data['ids'] + col.sched.unsuspendCards(card_ids) + # # GLOBAL / MISC # @@ -509,6 +519,20 @@ class NoteHandler(RestHandlerBase): note = col.getNote(req.ids[1]) return self._serialize(note) + @noReturnValue + def add_tags(self, col, req): + note = col.getNote(req.ids[1]) + for tag in req.data['tags']: + note.addTag(tag) + note.flush() + + @noReturnValue + def remove_tags(self, col, req): + note = col.getNote(req.ids[1]) + for tag in req.data['tags']: + note.delTag(tag) + note.flush() + class DeckHandler(RestHandlerBase): """Default handler group for 'deck' type.""" @@ -573,6 +597,22 @@ class CardHandler(RestHandlerBase): card = col.getCard(req.ids[1]) return self._serialize(card, req.data) + def _forward_to_note(self, card_id, name): + card = col.getCard(card_id) + + req_copy = req.copy() + req_copy.ids[1] = card.nid + + return self.app.execute_handler('note', name, col, req) + + @noReturnValue + def add_tags(self, col, req): + self._forward_to_note(req.ids[1], 'add_tags') + + @noReturnValue + def remove_tags(self, col, req): + self._forward_to_note(req.ids[1], 'remove_tags') + # Our entry point def make_app(global_conf, **local_conf): # TODO: we should setup the default language from conf! diff --git a/tests/test_rest_app.py b/tests/test_rest_app.py index 39405ca..8f3e45e 100644 --- a/tests/test_rest_app.py +++ b/tests/test_rest_app.py @@ -349,6 +349,28 @@ class CollectionHandlerTest(CollectionTestBase): card = self.execute('next_card', {}) self.assertEqual(card['reps'], 1) + def test_suspend_cards(self): + # add a note programatically + self.add_default_note() + + # get the id for the one card on this collection + card_id = self.collection.findCards('')[0] + + # suspend it + self.execute('suspend_cards', {'ids': [card_id]}) + + # test that getting the next card will be None + card = self.collection.sched.getCard() + self.assertEqual(card, None) + + # unsuspend it + self.execute('unsuspend_cards', {'ids': [card_id]}) + + # test that now we're getting the next card! + self.collection.sched.reset() + card = self.collection.sched.getCard() + self.assertEqual(card.id, card_id) + class ImportExportHandlerTest(CollectionTestBase): export_rows = [ ['Card front 1', 'Card back 1', 'Tag1 Tag2'], @@ -431,6 +453,30 @@ class NoteHandlerTest(CollectionTestBase): self.assertEqual(ret['string_tags'], 'Tag1 Tag2') self.assertEqual(ret['usn'], -1) + def test_add_tags(self): + self.add_default_note() + note_id = self.collection.findNotes('')[0] + note = self.collection.getNote(note_id) + self.assertFalse('NT1' in note.tags) + self.assertFalse('NT2' in note.tags) + + self.execute('add_tags', {'tags': ['NT1', 'NT2']}, note_id) + note = self.collection.getNote(note_id) + self.assertTrue('NT1' in note.tags) + self.assertTrue('NT2' in note.tags) + + def test_remove_tags(self): + self.add_default_note() + note_id = self.collection.findNotes('')[0] + note = self.collection.getNote(note_id) + self.assertTrue('Tag1' in note.tags) + self.assertTrue('Tag2' in note.tags) + + self.execute('remove_tags', {'tags': ['Tag1', 'Tag2']}, note_id) + note = self.collection.getNote(note_id) + self.assertFalse('Tag1' in note.tags) + self.assertFalse('Tag2' in note.tags) + class DeckHandlerTest(CollectionTestBase): def setUp(self): super(DeckHandlerTest, self).setUp()