wip
This commit is contained in:
parent
7a0b4e656d
commit
409245c41b
@ -376,6 +376,14 @@ class AnkiConnect:
|
|||||||
return scheduler
|
return scheduler
|
||||||
|
|
||||||
|
|
||||||
|
def database(self):
|
||||||
|
database = self.collection().db
|
||||||
|
if database is None:
|
||||||
|
raise Exception('database is not available')
|
||||||
|
else:
|
||||||
|
return database
|
||||||
|
|
||||||
|
|
||||||
def media(self):
|
def media(self):
|
||||||
media = self.collection().media
|
media = self.collection().media
|
||||||
if media is None:
|
if media is None:
|
||||||
@ -491,11 +499,9 @@ class AnkiConnect:
|
|||||||
@api()
|
@api()
|
||||||
def getDecks(self, cards):
|
def getDecks(self, cards):
|
||||||
decks = {}
|
decks = {}
|
||||||
collection = self.collection()
|
|
||||||
for card in cards:
|
for card in cards:
|
||||||
did = collection.db.scalar('select did from cards where id = ?', card)
|
did = self.database().scalar('select did from cards where id=?', card)
|
||||||
deck = collection.decks.get(did)['name']
|
deck = self.decks().get(did)['name']
|
||||||
|
|
||||||
if deck in decks:
|
if deck in decks:
|
||||||
decks[deck].append(card)
|
decks[deck].append(card)
|
||||||
else:
|
else:
|
||||||
@ -506,38 +512,45 @@ class AnkiConnect:
|
|||||||
|
|
||||||
@api()
|
@api()
|
||||||
def createDeck(self, deck):
|
def createDeck(self, deck):
|
||||||
self.startEditing()
|
try:
|
||||||
deckId = self.collection().decks.id(deck)
|
self.startEditing()
|
||||||
self.stopEditing()
|
did = self.decks().id(deck)
|
||||||
|
finally:
|
||||||
|
self.stopEditing()
|
||||||
|
|
||||||
return deckId
|
return did
|
||||||
|
|
||||||
|
|
||||||
@api()
|
@api()
|
||||||
def changeDeck(self, cards, deck):
|
def changeDeck(self, cards, deck):
|
||||||
self.startEditing()
|
try:
|
||||||
|
self.startEditing()
|
||||||
|
|
||||||
did = self.collection().decks.id(deck)
|
did = self.collection().decks.id(deck)
|
||||||
mod = anki.utils.intTime()
|
mod = anki.utils.intTime()
|
||||||
usn = self.collection().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.collection().sched.remFromDyn(cards)
|
self.collection().sched.remFromDyn(cards)
|
||||||
|
|
||||||
# then move into new deck
|
# then move into new deck
|
||||||
self.collection().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()
|
finally:
|
||||||
|
self.stopEditing()
|
||||||
|
|
||||||
|
|
||||||
@api()
|
@api()
|
||||||
def deleteDecks(self, decks, cardsToo=False):
|
def deleteDecks(self, decks, cardsToo=False):
|
||||||
self.startEditing()
|
try:
|
||||||
for deck in decks:
|
self.startEditing()
|
||||||
did = self.collection().decks.id(deck)
|
decks = filter(lambda d: d in self.deckNames(), decks)
|
||||||
self.collection().decks.rem(did, cardsToo)
|
for deck in decks:
|
||||||
self.stopEditing()
|
did = self.decks().id(deck)
|
||||||
|
self.decks().rem(did, cardsToo)
|
||||||
|
finally:
|
||||||
|
self.stopEditing()
|
||||||
|
|
||||||
|
|
||||||
@api()
|
@api()
|
||||||
|
@ -17,5 +17,25 @@ class TestDeckNamesAndIds(unittest.TestCase):
|
|||||||
self.assertEqual(result['Default'], 1)
|
self.assertEqual(result['Default'], 1)
|
||||||
|
|
||||||
|
|
||||||
|
class TestCreateDeck(unittest.TestCase):
|
||||||
|
def tearDown(self):
|
||||||
|
util.invoke('deleteDecks', {'decks': ['test']})
|
||||||
|
|
||||||
|
|
||||||
|
def runTest(self):
|
||||||
|
util.invoke('createDeck', {'deck': 'test'})
|
||||||
|
self.assertIn('test', util.invoke('deckNames'))
|
||||||
|
|
||||||
|
|
||||||
|
class TestDeleteDecks(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
util.invoke('createDeck', {'deck': 'test'})
|
||||||
|
|
||||||
|
|
||||||
|
def runTest(self):
|
||||||
|
util.invoke('deleteDecks', {'decks': ['test']})
|
||||||
|
self.assertNotIn('test', util.invoke('deckNames'))
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
unittest.main()
|
unittest.main()
|
||||||
|
Loading…
Reference in New Issue
Block a user