Compare commits

...

3 Commits

Author SHA1 Message Date
Tategoto Azarasi
306103c618 update README.md
Signed-off-by: Tategoto Azarasi <2724167997@qq.com>
2024-05-09 21:43:12 -07:00
Tategoto Azarasi
1c428c8627 remove try-except in updateNoteModel
Signed-off-by: Tategoto Azarasi <2724167997@qq.com>
2024-05-09 21:43:12 -07:00
Tategoto Azarasi
b77327fb00 add updateNoteModel
Signed-off-by: Tategoto Azarasi <2724167997@qq.com>
2024-05-09 21:43:12 -07:00
2 changed files with 95 additions and 0 deletions

View File

@ -3862,6 +3862,47 @@ Search parameters are passed to Anki, check the docs for more information: https
```
</details>
#### `updateNoteModel`
* Update the model, fields, and tags of an existing note.
This allows you to change the note's model, update its fields with new content, and set new tags.
<details>
<summary><i>Sample request:</i></summary>
```json
{
"action": "updateNoteModel",
"version": 6,
"params": {
"note": {
"id": 1514547547030,
"modelName": "NewModel",
"fields": {
"NewField1": "new field 1",
"NewField2": "new field 2",
"NewField3": "new field 3"
},
"tags": ["new", "updated", "tags"]
}
}
}
```
</details>
<details>
<summary><i>Sample result:</i></summary>
```json
{
"result": null,
"error": null
}
```
</details>
#### `updateNoteTags`
* Set a note's tags by note ID. Old tags will be removed.

View File

@ -835,6 +835,60 @@ class AnkiConnect:
if not updated:
raise Exception('Must provide a "fields" or "tags" property.')
@util.api()
def updateNoteModel(self, note):
"""
Update the model and fields of a given note.
:param note: A dictionary containing note details, including 'id', 'modelName', 'fields', and 'tags'.
"""
# 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 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', [])
# 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")
# 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 tags
anki_note.tags = new_tags
# Flush changes to ensure they are saved
anki_note.flush()
# Save changes to the collection
collection.autosave()
@util.api()
def updateNoteTags(self, note, tags):