From 311cb7e19282734f88d1cfce56730dcb93d901be Mon Sep 17 00:00:00 2001 From: Venkata Ramana Date: Sat, 2 Jan 2021 22:35:35 +0530 Subject: [PATCH] replaceTags and clearUnusedTags are added (#219) * clearUnusedTags and replaceTags are added * added clearUnusedTags and replaceTags * added clearUnusedTags and replaceTags * removed whitespaces and indentations * removed white spaces and indentations --- actions/notes.md | 69 ++++++++++++++++++++++++++++++++++++++++++++++ plugin/__init__.py | 37 ++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 1 deletion(-) diff --git a/actions/notes.md b/actions/notes.md index 639e972..83cc514 100644 --- a/actions/notes.md +++ b/actions/notes.md @@ -284,6 +284,75 @@ } ``` +* **clearUnusedTags** + + Clears all the unused tags in the notes for the current user. + + *Sample request*: + ```json + { + "action": "clearUnusedTags", + "version": 6 + } + ``` + + *Sample result*: + ```json + { + "result": null, + "error": null + } + ``` + +* **replaceTags** + + Replace tags in notes by note ID. + + *Sample request*: + ```json + { + "action": "replaceTags", + "version": 6, + "params": { + "notes": [1483959289817, 1483959291695], + "tag_to_replace": "european-languages", + "replace_with_tag": "french-languages" + } + } + ``` + + *Sample result*: + ```json + { + "result": null, + "error": null + } + ``` + +* **replaceTagsInAllNotes** + + Replace tags in all the notes for the current user. + + *Sample request*: + ```json + { + "action": "replaceTagsInAllCards", + "version": 6, + "params": { + "tag_to_replace": "european-languages", + "replace_with_tag": "french-languages" + } + } + ``` + + *Sample result*: + ```json + { + "result": null, + "error": null + } + ``` + * **findNotes** Returns an array of note IDs for a given query. Same query syntax as `guiBrowse`. diff --git a/plugin/__init__.py b/plugin/__init__.py index 7ccb69e..050ebf8 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -614,6 +614,41 @@ class AnkiConnect: @util.api() def getTags(self): return self.collection().tags.all() + + @util.api() + def clearUnusedTags(self): + self.collection().tags.registerNotes() + + @util.api() + def replaceTags(self, notes, tag_to_replace, replace_with_tag): + if self.collection() is not None: + self.window().progress.start() + for nid in notes: + note = self.collection().getNote(nid) + if note.hasTag(tag_to_replace): + note.delTag(tag_to_replace) + note.addtag(replace_with_tag) + note.flush() + self.window().requireReset() + self.window().progress.finish() + self.window().reset() + + @util.api() + def replaceTagsInAllNotes(self, tag_to_replace, replace_with_tag): + collection = self.collection() + if collection is not None: + nids = collection.db.list('select id from notes') + self.window().progress.start() + for nid in nids: + note = collection.getNote(nid) + if note.hasTag(tag_to_replace): + note.delTag(tag_to_replace) + note.addtag(replace_with_tag) + note.flush() + self.window().requireReset() + self.window().progress.finish() + self.window().reset() + return False @util.api() def setEaseFactors(self, cards, easeFactors): @@ -1392,4 +1427,4 @@ class AnkiConnect: # Entry # -ac = AnkiConnect() +ac = AnkiConnect() \ No newline at end of file