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 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,15 +512,18 @@ class AnkiConnect:
@api() @api()
def createDeck(self, deck): def createDeck(self, deck):
try:
self.startEditing() self.startEditing()
deckId = self.collection().decks.id(deck) did = self.decks().id(deck)
finally:
self.stopEditing() self.stopEditing()
return deckId return did
@api() @api()
def changeDeck(self, cards, deck): def changeDeck(self, cards, deck):
try:
self.startEditing() self.startEditing()
did = self.collection().decks.id(deck) did = self.collection().decks.id(deck)
@ -527,16 +536,20 @@ class AnkiConnect:
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)
finally:
self.stopEditing() self.stopEditing()
@api() @api()
def deleteDecks(self, decks, cardsToo=False): def deleteDecks(self, decks, cardsToo=False):
try:
self.startEditing() self.startEditing()
decks = filter(lambda d: d in self.deckNames(), decks)
for deck in decks: for deck in decks:
did = self.collection().decks.id(deck) did = self.decks().id(deck)
self.collection().decks.rem(did, cardsToo) self.decks().rem(did, cardsToo)
finally:
self.stopEditing() self.stopEditing()

View File

@ -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()