Merge pull request #38 from techdavid/more-model-actions

Add more model actions
This commit is contained in:
Alex Yatskov 2017-08-21 15:48:27 -07:00 committed by GitHub
commit 4c9721e28b
2 changed files with 104 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import hashlib
import inspect
import json
import os.path
import re
import select
import socket
import sys
@ -511,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:
@ -527,6 +540,37 @@ class AnkiBridge:
return [field['name'] for field in model['flds']]
def modelFieldsOnTemplates(self, modelName):
model = self.collection().models.byName(modelName)
if model is not None:
templates = {}
for template in model['tmpls']:
fields = []
for side in ['qfmt', 'afmt']:
fieldsForSide = []
# based on _fieldsOnTemplate from aqt/clayout.py
matches = re.findall('{{[^#/}]+?}}', template[side])
for match in matches:
# remove braces and modifiers
match = re.sub(r'[{}]', '', match)
match = match.split(":")[-1]
# for the answer side, ignore fields present on the question side + the FrontSide field
if match == 'FrontSide' or side == 'afmt' and match in fields[0]:
continue
fieldsForSide.append(match)
fields.append(fieldsForSide)
templates[template['name']] = fields
return templates
def getDeckConfig(self, deck):
if not deck in self.deckNames():
return False
@ -852,11 +896,21 @@ class AnkiConnect:
return self.anki.modelNames()
@webApi
def modelNamesAndIds(self):
return self.anki.modelNamesAndIds()
@webApi
def modelFieldNames(self, modelName):
return self.anki.modelFieldNames(modelName)
@webApi
def modelFieldsOnTemplates(self, modelName):
return self.anki.modelFieldsOnTemplates(modelName)
@webApi
def getDeckConfig(self, deck):
return self.anki.getDeckConfig(deck)

View File

@ -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.
@ -209,6 +230,35 @@ Below is a list of currently supported actions. Requests with invalid actions or
]
```
* **modelFieldsOnTemplates**
Returns an object indicating the fields on the question and answer side of each card template for the given model
name. The question side is given first in each array.
*Sample request*:
```
{
"action": "modelFieldsOnTemplates",
"params": {
"modelName": "Basic (and reversed card)"
}
}
```
*Sample response*:
```
{
"Card 1": [
["Front"],
["Back"]
],
"Card 2": [
["Back"],
["Front"]
]
}
```
* **getDeckConfig**
Gets the config group object for the given deck.