Merge pull request #24 from techdavid/aresuspended-and-aredue

Add areDue function and change isSuspended to areSuspended
This commit is contained in:
Alex Yatskov 2017-08-12 12:39:25 -07:00 committed by GitHub
commit e50f2af06b
2 changed files with 75 additions and 19 deletions

View File

@ -24,6 +24,7 @@ import os.path
import select import select
import socket import socket
import sys import sys
from time import time
# #
@ -401,18 +402,47 @@ class AnkiBridge:
return False return False
def isSuspended(self, card): def areSuspended(self, cards):
card = self.collection().getCard(card) suspended = []
if card.queue == -1: for card in cards:
return True card = self.collection().getCard(card)
else: if card.queue == -1:
return False suspended.append(True)
else:
suspended.append(False)
return suspended
def areDue(self, cards):
due = []
for card in cards:
if self.findCards('cid:%s is:new' % card):
due.append(True)
continue
date, ivl = self.collection().db.all('select id/1000.0, ivl from revlog where cid = ?', card)[-1]
if (ivl >= -1200):
if self.findCards('cid:%s is:due' % card):
due.append(True)
else:
due.append(False)
else:
if date - ivl <= time():
due.append(True)
else:
due.append(False)
return due
def getIntervals(self, cards, complete=False): def getIntervals(self, cards, complete=False):
intervals = [] intervals = []
for card in cards: for card in cards:
interval = self.window().col.db.list('select ivl from revlog where cid = ?', card) if self.findCards('cid:%s is:new' % card):
intervals.append(0)
continue
interval = self.collection().db.list('select ivl from revlog where cid = ?', card)
if not complete: if not complete:
interval = interval[-1] interval = interval[-1]
intervals.append(interval) intervals.append(interval)
@ -510,22 +540,22 @@ class AnkiBridge:
def changeDeck(self, cards, deck): def changeDeck(self, cards, deck):
self.startEditing() self.startEditing()
did = self.window().col.decks.id(deck) did = self.collection().decks.id(deck)
mod = anki.utils.intTime() mod = anki.utils.intTime()
usn = self.window().col.usn() usn = self.collection().usn()
# normal cards # normal cards
scids = anki.utils.ids2str(cards) scids = anki.utils.ids2str(cards)
# remove any cards from filtered deck first # remove any cards from filtered deck first
self.window().col.sched.remFromDyn(cards) self.collection().sched.remFromDyn(cards)
# then move into new deck # then move into new deck
self.window().col.db.execute('update cards set usn=?, mod=?, did=? where id in ' + scids, usn, mod, did) self.collection().db.execute('update cards set usn=?, mod=?, did=? where id in ' + scids, usn, mod, did)
self.stopEditing() self.stopEditing()
def cardsToNotes(self, cards): def cardsToNotes(self, cards):
return self.window().col.db.list('select distinct nid from cards where id in ' + anki.utils.ids2str(cards)) return self.collection().db.list('select distinct nid from cards where id in ' + anki.utils.ids2str(cards))
def guiBrowse(self, query=None): def guiBrowse(self, query=None):
@ -755,8 +785,13 @@ class AnkiConnect:
@webApi @webApi
def isSuspended(self, card): def areSuspended(self, cards):
return self.anki.isSuspended(card) return self.anki.areSuspended(cards)
@webApi
def areDue(self, cards):
return self.anki.areDue(cards)
@webApi @webApi

View File

@ -385,23 +385,44 @@ Below is a list of currently supported actions. Requests with invalid actions or
true true
``` ```
* **isSuspended** * **areSuspended**
Returns `true` if the given card is suspended or `false` otherwise. Returns an array indicating whether each of the given cards is suspended (in the same order).
*Sample request*: *Sample request*:
``` ```
{ {
"action": "isSuspended", "action": "areSuspended",
"params": { "params": {
"card": 1483959291685 "cards": [1483959291685, 1483959293217]
} }
} }
``` ```
*Sample response*: *Sample response*:
``` ```
false [false, true]
```
* **areDue**
Returns an array indicating whether each of the given cards is due (in the same order). Note: cards in the learning
queue with a large interval (over 20 minutes) are treated as not due until the time of their interval has passed, to
match the way Anki treats them when reviewing.
*Sample request*:
```
{
"action": "areDue",
"params": {
"cards": [1483959291685, 1483959293217]
}
}
```
*Sample response*:
```
[false, true]
``` ```
* **getIntervals** * **getIntervals**