Add modelFieldsOnTemplates action

This commit is contained in:
David Bailey 2017-08-21 15:10:57 +01:00
parent d06b231b51
commit 75a3a25e91
2 changed files with 66 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
@ -527,6 +528,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
@ -857,6 +889,11 @@ class AnkiConnect:
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

@ -209,6 +209,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.