Update changes from PR and add documentation

This commit is contained in:
c-okelly 2019-03-01 17:38:30 +00:00
parent 31aad05668
commit bbcb29ea43
3 changed files with 112 additions and 3 deletions

View File

@ -32,7 +32,6 @@ from operator import itemgetter
from time import time
from unicodedata import normalize
#
# Constants
#
@ -849,8 +848,15 @@ class AnkiConnect:
@api()
def createModel(self, modelName, inOrderFields, cardTemplates):
def createModel(self, modelName, inOrderFields, cardTemplates, css = None):
# https://github.com/dae/anki/blob/b06b70f7214fb1f2ce33ba06d2b095384b81f874/anki/stdmodels.py
if (len(inOrderFields) == 0):
raise Exception('Must provide at least one field for inOrderFields')
if (len(cardTemplates) == 0):
raise Exception('Must provide at least one card for cardTemplates')
if (modelName in self.collection().models.allNames()):
raise Exception('Model name already exists')
collection = self.collection()
mm = collection.models
@ -862,7 +868,10 @@ class AnkiConnect:
fm = mm.newField(_(field))
mm.addField(m, fm)
# TODO? => There is no validation that card fields are used correctly
# Add shared css to model if exists. Use default otherwise
if (css is not None):
m['css'] = css
# Generate new card template(s)
cardCount = 1
for card in cardTemplates:

View File

@ -727,6 +727,101 @@ guarantee that your application continues to function properly in the future.
}
```
**Create new Model**
Creates a new model to be used in Anki. User must provide modelName, inOrderFields and cardTemplates to be used
in the model.
*Sample request*
```json
{
"action": "createNewModel",
"version": 6,
"params": {
"modelName": "newModelName",
"inOrderFields": ["Field1", "Field2", "Field3"],
"css": "Optional CSS with default to builtin css",
"cardTemplates": [
{
"Front": "Front html {{Field1}}",
"Back": "Back html {{Field2}}"
}
]
}
}
```
*Sample result*
```json
{
"result":{
"sortf":0,
"did":1,
"latexPre":"\\documentclass[12pt]{article}\n\\special{papersize=3in,5in}\n\\usepackage[utf8]{inputenc}\n\\usepackage{amssymb,amsmath}\n\\pagestyle{empty}\n\\setlength{\\parindent}{0in}\n\\begin{document}\n",
"latexPost":"\\end{document}",
"mod":1551462107,
"usn":-1,
"vers":[
],
"type":0,
"css":".card {\n font-family: arial;\n font-size: 20px;\n text-align: center;\n color: black;\n background-color: white;\n}\n",
"name":"TestApiModel",
"flds":[
{
"name":"Field1",
"ord":0,
"sticky":false,
"rtl":false,
"font":"Arial",
"size":20,
"media":[
]
},
{
"name":"Field2",
"ord":1,
"sticky":false,
"rtl":false,
"font":"Arial",
"size":20,
"media":[
]
}
],
"tmpls":[
{
"name":"Card 1",
"ord":0,
"qfmt":"",
"afmt":"This is the back of the card {{Field2}}",
"did":null,
"bqfmt":"",
"bafmt":""
}
],
"tags":[
],
"id":"1551462107104",
"req":[
[
0,
"none",
[
]
]
]
},
"error":null
}
```
#### Notes ####
* **addNote**

View File

@ -20,6 +20,11 @@ class TestModels(unittest.TestCase):
# modelFieldsOnTemplates
modelFieldsOnTemplates = util.invoke('modelFieldsOnTemplates', modelName=modelNames[0])
# createModel with css
newModel = util.invoke('createModel', modelName='testModel', inOrderFields=['field1', 'field2'], cardTemplates=[{'Front':'field1','Back':'field2'}], css='some random css')
# createModel without css
newModel = util.invoke('createModel', modelName='testModel-second', inOrderFields=['field1', 'field2'], cardTemplates=[{'Front':'field1','Back':'field2'}])
if __name__ == '__main__':
unittest.main()