From 1c428c8627117b5e61562524aff6304c14bc26b0 Mon Sep 17 00:00:00 2001 From: Tategoto Azarasi Date: Fri, 10 May 2024 12:16:12 +0800 Subject: [PATCH] remove try-except in updateNoteModel Signed-off-by: Tategoto Azarasi <2724167997@qq.com> --- plugin/__init__.py | 78 ++++++++++++++++++++++------------------------ 1 file changed, 37 insertions(+), 41 deletions(-) diff --git a/plugin/__init__.py b/plugin/__init__.py index 2fdb138..7dc3dd7 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -842,57 +842,53 @@ class AnkiConnect: :param note: A dictionary containing note details, including 'id', 'modelName', 'fields', and 'tags'. """ - try: - # Extract and validate the note ID - note_id = note.get('id') - if not note_id: - raise ValueError("Note ID is required") + # Extract and validate the note ID + note_id = note.get('id') + if not note_id: + raise ValueError("Note ID is required") - # Extract and validate the new model name - new_model_name = note.get('modelName') - if not new_model_name: - raise ValueError("Model name is required") + # Extract and validate the new model name + new_model_name = note.get('modelName') + if not new_model_name: + raise ValueError("Model name is required") - # Extract and validate the new fields - new_fields = note.get('fields') - if not new_fields or not isinstance(new_fields, dict): - raise ValueError("Fields must be provided as a dictionary") + # Extract and validate the new fields + new_fields = note.get('fields') + if not new_fields or not isinstance(new_fields, dict): + raise ValueError("Fields must be provided as a dictionary") - # Extract the new tags - new_tags = note.get('tags', []) + # Extract the new tags + new_tags = note.get('tags', []) - # Get the current note from the collection - anki_note = self.getNote(note_id) + # Get the current note from the collection + anki_note = self.getNote(note_id) - # Get the new model from the collection - collection = self.collection() - new_model = collection.models.by_name(new_model_name) - if not new_model: - raise ValueError(f"Model '{new_model_name}' not found") + # Get the new model from the collection + collection = self.collection() + new_model = collection.models.by_name(new_model_name) + if not new_model: + raise ValueError(f"Model '{new_model_name}' not found") - # Update the note's model - anki_note.mid = new_model['id'] - anki_note._fmap = collection.models.field_map(new_model) - anki_note.fields = [''] * len(new_model['flds']) + # Update the note's model + anki_note.mid = new_model['id'] + anki_note._fmap = collection.models.field_map(new_model) + anki_note.fields = [''] * len(new_model['flds']) - # Update the fields with new values - for name, value in new_fields.items(): - for anki_name in anki_note.keys(): - if name.lower() == anki_name.lower(): - anki_note[anki_name] = value - break + # Update the fields with new values + for name, value in new_fields.items(): + for anki_name in anki_note.keys(): + if name.lower() == anki_name.lower(): + anki_note[anki_name] = value + break - # Update the tags - anki_note.tags = new_tags + # Update the tags + anki_note.tags = new_tags - # Flush changes to ensure they are saved - anki_note.flush() + # Flush changes to ensure they are saved + anki_note.flush() - # Save changes to the collection - collection.autosave() - - except Exception as e: - raise Exception(f"Failed to update note model: {e}") + # Save changes to the collection + collection.autosave() @util.api() def updateNoteTags(self, note, tags):