From baed642489d8affa50b1a16ef28a53c44b5a66fa Mon Sep 17 00:00:00 2001 From: oakkitten Date: Wed, 30 Mar 2022 19:32:39 +0100 Subject: [PATCH] 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. --- README.md | 4 ++-- plugin/__init__.py | 12 +++++++++++- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a7966d4..f78b943 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/plugin/__init__.py b/plugin/__init__.py index 7f4ddf5..5234d1e 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -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()