From 2e09db9dab2242f69c1979fdd5f19e9c651ba869 Mon Sep 17 00:00:00 2001 From: David Bailey Date: Mon, 21 Aug 2017 17:15:11 +0100 Subject: [PATCH] Add modelNamesAndIds action --- AnkiConnect.py | 17 +++++++++++++++++ README.md | 21 +++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/AnkiConnect.py b/AnkiConnect.py index 391e6ce..036e935 100644 --- a/AnkiConnect.py +++ b/AnkiConnect.py @@ -512,6 +512,18 @@ class AnkiBridge: return collection.models.allNames() + def modelNamesAndIds(self): + models = {} + + modelNames = self.modelNames() + for model in modelNames: + mid = self.collection().models.byName(model)['id'] + mid = int(mid) # sometimes Anki stores the ID as a string + models[model] = mid + + return models + + def modelNameFromId(self, modelId): collection = self.collection() if collection is not None: @@ -884,6 +896,11 @@ class AnkiConnect: return self.anki.modelNames() + @webApi + def modelNamesAndIds(self): + return self.anki.modelNamesAndIds() + + @webApi def modelFieldNames(self, modelName): return self.anki.modelFieldNames(modelName) diff --git a/README.md b/README.md index 8d2c1fc..05320b5 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,27 @@ Below is a list of currently supported actions. Requests with invalid actions or ] ``` +* **modelNamesAndIds** + + Gets the complete list of model names and their corresponding IDs for the current user. + + *Sample request*: + ``` + { + "action": "modelNamesAndIds" + } + ``` + + *Sample response*: + ``` + { + "Basic": 1483883011648 + "Basic (and reversed card)": 1483883011644 + "Basic (optional reversed card)": 1483883011631 + "Cloze": 1483883011630 + } + ``` + * **modelFieldNames** Gets the complete list of field names for the provided model name.