Handle Anki intermediate states
This commit is contained in:
parent
30d243790f
commit
a623e97ebc
@ -186,13 +186,16 @@ class AjaxServer:
|
|||||||
|
|
||||||
class AnkiBridge:
|
class AnkiBridge:
|
||||||
def addNote(self, deckName, modelName, fields, tags=[]):
|
def addNote(self, deckName, modelName, fields, tags=[]):
|
||||||
|
collection = self.collection()
|
||||||
|
if collection is None:
|
||||||
|
return
|
||||||
|
|
||||||
note = self.createNote(deckName, modelName, fields, tags)
|
note = self.createNote(deckName, modelName, fields, tags)
|
||||||
if note is None:
|
if note is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
self.startEditing()
|
self.startEditing()
|
||||||
|
|
||||||
collection = self.collection()
|
|
||||||
collection.addNote(note)
|
collection.addNote(note)
|
||||||
collection.autosave()
|
collection.autosave()
|
||||||
|
|
||||||
@ -204,15 +207,19 @@ class AnkiBridge:
|
|||||||
|
|
||||||
|
|
||||||
def createNote(self, deckName, modelName, fields, tags=[]):
|
def createNote(self, deckName, modelName, fields, tags=[]):
|
||||||
model = self.models().byName(modelName)
|
collection = self.collection()
|
||||||
|
if collection is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
model = collection.models.byName(modelName)
|
||||||
if model is None:
|
if model is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
deck = self.decks().byName(deckName)
|
deck = collection.decks.byName(deckName)
|
||||||
if deck is None:
|
if deck is None:
|
||||||
return
|
return
|
||||||
|
|
||||||
note = anki.notes.Note(self.collection(), model)
|
note = anki.notes.Note(collection, model)
|
||||||
note.model()['did'] = deck['id']
|
note.model()['did'] = deck['id']
|
||||||
note.tags = tags
|
note.tags = tags
|
||||||
|
|
||||||
@ -235,7 +242,7 @@ class AnkiBridge:
|
|||||||
|
|
||||||
|
|
||||||
def stopEditing(self):
|
def stopEditing(self):
|
||||||
if self.collection():
|
if self.collection() is not None:
|
||||||
self.window().maybeReset()
|
self.window().maybeReset()
|
||||||
|
|
||||||
|
|
||||||
@ -247,26 +254,26 @@ class AnkiBridge:
|
|||||||
return self.window().col
|
return self.window().col
|
||||||
|
|
||||||
|
|
||||||
def models(self):
|
|
||||||
return self.collection().models
|
|
||||||
|
|
||||||
|
|
||||||
def modelNames(self):
|
def modelNames(self):
|
||||||
return self.models().allNames()
|
collection = self.collection()
|
||||||
|
if collection is not None:
|
||||||
|
return collection.models.allNames()
|
||||||
|
|
||||||
|
|
||||||
def modelFieldNames(self, modelName):
|
def modelFieldNames(self, modelName):
|
||||||
model = self.models().byName(modelName)
|
collection = self.collection()
|
||||||
|
if collection is None:
|
||||||
|
return
|
||||||
|
|
||||||
|
model = collection.models.byName(modelName)
|
||||||
if model is not None:
|
if model is not None:
|
||||||
return [field['name'] for field in model['flds']]
|
return [field['name'] for field in model['flds']]
|
||||||
|
|
||||||
|
|
||||||
def decks(self):
|
|
||||||
return self.collection().decks
|
|
||||||
|
|
||||||
|
|
||||||
def deckNames(self):
|
def deckNames(self):
|
||||||
return self.decks().allNames()
|
collection = self.collection()
|
||||||
|
if collection is not None:
|
||||||
|
return collection.decks.allNames()
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user