From e8088146f37d3d2f4290ca7013b56ac6ab0fbf2c Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 3 Aug 2017 21:07:22 +0100 Subject: [PATCH 1/3] Add tagging and suspend functions --- AnkiConnect.py | 34 ++++++++++++++++++++++ README.md | 78 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) diff --git a/AnkiConnect.py b/AnkiConnect.py index f323d50..82798a8 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -375,6 +375,20 @@ class AnkiBridge: return note + def addTags(self, query, tags, add=True): + notes = aqt.mw.col.findNotes(query) + aqt.mw.col.tags.bulkAdd(notes, tags, add) + + + def suspend(self, query, suspend=True): + cards = aqt.mw.col.findCards(query) + if suspend: + suspendFunction = aqt.mw.col.sched.suspendCards + else: + suspendFunction = aqt.mw.col.sched.unsuspendCards + suspendFunction(cards) + + def startEditing(self): self.window().requireReset() @@ -662,6 +676,26 @@ class AnkiConnect: return results + @webApi + def addTags(self, query, tags, add=True): + return self.anki.addTags(query, tags, add) + + + @webApi + def removeTags(self, query, tags): + return self.anki.addTags(query, tags, False) + + + @webApi + def suspend(self, query, suspend=True): + return self.anki.suspend(query, suspend) + + + @webApi + def unsuspend(self, query): + return self.anki.suspend(query, False) + + @webApi def upgrade(self): response = QMessageBox.question( diff --git a/README.md b/README.md index a824339..b0ba8f5 100644 --- a/README.md +++ b/README.md @@ -305,6 +305,84 @@ Below is a list of currently supported actions. Requests with invalid actions or ] ``` +* **addTags** + + Adds tags to notes matching a query (same syntax as **browse**/**guiBrowse**). + + *Sample request*: + ``` + { + "action": "addTags", + "params": { + "query": "deck:French or deck:Spanish", + "tags": "european-languages" + } + } + ``` + + *Sample response*: + ``` + null + ``` + +* **removeTags** + + Remove tags from notes matching a query (same syntax as **browse**/**guiBrowse**). + + *Sample request*: + ``` + { + "action": "removeTags", + "params": { + "query": "deck:Japanese or deck:Chinese", + "tags": "european-languages" + } + } + ``` + + *Sample response*: + ``` + null + ``` + +* **suspend** + + Suspend cards matching a query (same syntax as **browse**/**guiBrowse**). + + *Sample request*: + ``` + { + "action": "suspend", + "params": { + "query": "tag:difficult" + } + } + ``` + + *Sample response*: + ``` + null + ``` + +* **unsuspend** + + Unsuspend cards matching a query (same syntax as **browse**/**guiBrowse**). + + *Sample request*: + ``` + { + "action": "unsuspend", + "params": { + "query": "tag:easy" + } + } + ``` + + *Sample response*: + ``` + null + ``` + * **guiBrowse** Invokes the card browser and searches for a given query. Returns an array of identifiers of the cards that were found. From a78d40457cb9aa06a1ef44ba1841675dee5fbf0e Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 3 Aug 2017 22:21:59 +0100 Subject: [PATCH 2/3] Change behaviour of tag/suspend functions --- AnkiConnect.py | 27 ++++++++++++--------------- README.md | 16 ++++++++-------- 2 files changed, 20 insertions(+), 23 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index 82798a8..a09006e 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -375,18 +375,15 @@ class AnkiBridge: return note - def addTags(self, query, tags, add=True): - notes = aqt.mw.col.findNotes(query) + def addTags(self, notes, tags, add=True): aqt.mw.col.tags.bulkAdd(notes, tags, add) - def suspend(self, query, suspend=True): - cards = aqt.mw.col.findCards(query) + def suspend(self, cards, suspend=True): if suspend: - suspendFunction = aqt.mw.col.sched.suspendCards + aqt.mw.col.sched.suspendCards(cards) else: - suspendFunction = aqt.mw.col.sched.unsuspendCards - suspendFunction(cards) + aqt.mw.col.sched.unsuspendCards(cards) def startEditing(self): @@ -677,23 +674,23 @@ class AnkiConnect: @webApi - def addTags(self, query, tags, add=True): - return self.anki.addTags(query, tags, add) + def addTags(self, notes, tags, add=True): + return self.anki.addTags(notes, tags, add) @webApi - def removeTags(self, query, tags): - return self.anki.addTags(query, tags, False) + def removeTags(self, notes, tags): + return self.anki.addTags(notes, tags, False) @webApi - def suspend(self, query, suspend=True): - return self.anki.suspend(query, suspend) + def suspend(self, cards, suspend=True): + return self.anki.suspend(cards, suspend) @webApi - def unsuspend(self, query): - return self.anki.suspend(query, False) + def unsuspend(self, cards): + return self.anki.suspend(cards, False) @webApi diff --git a/README.md b/README.md index b0ba8f5..cc003ba 100644 --- a/README.md +++ b/README.md @@ -307,14 +307,14 @@ Below is a list of currently supported actions. Requests with invalid actions or * **addTags** - Adds tags to notes matching a query (same syntax as **browse**/**guiBrowse**). + Adds tags to notes by note ID. *Sample request*: ``` { "action": "addTags", "params": { - "query": "deck:French or deck:Spanish", + "notes": [1483959289817, 1483959291695], "tags": "european-languages" } } @@ -327,14 +327,14 @@ Below is a list of currently supported actions. Requests with invalid actions or * **removeTags** - Remove tags from notes matching a query (same syntax as **browse**/**guiBrowse**). + Remove tags from notes by note ID. *Sample request*: ``` { "action": "removeTags", "params": { - "query": "deck:Japanese or deck:Chinese", + "notes": [1483959289817, 1483959291695], "tags": "european-languages" } } @@ -347,14 +347,14 @@ Below is a list of currently supported actions. Requests with invalid actions or * **suspend** - Suspend cards matching a query (same syntax as **browse**/**guiBrowse**). + Suspend cards by card ID. *Sample request*: ``` { "action": "suspend", "params": { - "query": "tag:difficult" + "cards": [1483959291685, 1483959293217] } } ``` @@ -366,14 +366,14 @@ Below is a list of currently supported actions. Requests with invalid actions or * **unsuspend** - Unsuspend cards matching a query (same syntax as **browse**/**guiBrowse**). + Unsuspend cards by card ID. *Sample request*: ``` { "action": "unsuspend", "params": { - "query": "tag:easy" + "cards": [1483959291685, 1483959293217] } } ``` From 30f3d5618a6347948b21b629e06537b43aa121d2 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Thu, 3 Aug 2017 22:31:47 +0100 Subject: [PATCH 3/3] Rename browse to findCards and add new findNotes function --- AnkiConnect.py | 34 +++++++++++++++++++++++----------- README.md | 35 +++++++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 17 deletions(-) diff --git a/AnkiConnect.py b/AnkiConnect.py index a09006e..f1e0aec 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -460,6 +460,20 @@ class AnkiBridge: return deck['name'] + def findNotes(self, query=None): + if query is not None: + return aqt.mw.col.findNotes(query) + else: + return [] + + + def findCards(self, query=None): + if query is not None: + return aqt.mw.col.findCards(query) + else: + return [] + + def guiBrowse(self, query=None): browser = aqt.dialogs.open('Browser', self.window()) browser.activateWindow() @@ -474,13 +488,6 @@ class AnkiBridge: return browser.model.cards - def browse(self, query=None): - if query is not None: - return aqt.mw.col.findCards(query) - else: - return [] - - def guiAddCards(self): addCards = aqt.dialogs.open('AddCards', self.window()) addCards.activateWindow() @@ -722,13 +729,18 @@ class AnkiConnect: @webApi - def guiBrowse(self, query=None): - return self.anki.guiBrowse(query) + def findNotes(self, query=None): + return self.anki.findNotes(query) @webApi - def browse(self, query=None): - return self.anki.browse(query) + def findCards(self, query=None): + return self.anki.findCards(query) + + + @webApi + def guiBrowse(self, query=None): + return self.anki.guiBrowse(query) @webApi diff --git a/README.md b/README.md index cc003ba..980f4fe 100644 --- a/README.md +++ b/README.md @@ -383,14 +383,37 @@ Below is a list of currently supported actions. Requests with invalid actions or null ``` -* **guiBrowse** +* **findNotes** - Invokes the card browser and searches for a given query. Returns an array of identifiers of the cards that were found. + Returns an array of note IDs for a given query (same query syntax as **guiBrowse**). *Sample request*: ``` { - "action": "guiBrowse", + "action": "findCards", + "params": { + "query": "deck:current" + } + } + ``` + + *Sample response*: + ``` + [ + 1483959289817, + 1483959291695 + ] + ``` + +* **findCards** + + Returns an array of card IDs for a given query (functionally identical to **guiBrowse** but doesn't use the GUI + for better performance). + + *Sample request*: + ``` + { + "action": "findCards", "params": { "query": "deck:current" } @@ -406,14 +429,14 @@ Below is a list of currently supported actions. Requests with invalid actions or ] ``` -* **browse** +* **guiBrowse** - Functionally identical to **guiBrowse**, but accesses the database without using the GUI for increased performance. + Invokes the card browser and searches for a given query. Returns an array of identifiers of the cards that were found. *Sample request*: ``` { - "action": "browse", + "action": "guiBrowse", "params": { "query": "deck:current" }