From bbf271c5ac611705accbf4ec9dbaa3382ba439ec Mon Sep 17 00:00:00 2001 From: Eloy Robillard Date: Fri, 23 Feb 2024 06:39:35 +0100 Subject: [PATCH] Return reason note cannot be added in new api endpoint --- README.md | 64 ++++++++++++++++++++++++++++++++++++++++++++++ plugin/__init__.py | 19 ++++++++++++++ 2 files changed, 83 insertions(+) diff --git a/README.md b/README.md index 4a71179..3044103 100644 --- a/README.md +++ b/README.md @@ -3698,6 +3698,70 @@ Search parameters are passed to Anki, check the docs for more information: https ``` +#### `canAddNotesWithErrorDetail` + +* Accepts an array of objects which define parameters for candidate notes (see `addNote`) and returns an array of + objects with fields `canAdd` and `error`. + + * `canAdd` indicates whether or not the parameters at the corresponding index could be used to create a new note. + * `error` contains an explanation of why a note cannot be added. + +
+ Sample request: + + ```json + { + "action": "canAddNotesWithErrorDetail", + "version": 6, + "params": { + "notes": [ + { + "deckName": "Default", + "modelName": "Basic", + "fields": { + "Front": "front content", + "Back": "back content" + }, + "tags": [ + "yomichan" + ] + }, + { + "deckName": "Default", + "modelName": "Basic", + "fields": { + "Front": "front content 2", + "Back": "back content 2" + }, + "tags": [ + "yomichan" + ] + } + ] + } + } + ``` +
+ +
+ Sample result: + + ```json + { + "result": [ + { + canAdd: false, + error: 'cannot create note because it is a duplicate' + }, + { + canAdd: true + } + ], + "error": null + } + ``` +
+ #### `updateNoteFields` * Modify the fields of an existing note. You can also include audio, video, or picture files which will be added to the note with an diff --git a/plugin/__init__.py b/plugin/__init__.py index dd8fd6e..4c69642 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -788,6 +788,17 @@ class AnkiConnect: except: return False + @util.api() + def canAddNoteWithErrorDetail(self, note): + try: + return { + 'canAdd': bool(self.createNote(note)) + } + except Exception as e: + return { + 'canAdd': False, + 'error': str(e) + } @util.api() def updateNoteFields(self, note): @@ -1959,6 +1970,14 @@ class AnkiConnect: return results + @util.api() + def canAddNotesWithErrorDetail(self, notes): + results = [] + for note in notes: + results.append(self.canAddNoteWithErrorDetail(note)) + + return results + @util.api() def exportPackage(self, deck, path, includeSched=False):