From dc01014daf1a9e7aeebdc0b15d39872cb3d7a150 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Sun, 6 Aug 2017 02:29:59 +0100 Subject: [PATCH] Fix issue #20; add isSuspended function; update code style --- AnkiConnect.py | 38 ++++++++++++++++++++++++++++++++------ README.md | 29 +++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index 7d32328..36d2f7c 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -377,15 +377,36 @@ class AnkiBridge: def addTags(self, notes, tags, add=True): self.startEditing() - aqt.mw.col.tags.bulkAdd(notes, tags, add) + self.collection().tags.bulkAdd(notes, tags, add) self.stopEditing() def suspend(self, cards, suspend=True): - if suspend: - aqt.mw.col.sched.suspendCards(cards) + for card in cards: + isSuspended = self.isSuspended(card) + if suspend and isSuspended: + cards.remove(card) + elif not suspend and not isSuspended: + cards.remove(card) + + if cards: + self.startEditing() + if suspend: + self.collection().sched.suspendCards(cards) + else: + self.collection().sched.unsuspendCards(cards) + self.stopEditing() + return True + + return False + + + def isSuspended(self, card): + card = self.collection().getCard(card) + if card.queue == -1: + return True else: - aqt.mw.col.sched.unsuspendCards(cards) + return False def startEditing(self): @@ -464,14 +485,14 @@ class AnkiBridge: def findNotes(self, query=None): if query is not None: - return aqt.mw.col.findNotes(query) + return self.collection().findNotes(query) else: return [] def findCards(self, query=None): if query is not None: - return aqt.mw.col.findCards(query) + return self.collection().findCards(query) else: return [] @@ -702,6 +723,11 @@ class AnkiConnect: return self.anki.suspend(cards, False) + @webApi + def isSuspended(self, card): + return self.anki.isSuspended(card) + + @webApi def upgrade(self): response = QMessageBox.question( diff --git a/README.md b/README.md index 980f4fe..b3ec799 100644 --- a/README.md +++ b/README.md @@ -347,7 +347,8 @@ Below is a list of currently supported actions. Requests with invalid actions or * **suspend** - Suspend cards by card ID. + Suspend cards by card ID; returns `true` if successful (at least one card wasn't already suspended) or `false` + otherwise. *Sample request*: ``` @@ -361,12 +362,13 @@ Below is a list of currently supported actions. Requests with invalid actions or *Sample response*: ``` - null + true ``` * **unsuspend** - Unsuspend cards by card ID. + Unsuspend cards by card ID; returns `true` if successful (at least one card was previously suspended) or `false` + otherwise. *Sample request*: ``` @@ -380,7 +382,26 @@ Below is a list of currently supported actions. Requests with invalid actions or *Sample response*: ``` - null + true + ``` + +* **isSuspended** + + Returns `true` if the given card is suspended or `false` otherwise. + + *Sample request*: + ``` + { + "action": "isSuspended", + "params": { + "card": 1483959291685 + } + } + ``` + + *Sample response*: + ``` + false ``` * **findNotes**