Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def processNote(self, deckName):
self.window.debug.appendPlainText("Processing Notes")
deck = mw.col.decks.get(mw.col.decks.id(deckName))
# create custom model
model = self.addCustomModel(deckName, mw.col)
# assign custom model to new deck
mw.col.decks.select(deck["id"])
mw.col.decks.get(deck)["mid"] = model["id"]
mw.col.decks.save(deck)
# assign new deck to custom model
mw.col.models.setCurrent(model)
mw.col.models.current()["did"] = deck["id"]
mw.col.models.save(model)
# start creating notes
if self.new:
for term in self.new:
note = mw.col.newNote()
note['term'] = term['term']+"[sound:MG-"+term['term']+".mp3]"
note['definition'] = term['definition']
note['uk'] = term['uk']
note['us'] = term['us']
if term['phrases'][0]:
for index, phrase in enumerate(term['phrases']):
note['phrase' + str(index)] = phrase
note['phrase_explain' + str(index)] = term['phrases_explains'][index]
note['pplaceHolder' + str(index)] = "Tap To View"
if term['sentences'][0]:
for index, sentence in enumerate(term['sentences']):
def getOrCreateModel(modelName):
model = mw.col.models.byName(modelName)
if model:
if set([f['name'] for f in model['flds']]) == set(MODEL_FIELDS):
return model
else:
logger.warning('模版字段异常,自动删除重建')
mw.col.models.rem(model)
logger.info(f'创建新模版:{modelName}')
newModel = mw.col.models.new(modelName)
mw.col.models.add(newModel)
for field in MODEL_FIELDS:
mw.col.models.addField(newModel, mw.col.models.newField(field))
mw.col.models.update(newModel)
return newModel
def processNote(self, deckName):
self.window.debug.appendPlainText("Processing Notes")
deck = mw.col.decks.get(mw.col.decks.id(deckName))
# create custom model
model = self.addCustomModel(deckName, mw.col)
# assign custom model to new deck
mw.col.decks.select(deck["id"])
mw.col.decks.get(deck)["mid"] = model["id"]
mw.col.decks.save(deck)
# assign new deck to custom model
mw.col.models.setCurrent(model)
mw.col.models.current()["did"] = deck["id"]
mw.col.models.save(model)
# start creating notes
if self.new:
for term in self.new:
note = mw.col.newNote()
note['term'] = term['term']+"[sound:MG-"+term['term']+".mp3]"
note['definition'] = term['definition']
note['uk'] = term['uk']
note['us'] = term['us']
if term['phrases'][0]:
for index, phrase in enumerate(term['phrases']):
note['phrase' + str(index)] = phrase
note['phrase_explain' + str(index)] = term['phrases_explains'][index]
note['pplaceHolder' + str(index)] = "Tap To View"
def createDeck(self, name, terms):
"""create new Anki deck from downloaded data"""
#create new deck and custom model
deck = mw.col.decks.get(mw.col.decks.id(name))
model = addCustomModel(name, mw.col)
#assign custom model to new deck
mw.col.decks.select(deck["id"])
mw.col.decks.get(deck)["mid"] = model["id"]
mw.col.decks.save(deck)
#assign new deck to custom model
mw.col.models.setCurrent(model)
mw.col.models.current()["did"] = deck["id"]
mw.col.models.save(model)
for term in terms:
note = mw.col.newNote()
note["Front"] = term["term"]
note["Back"] = term["definition"]
mw.col.addNote(note)
mw.col.reset()
mw.reset()
def show_models(self):
'''
show choose note type window
'''
edit = QPushButton(anki.lang._("Manage"),
clicked=lambda: aqt.models.Models(mw, self))
ret = StudyDeck(mw, names=lambda: sorted(mw.col.models.allNames()),
accept=anki.lang._("Choose"), title=anki.lang._("Choose Note Type"),
help="_notes", parent=self, buttons=[edit],
cancel=True, geomKey="selectModel")
if ret.name:
model = mw.col.models.byName(ret.name)
self.models_button.setText(
u'%s [%s]' % (_('CHOOSE_NOTE_TYPES'), ret.name))
return model
def airtableImport(col, deck, modelName, table, view, app_key):
did = mw.col.decks.id(deck)
mw.col.decks.select(did)
model = mw.col.models.byName(modelName)
if not model:
model = mw.col.models.new(modelName)
mw.col.models.add(model)
template = mw.col.models.newTemplate("Default")
mw.col.models.addTemplate(model, template)
model['did'] = did
deck = mw.col.decks.get(did)
deck['mid'] = model['id']
mw.col.decks.save(deck)
airtable = AirtableImporter(mw.col, table, view, Config["key"], app_key)
airtable.updateModel(model)
airtable.initMapping()
airtable.run()
def getOrCreateModel(modelName):
model = mw.col.models.byName(modelName)
if model:
if set([f['name'] for f in model['flds']]) == set(MODEL_FIELDS):
return model
else:
logger.warning('模版字段异常,自动删除重建')
mw.col.models.rem(model)
logger.info(f'创建新模版:{modelName}')
newModel = mw.col.models.new(modelName)
mw.col.models.add(newModel)
for field in MODEL_FIELDS:
mw.col.models.addField(newModel, mw.col.models.newField(field))
mw.col.models.update(newModel)
return newModel
def createDeck(self, name, terms):
"""create new Anki deck from downloaded data"""
#create new deck and custom model
deck = mw.col.decks.get(mw.col.decks.id(name))
model = addCustomModel(name, mw.col)
#assign custom model to new deck
mw.col.decks.select(deck["id"])
mw.col.decks.get(deck)["mid"] = model["id"]
mw.col.decks.save(deck)
#assign new deck to custom model
mw.col.models.setCurrent(model)
mw.col.models.current()["did"] = deck["id"]
mw.col.models.save(model)
for term in terms:
note = mw.col.newNote()
note["Front"] = term["term"]
note["Back"] = term["definition"]
mw.col.addNote(note)
mw.col.reset()
mw.reset()
def create(self, text, settings):
currentCard = mw.reviewer.card
currentNote = currentCard.note()
model = mw.col.models.byName(settings['modelName'])
newNote = Note(mw.col, model)
newNote.tags = currentNote.tags
setField(newNote, settings['textField'], fixImages(text))
if settings['extractDeck']:
deck = mw.col.decks.byName(settings['extractDeck'])
if not deck:
showWarning(
'Destination deck no longer exists. '
'Please update your settings.'
)
return
did = deck['id']
else:
did = currentCard.did
def _handle_open(self, link):
m = re.search("open note:'(.+)' tmpl:'(.+)'", link)
model_id = m.group(1)
tmpl_ord = int(m.group(2))
model = mw.col.models.get(model_id)
tmpl = model['tmpls'][tmpl_ord]
d = QDialog(mw)
ed = editordialog.Dialog()
css_ed = CSSEditor()
css_ed.setText(model['css'])
ed.setupUi(mw, css_ed, d)
def on_change():
model['css'] = css_ed.toPlainText()
templateserver.update_stylesheet(model, model['css'])
def on_close():
mw.col.models.save(model)
self.diag.setVisible(True)
self._redraw()