Merge pull request #25 from techdavid/add-getdecks-and-deletedecks

Add getDecks and deleteDecks functions
This commit is contained in:
Alex Yatskov 2017-08-13 14:50:05 -07:00 committed by GitHub
commit 792a5c0134
2 changed files with 77 additions and 0 deletions

View File

@ -537,6 +537,20 @@ class AnkiBridge:
return []
def getDecks(self, cards):
decks = {}
for card in cards:
did = self.collection().db.scalar('select did from cards where id = ?', card)
deck = self.collection().decks.get(did)['name']
if deck in decks:
decks[deck].append(card)
else:
decks[deck] = [card]
return decks
def changeDeck(self, cards, deck):
self.startEditing()
@ -554,6 +568,14 @@ class AnkiBridge:
self.stopEditing()
def deleteDecks(self, decks, cardsToo=False):
self.startEditing()
for deck in decks:
id = self.collection().decks.id(deck)
self.collection().decks.rem(id, cardsToo)
self.stopEditing()
def cardsToNotes(self, cards):
return self.collection().db.list('select distinct nid from cards where id in ' + anki.utils.ids2str(cards))
@ -837,11 +859,21 @@ class AnkiConnect:
return self.anki.findCards(query)
@webApi
def getDecks(self, cards):
return self.anki.getDecks(cards)
@webApi
def changeDeck(self, cards, deck):
return self.anki.changeDeck(cards, deck)
@webApi
def deleteDecks(self, decks, cardsToo=False):
return self.anki.deleteDecks(decks, cardsToo)
@webApi
def cardsToNotes(self, cards):
return self.anki.cardsToNotes(cards)

View File

@ -511,6 +511,30 @@ Below is a list of currently supported actions. Requests with invalid actions or
]
```
* **getDecks**
Accepts an array of card IDs and returns an object with each deck name as a key, and its value an array of the given
cards which belong to it.
*Sample request*:
```
{
"action": "getDecks",
"params": {
"cards": [1502298036657, 1502298033753, 1502032366472]
}
}
```
*Sample response*:
```
{
"Default": [1502032366472],
"Japanese::JLPT N3": [1502298036657, 1502298033753]
}
```
* **changeDeck**
Moves cards with the given IDs to a different deck, creating the deck if it doesn't exist yet.
@ -531,6 +555,27 @@ Below is a list of currently supported actions. Requests with invalid actions or
null
```
* **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.
*Sample request*:
```
{
"action": "deleteDecks",
"params": {
"decks": ["Japanese::JLPT N5", "Easy Spanish"],
"cardsToo": true
}
}
```
*Sample response*:
```
null
```
* **cardsToNotes**
Returns an (unordered) array of note IDs for the given card IDs. For cards with the same note, the ID is only