Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _getOriginal(note):
csum = fieldChecksum(note.fields[0])
original_nid = mw.col.db.first("select id from notes where csum == {} and id != {} and mid == {}".format(
csum, note.id, note.mid))[0]
return mw.col.getNote(original_nid)
def updateNoteFields(self, note):
# showInfo("{}".format(note['id']))
ankiNote = aqt.mw.col.getNote(note['id'])
# showInfo("{}".format(ankiNote))
if ankiNote is None:
raise Exception('note was not found: {}'.format(note['id']))
for name, value in note['fields'].items():
if name in ankiNote:
ankiNote[name] = value
ankiNote.flush()
def words_for_query(query):
notes = [mw.col.getNote(id_) for id_ in mw.col.findNotes(query)]
rv = set()
for note in notes:
rv.update(f for f in note.fields if is_chinese_word(f))
return rv
def _addPrioFields(self):
model = mw.col.models.byName(self.settings['modelName'])
if self.settings['prioField'] in getFieldNames(
self.settings['modelName']
):
return
field = mw.col.models.newField(self.settings['prioField'])
mw.col.models.addField(model, field)
for (nid,) in mw.col.db.execute(
'SELECT id FROM notes WHERE mid = ?', model['id']
):
note = mw.col.getNote(nid)
setField(
note, self.settings['prioField'], self.settings['prioDefault']
)
note.flush()
showInfo(
'A <b><i>Priority</i></b> field has been added to your IR notes. '
'Valid priority values are integers 1-10, where 10 represents '
def doOnSelection( b, preF, perF, postF, progLabel ):
st = preF( b )
if not st: return
nids = b.selectedNotes()
mw.progress.start( label=progLabel, max=len( nids ) )
for i,nid in enumerate( nids ):
mw.progress.update( value=i )
n = mw.col.getNote( nid )
st = perF( st, n )
mw.progress.finish()
st = postF( st )
mw.col.updateFieldCache( nids )
if not st or st.get( '__reset', True ):
mw.reset()
def _getCardInfo(self, did):
cardInfo = []
for cid, nid in mw.col.db.execute(
'select id, nid from cards where did = ?', did
):
note = mw.col.getNote(nid)
if note.model()['name'] == self.settings['modelName']:
if self.settings['prioEnabled']:
prio = note[self.settings['prioField']]
else:
prio = None
cardInfo.append(
{
'id': cid,
'nid': nid,
'title': note[self.settings['titleField']],
'priority': prio,
}
)
return cardInfo
def bulk_fill_silhouette():
prompt = PROMPT_TEMPLATE.format(
field_names='<i>silhouette</i>', extra_info=''
)
if not askUser(prompt):
return
d_has_fields = 0
d_success = 0
note_ids = Finder(mw.col).findNotes('deck:current')
mw.progress.start(immediate=True, min=0, max=len(note_ids))
for i, nid in enumerate(note_ids):
note = mw.col.getNote(nid)
copy = dict(note)
if has_any_field(config['fields']['silhouette'], copy):
d_has_fields += 1
msg = '''
<b>Processing:</b> %(hanzi)s<br>
<b>Updated:</b> %(filled)d''' % {
'hanzi': get_hanzi(copy),
'filled': d_success,
}
mw.progress.update(label=msg, value=i)
hanzi = get_first(config['fields']['hanzi'], copy)
fill_silhouette(hanzi, copy)
d_success = save_note(note, copy)
msg = '''
<b>Update complete!</b> %(hanzi)s<br>
fields = config.get_fields(
['pinyin', 'pinyinTaiwan', 'cantonese', 'bopomofo']
)
if not askUser(prompt):
return
d_has_fields = 0
d_added_pinyin = 0
d_updated = 0
note_ids = Finder(mw.col).findNotes('deck:current')
mw.progress.start(immediate=True, min=0, max=len(note_ids))
for i, nid in enumerate(note_ids):
note = mw.col.getNote(nid)
copy = dict(note)
if has_any_field(copy, fields) and has_any_field(
config['fields']['hanzi'], copy
):
d_has_fields += 1
msg = '''
<b>Processing:</b> %(hanzi)s<br>
<b>Filled pinyin:</b> %(pinyin)d notes<br>
<b>Updated: </b>%(updated)d fields''' % {
'hanzi': get_hanzi(copy),
'pinyin': d_added_pinyin,
'updated': d_updated,
}
mw.progress.update(label=msg, value=i)
def regenerate_all():
# Find the models that have the right name and fields; faster than
# checking every note
if not askUser("Do you want to regenerate all kanji diagrams? "
'This may take some time and will overwrite the '
'destination Diagram fields.'):
return
models = [m for m in mw.col.models.all() if modelIsCorrectType(m)]
# Find the notes in those models and give them kanji
for model in models:
for nid in mw.col.models.nids(model):
addKanji(mw.col.getNote(nid))
showInfo("Done regenerating colorized kanji diagrams!")
"""
if not (front and back):
return
frontText, backText = "<br>".join(front), "<br>".join(back)
logger.debug('Importing front text: \n{}'.format(frontText))
logger.debug('Importing back text: \n{}'.format(backText))
# handle special ascii characters
frontText = frontText.decode('utf-8')
backText = backText.decode('utf-8')
if currentID:
newID = None
try:
note = mw.col.getNote(currentID)
newID = modifyNote(note, frontText, backText, tag)
except:
newID = addNote(frontText, backText, tag, model, deck, currentID)
if newID:
# Overwrite in case format was off
toWrite[-2] = ("\n".format(currentID))
else:
newID = addNote(frontText, backText, tag, model, deck)
if newID:
toWrite.insert(len(toWrite) - 1, "\n".format(newID))
tempFile.writelines(toWrite)
if newID: