Fix api method deleteDecks

The method was failing due to Anki API changes.

Also, make it mandatory to call the method with `cardsToo=True`,
since deleting decks on Anki >= 2.1.28 without cards is
no longer supported, and the deprecated `decks.rem()` method
on Anki >= 2.1.45 ignores keyword arguments.
This commit is contained in:
oakkitten 2022-03-30 19:32:39 +01:00
parent 0fdc93abc6
commit baed642489
2 changed files with 13 additions and 3 deletions

View File

@ -745,8 +745,8 @@ corresponding to when the API was available for use.
* **deleteDecks**
Deletes decks with the given names. If `cardsToo` is `true` (defaults to `false` if unspecified), the cards within
the deleted decks will also be deleted; otherwise they will be moved to the default deck.
Deletes decks with the given names.
The argument `cardsToo` *must* be specified and set to `true`.
*Sample request*:
```json

View File

@ -531,12 +531,22 @@ class AnkiConnect:
@util.api()
def deleteDecks(self, decks, cardsToo=False):
if not cardsToo:
# since f592672fa952260655881a75a2e3c921b2e23857 (2.1.28)
# (see anki$ git log "-Gassert cardsToo")
# you can't delete decks without deleting cards as well.
# however, since 62c23c6816adf912776b9378c008a52bb50b2e8d (2.1.45)
# passing cardsToo to `rem` (long deprecated) won't raise an error!
# this is dangerous, so let's raise our own exception
if self._anki21_version >= 28:
raise Exception("Since Anki 2.1.28 it's not possible "
"to delete decks without deleting cards as well")
try:
self.startEditing()
decks = filter(lambda d: d in self.deckNames(), decks)
for deck in decks:
did = self.decks().id(deck)
self.decks().rem(did, cardsToo)
self.decks().rem(did, cardsToo=cardsToo)
finally:
self.stopEditing()