Merge pull request #157 from toasted-nutbread/duplicate-scope

Add duplicateScope option for addNote
This commit is contained in:
Alex Yatskov 2020-04-23 20:02:30 -07:00 committed by GitHub
commit 9be1ea30f5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 5 deletions

View File

@ -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"

View File

@ -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