From 336dc8bb31d293cde7de0bcac23ab881834a129c Mon Sep 17 00:00:00 2001 From: introt Date: Sat, 26 Nov 2022 14:56:34 +0200 Subject: [PATCH 1/2] Added modelFieldDescriptions Similar to modelFieldNames, returns a list of note field decriptions (the text seen in the gui editor when the field is empty) --- README.md | 23 +++++++++++++++++++++++ plugin/__init__.py | 9 +++++++++ tests/test_models.py | 5 +++++ 3 files changed, 37 insertions(+) diff --git a/README.md b/README.md index 7516ac7..0e66f7f 100644 --- a/README.md +++ b/README.md @@ -1829,6 +1829,29 @@ corresponding to when the API was available for use. } ``` +* **modelFieldDescriptions** + + Gets the complete list of field descriptions (the text seen in the gui editor when a field is empty) for the provided model name. + + *Sample request*: + ```json + { + "action": "modelFieldDescriptions", + "version": 6, + "params": { + "modelName": "Basic" + } + } + ``` + + *Sample result*: + ```json + { + "result": ["", ""], + "error": null + } + ``` + * **modelFieldsOnTemplates** Returns an object indicating the fields on the question and answer side of each card template for the given model diff --git a/plugin/__init__.py b/plugin/__init__.py index 056ca01..9562013 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1069,6 +1069,15 @@ class AnkiConnect: return [field['name'] for field in model['flds']] + @util.api() + def modelFieldDescriptions(self, modelName): + model = self.collection().models.byName(modelName) + if model is None: + raise Exception('model was not found: {}'.format(modelName)) + else: + return [field['description'] for field in model['flds']] + + @util.api() def modelFieldsOnTemplates(self, modelName): model = self.collection().models.byName(modelName) diff --git a/tests/test_models.py b/tests/test_models.py index 7c5dbb7..bed3b82 100755 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -16,6 +16,11 @@ def test_modelFieldNames(setup): assert result == ["field1", "field2"] +def test_modelFieldDescriptions(setup): + result = ac.modelFieldDescriptions(modelName="test_model") + assert result == ["", ""] + + def test_modelFieldsOnTemplates(setup): result = ac.modelFieldsOnTemplates(modelName="test_model") assert result == { From ff4fb13428f733c4cface371e1d57026ee3de429 Mon Sep 17 00:00:00 2001 From: introt Date: Sun, 27 Nov 2022 12:19:58 +0200 Subject: [PATCH 2/2] Fix KeyError in modelFieldDescriptions on older Anki versions, where field descriptions aren't available --- plugin/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/plugin/__init__.py b/plugin/__init__.py index 9562013..0f6fcbb 100644 --- a/plugin/__init__.py +++ b/plugin/__init__.py @@ -1075,7 +1075,12 @@ class AnkiConnect: if model is None: raise Exception('model was not found: {}'.format(modelName)) else: - return [field['description'] for field in model['flds']] + try: + return [field['description'] for field in model['flds']] + except KeyError: + # older versions of Anki don't have field descriptions + return ['' for field in model['flds']] + @util.api()