This commit is contained in:
Alex Yatskov 2018-05-07 14:18:20 -07:00
parent 7a0b4e656d
commit 409245c41b
2 changed files with 57 additions and 24 deletions

View File

@ -376,6 +376,14 @@ class AnkiConnect:
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):
media = self.collection().media
if media is None:
@ -491,11 +499,9 @@ class AnkiConnect:
@api()
def getDecks(self, cards):
decks = {}
collection = self.collection()
for card in cards:
did = collection.db.scalar('select did from cards where id = ?', card)
deck = collection.decks.get(did)['name']
did = self.database().scalar('select did from cards where id=?', card)
deck = self.decks().get(did)['name']
if deck in decks:
decks[deck].append(card)
else:
@ -506,15 +512,18 @@ class AnkiConnect:
@api()
def createDeck(self, deck):
try:
self.startEditing()
deckId = self.collection().decks.id(deck)
did = self.decks().id(deck)
finally:
self.stopEditing()
return deckId
return did
@api()
def changeDeck(self, cards, deck):
try:
self.startEditing()
did = self.collection().decks.id(deck)
@ -527,16 +536,20 @@ class AnkiConnect:
self.collection().sched.remFromDyn(cards)
# 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)
finally:
self.stopEditing()
@api()
def deleteDecks(self, decks, cardsToo=False):
try:
self.startEditing()
decks = filter(lambda d: d in self.deckNames(), decks)
for deck in decks:
did = self.collection().decks.id(deck)
self.collection().decks.rem(did, cardsToo)
did = self.decks().id(deck)
self.decks().rem(did, cardsToo)
finally:
self.stopEditing()

View File

@ -17,5 +17,25 @@ class TestDeckNamesAndIds(unittest.TestCase):
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__':
unittest.main()