Adding string method for AnkiNoteParams class. Expanded error catching for addNote.

addNotes can now handle when one card of an array throws and error and continue adding cards.
This commit is contained in:
c-okelly 2018-03-11 11:55:47 +00:00
parent 5d23ef1385
commit d79dd1408b

View File

@ -338,6 +338,8 @@ class AnkiNoteParams:
type(self.tags) == list and verifyStringList(self.tags) type(self.tags) == list and verifyStringList(self.tags)
) )
def __str__(self):
return "DeckName: " + self.deckName + ". ModelName: " + self.modelName + ". Fields: " + str(self.fields) + ". Tags: " + str(self.tags) + "."
# #
# AnkiBridge # AnkiBridge
@ -374,7 +376,7 @@ class AnkiBridge:
note = self.createNote(params) note = self.createNote(params)
if note is None: if note is None:
raise Exception("Failed to create note from params" + str(params)) raise Exception("Failed to create note from params: " + str(params))
if params.audio is not None and len(params.audio.fields) > 0: if params.audio is not None and len(params.audio.fields) > 0:
data = download(params.audio.url) data = download(params.audio.url)
@ -409,11 +411,11 @@ class AnkiBridge:
model = collection.models.byName(params.modelName) model = collection.models.byName(params.modelName)
if model is None: if model is None:
raise Exception("Model was not found for model: " + str(params.modelName)) raise Exception("Model was not found for model: " + params.modelName)
deck = collection.decks.byName(params.deckName) deck = collection.decks.byName(params.deckName)
if deck is None: if deck is None:
raise Exception("Deck was not found for deck: " + str(params.deckName)) raise Exception("Deck was not found for deck: " + params.deckName)
note = anki.notes.Note(collection, model) note = anki.notes.Note(collection, model)
note.model()['did'] = deck['id'] note.model()['did'] = deck['id']
@ -423,7 +425,13 @@ class AnkiBridge:
if name in note: if name in note:
note[name] = value note[name] = value
if not note.dupeOrEmpty(): # Returns 1 if empty. 2 if duplicate. Otherwise returns False
duplicateOrEmpty = note.dupeOrEmpty()
if duplicateOrEmpty == 1:
raise Exception("Note was empty. Param were: " + str(params))
elif duplicateOrEmpty == 2:
raise Exception("Note is duplicate of existing note. Params were: " + str(params))
elif duplicateOrEmpty == False:
return note return note
def updateNoteFields(self, params): def updateNoteFields(self, params):
@ -1110,11 +1118,14 @@ class AnkiConnect:
def addNotes(self, notes): def addNotes(self, notes):
results = [] results = []
for note in notes: for note in notes:
params = AnkiNoteParams(note) try:
if params.validate(): params = AnkiNoteParams(note)
results.append(self.anki.addNote(params)) if params.validate():
else: results.append(self.anki.addNote(params))
results.append(None) else:
results.append(None)
except Exception as e:
results.append(str(e))
return results return results