From bda22a20a3d98b27cf63f2f46eda67cb44e2cd09 Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 23 Apr 2020 19:39:11 -0400 Subject: [PATCH] Add duplicateScope option for addNote --- README.md | 7 +++++-- plugin/__init__.py | 9 ++++++--- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 5484479..c69240a 100644 --- a/README.md +++ b/README.md @@ -980,7 +980,9 @@ guarantee that your application continues to function properly in the future. downloaded files with an MD5 hash that matches the provided value. This is useful for avoiding the saving of error pages and stub files. The `fields` member is a list of fields that should play audio when the card is displayed in Anki. The `allowDuplicate` member inside `options` group can be set to true to enable adding duplicate cards. - Normally duplicate cards can not be added and trigger exception. + Normally duplicate cards can not be added and trigger exception. The `duplicateScope` member inside `options` can be + used to specify the scope for which duplicates are checked. A value of `"deck"` will only check for duplicates in the + target deck; any other value will check the entire collection. *Sample request*: ```json @@ -996,7 +998,8 @@ guarantee that your application continues to function properly in the future. "Back": "back content" }, "options": { - "allowDuplicate": false + "allowDuplicate": false, + "duplicateScope": "deck" }, "tags": [ "yomichan" diff --git a/plugin/__init__.py b/plugin/__init__.py index e00946f..db241aa 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -204,13 +204,16 @@ class AnkiConnect: ankiNote[name] = value allowDuplicate = False + duplicateScope = None if 'options' in note: if 'allowDuplicate' in note['options']: allowDuplicate = note['options']['allowDuplicate'] if type(allowDuplicate) is not bool: raise Exception('option parameter \'allowDuplicate\' must be boolean') + if 'duplicateScope' in note['options']: + duplicateScope = note['options']['duplicateScope'] - duplicateOrEmpty = self.isNoteDuplicateOrEmptyInDeck(ankiNote, deck) + duplicateOrEmpty = self.isNoteDuplicateOrEmptyInScope(ankiNote, deck, duplicateScope) if duplicateOrEmpty == 1: raise Exception('cannot create note because it is empty') elif duplicateOrEmpty == 2: @@ -223,10 +226,10 @@ class AnkiConnect: else: raise Exception('cannot create note for unknown reason') - def isNoteDuplicateOrEmptyInDeck(self, note, deck): + def isNoteDuplicateOrEmptyInScope(self, note, deck, duplicateScope): "1 if first is empty; 2 if first is a duplicate, False otherwise." result = note.dupeOrEmpty() - if result != 2: + if result != 2 or duplicateScope != 'deck': return result # dupeOrEmpty returns if a note is a global duplicate