added tests & documentation, removed dead code

This commit is contained in:
Austin Siew 2022-12-28 18:37:05 -07:00
parent e42e936655
commit 69baea296a
3 changed files with 175 additions and 61 deletions

View File

@ -1865,6 +1865,38 @@ corresponding to when the API was available for use.
} }
``` ```
* **modelFieldFonts**
Gets the complete list of fonts along with their font sizes.
*Sample request*:
```json
{
"action": "modelFieldFonts",
"version": 6,
"params": {
"modelName": "Basic"
}
}
```
*Sample result*:
```json
{
"result": {
"Front": {
"font": "Arial",
"size": 20
},
"Back": {
"font": "Arial",
"size": 20
}
},
"error": null
}
```
* **modelFieldsOnTemplates** * **modelFieldsOnTemplates**
Returns an object indicating the fields on the question and answer side of each card template for the given model Returns an object indicating the fields on the question and answer side of each card template for the given model
@ -2214,18 +2246,19 @@ corresponding to when the API was available for use.
} }
``` ```
* **modelFieldRemove** * **modelFieldSetFont**
Deletes a field within a given model. Sets the font for a field within a given model.
*Sample Request*: *Sample Request*:
```json ```json
{ {
"action": "modelFieldRemove", "action": "modelFieldSetFont",
"version": 6, "version": 6,
"params": { "params": {
"modelName": "Basic", "modelName": "Basic",
"fieldName": "Front" "fieldName": "Front",
"font": "Courier"
} }
} }
``` ```
@ -2238,6 +2271,58 @@ corresponding to when the API was available for use.
} }
``` ```
* **modelFieldSetFontSize**
Sets the font size for a field within a given model.
*Sample Request*:
```json
{
"action": "modelFieldSetFont",
"version": 6,
"params": {
"modelName": "Basic",
"fieldName": "Front",
"fontSize": 10
}
}
```
*Sample result*:
```json
{
"result": null,
"error": null
}
```
* **modelFieldSetDescription**
Sets the description (the text seen in the gui editor when a field is empty) for a field within a given model.
Older versions of Anki (2.1.49 and below) do not have field descriptions. In that case, this will return with `false`.
*Sample Request*:
```json
{
"action": "modelFieldSetDescription",
"version": 6,
"params": {
"modelName": "Basic",
"fieldName": "Front",
"description": "example field description"
}
}
```
*Sample result*:
```json
{
"result": true,
"error": null
}
```
#### Note Actions #### Note Actions
* **addNote** * **addNote**

View File

@ -1102,18 +1102,13 @@ class AnkiConnect:
def modelFieldFonts(self, modelName): def modelFieldFonts(self, modelName):
model = self.getModel(modelName) model = self.getModel(modelName)
fonts = [] fonts = {}
for field in model['flds']: for field in model['flds']:
#fonts.append([field['font'], field['size']]) fonts[field['name']] = {
'font': field['font'],
fieldFont = { 'size': field['size'],
field['name']: {
'font': field['font'],
'size': field['size'],
}
} }
fonts.append(fieldFont)
return fonts return fonts
@ -1236,16 +1231,6 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldRename(self, modelName, oldFieldName, newFieldName): def modelFieldRename(self, modelName, oldFieldName, newFieldName):
#mm = self.collection().models
#model = mm.byName(modelName)
#if model is None:
# raise Exception('model was not found: {}'.format(modelName))
#fieldMap = mm.fieldMap(model)
#if oldFieldName not in fieldMap:
# raise Exception('field was not found in {}: {}'.format(modelName, oldFieldName))
#field = fieldMap[oldFieldName][1]
mm = self.collection().models mm = self.collection().models
model = self.getModel(modelName) model = self.getModel(modelName)
field = self.getField(modelName, oldFieldName) field = self.getField(modelName, oldFieldName)
@ -1257,16 +1242,6 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldReposition(self, modelName, fieldName, index): def modelFieldReposition(self, modelName, fieldName, index):
#mm = self.collection().models
#model = mm.byName(modelName)
#if model is None:
# raise Exception('model was not found: {}'.format(modelName))
#fieldMap = mm.fieldMap(model)
#if fieldName not in fieldMap:
# raise Exception('field was not found in {}: {}'.format(modelName, fieldName))
#field = fieldMap[fieldName][1]
mm = self.collection().models mm = self.collection().models
model = self.getModel(modelName) model = self.getModel(modelName)
field = self.getField(modelName, fieldName) field = self.getField(modelName, fieldName)
@ -1281,10 +1256,6 @@ class AnkiConnect:
mm = self.collection().models mm = self.collection().models
model = self.getModel(modelName) model = self.getModel(modelName)
#model = mm.byName(modelName)
#if model is None:
# raise Exception('model was not found: {}'.format(modelName))
# only adds the field if it doesn't already exist # only adds the field if it doesn't already exist
fieldMap = mm.fieldMap(model) fieldMap = mm.fieldMap(model)
if fieldName not in fieldMap: if fieldName not in fieldMap:
@ -1302,16 +1273,6 @@ class AnkiConnect:
@util.api() @util.api()
def modelFieldRemove(self, modelName, fieldName): def modelFieldRemove(self, modelName, fieldName):
#mm = self.collection().models
#model = mm.byName(modelName)
#if model is None:
# raise Exception('model was not found: {}'.format(modelName))
#fieldMap = mm.fieldMap(model)
#if fieldName not in fieldMap:
# raise Exception('field was not found in {}: {}'.format(modelName, fieldName))
#field = fieldMap[fieldName][1]
mm = self.collection().models mm = self.collection().models
model = self.getModel(modelName) model = self.getModel(modelName)
field = self.getField(modelName, fieldName) field = self.getField(modelName, fieldName)
@ -1335,20 +1296,6 @@ class AnkiConnect:
self.save_model(mm, model) self.save_model(mm, model)
@util.api()
def modelFieldSetDescription(self, modelName, fieldName, description):
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, fieldName)
if not isinstance(description, str):
raise Exception('description should be a string: {}'.format(description))
field['description'] = description
self.save_model(mm, model)
@util.api() @util.api()
def modelFieldSetFontSize(self, modelName, fieldName, fontSize): def modelFieldSetFontSize(self, modelName, fieldName, fontSize):
mm = self.collection().models mm = self.collection().models
@ -1363,6 +1310,22 @@ class AnkiConnect:
self.save_model(mm, model) self.save_model(mm, model)
@util.api()
def modelFieldSetDescription(self, modelName, fieldName, description):
mm = self.collection().models
model = self.getModel(modelName)
field = self.getField(modelName, fieldName)
if not isinstance(description, str):
raise Exception('description should be a string: {}'.format(description))
if 'description' in field: # older versions do not have the 'description' key
field['description'] = description
self.save_model(mm, model)
return True
return False
@util.api() @util.api()
def deckNameFromId(self, deckId): def deckNameFromId(self, deckId):
deck = self.collection().decks.get(deckId) deck = self.collection().decks.get(deckId)

View File

@ -21,6 +21,20 @@ def test_modelFieldDescriptions(setup):
assert result == ["", ""] assert result == ["", ""]
def test_modelFieldFonts(setup):
result = ac.modelFieldFonts(modelName="test_model")
assert result == {
"field1": {
"font": "Arial",
"size": 20,
},
"field2": {
"font": "Arial",
"size": 20,
},
}
def test_modelFieldsOnTemplates(setup): def test_modelFieldsOnTemplates(setup):
result = ac.modelFieldsOnTemplates(modelName="test_model") result = ac.modelFieldsOnTemplates(modelName="test_model")
assert result == { assert result == {
@ -172,3 +186,55 @@ class TestModelFieldNames:
result = ac.modelFieldNames(modelName="test_model") result = ac.modelFieldNames(modelName="test_model")
assert result == ["field2"] assert result == ["field2"]
def test_modelFieldSetFont(self, setup):
ac.modelFieldSetFont(
modelName="test_model",
fieldName="field1",
font="Courier",
)
result = ac.modelFieldFonts(modelName="test_model")
assert result == {
"field1": {
"font": "Courier",
"size": 20,
},
"field2": {
"font": "Arial",
"size": 20,
},
}
def test_modelFieldSetFontSize(self, setup):
ac.modelFieldSetFontSize(
modelName="test_model",
fieldName="field2",
fontSize=16,
)
result = ac.modelFieldFonts(modelName="test_model")
assert result == {
"field1": {
"font": "Arial",
"size": 20,
},
"field2": {
"font": "Arial",
"size": 16,
},
}
def test_modelFieldSetDescription(self, setup):
set_desc = ac.modelFieldSetDescription(
modelName="test_model",
fieldName="field1",
description="test description",
)
result = ac.modelFieldDescriptions(modelName="test_model")
if set_desc:
assert result == ["test description", ""]
else:
assert result == ["", ""]