remove try-except in updateNoteModel

Signed-off-by: Tategoto Azarasi <2724167997@qq.com>
This commit is contained in:
Tategoto Azarasi 2024-05-10 12:16:12 +08:00 committed by Alex Yatskov
parent b77327fb00
commit 1c428c8627

View File

@ -842,57 +842,53 @@ class AnkiConnect:
:param note: A dictionary containing note details, including 'id', 'modelName', 'fields', and 'tags'. :param note: A dictionary containing note details, including 'id', 'modelName', 'fields', and 'tags'.
""" """
try: # Extract and validate the note ID
# Extract and validate the note ID note_id = note.get('id')
note_id = note.get('id') if not note_id:
if not note_id: raise ValueError("Note ID is required")
raise ValueError("Note ID is required")
# Extract and validate the new model name # Extract and validate the new model name
new_model_name = note.get('modelName') new_model_name = note.get('modelName')
if not new_model_name: if not new_model_name:
raise ValueError("Model name is required") raise ValueError("Model name is required")
# Extract and validate the new fields # Extract and validate the new fields
new_fields = note.get('fields') new_fields = note.get('fields')
if not new_fields or not isinstance(new_fields, dict): if not new_fields or not isinstance(new_fields, dict):
raise ValueError("Fields must be provided as a dictionary") raise ValueError("Fields must be provided as a dictionary")
# Extract the new tags # Extract the new tags
new_tags = note.get('tags', []) new_tags = note.get('tags', [])
# Get the current note from the collection # Get the current note from the collection
anki_note = self.getNote(note_id) anki_note = self.getNote(note_id)
# Get the new model from the collection # Get the new model from the collection
collection = self.collection() collection = self.collection()
new_model = collection.models.by_name(new_model_name) new_model = collection.models.by_name(new_model_name)
if not new_model: if not new_model:
raise ValueError(f"Model '{new_model_name}' not found") raise ValueError(f"Model '{new_model_name}' not found")
# Update the note's model # Update the note's model
anki_note.mid = new_model['id'] anki_note.mid = new_model['id']
anki_note._fmap = collection.models.field_map(new_model) anki_note._fmap = collection.models.field_map(new_model)
anki_note.fields = [''] * len(new_model['flds']) anki_note.fields = [''] * len(new_model['flds'])
# Update the fields with new values # Update the fields with new values
for name, value in new_fields.items(): for name, value in new_fields.items():
for anki_name in anki_note.keys(): for anki_name in anki_note.keys():
if name.lower() == anki_name.lower(): if name.lower() == anki_name.lower():
anki_note[anki_name] = value anki_note[anki_name] = value
break break
# Update the tags # Update the tags
anki_note.tags = new_tags anki_note.tags = new_tags
# Flush changes to ensure they are saved # Flush changes to ensure they are saved
anki_note.flush() anki_note.flush()
# Save changes to the collection # Save changes to the collection
collection.autosave() collection.autosave()
except Exception as e:
raise Exception(f"Failed to update note model: {e}")
@util.api() @util.api()
def updateNoteTags(self, note, tags): def updateNoteTags(self, note, tags):