Added model field name api points and skeleton documentation to README
This commit is contained in:
parent
55327b6ccf
commit
ebdca1db6b
115
README.md
115
README.md
@ -2099,6 +2099,121 @@ corresponding to when the API was available for use.
|
|||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
* **modelFieldRename**
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
|
*Sample Request*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"action": "modelFieldRename",
|
||||||
|
"version": 6,
|
||||||
|
"params": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*Sample result*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"result": null,
|
||||||
|
"error": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* **modelFieldReposition**
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
|
*Sample Request*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"action": "modelFieldReposition",
|
||||||
|
"version": 6,
|
||||||
|
"params": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*Sample result*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"result": null,
|
||||||
|
"error": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* **modelFieldAdd**
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
|
*Sample Request*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"action": "modelFieldAdd",
|
||||||
|
"version": 6,
|
||||||
|
"params": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*Sample result*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"result": null,
|
||||||
|
"error": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* **modelFieldRemove**
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
|
*Sample Request*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"action": "modelFieldRemove",
|
||||||
|
"version": 6,
|
||||||
|
"params": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*Sample result*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"result": null,
|
||||||
|
"error": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
* **editFieldNames**
|
||||||
|
|
||||||
|
TODO
|
||||||
|
|
||||||
|
*Sample Request*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"action": "editFieldNames",
|
||||||
|
"version": 6,
|
||||||
|
"params": {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
*Sample result*:
|
||||||
|
```json
|
||||||
|
{
|
||||||
|
"result": null,
|
||||||
|
"error": null
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
#### Note Actions
|
#### Note Actions
|
||||||
|
|
||||||
* **addNote**
|
* **addNote**
|
||||||
|
@ -1185,6 +1185,106 @@ class AnkiConnect:
|
|||||||
return updatedModels
|
return updatedModels
|
||||||
|
|
||||||
|
|
||||||
|
@util.api()
|
||||||
|
def modelFieldRename(self, modelName, oldFieldName, newFieldName):
|
||||||
|
#self.startEditing()
|
||||||
|
|
||||||
|
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.renameField(model, field, newFieldName)
|
||||||
|
|
||||||
|
self.save_model(mm, model)
|
||||||
|
#self.stopEditing()
|
||||||
|
|
||||||
|
|
||||||
|
@util.api()
|
||||||
|
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.repositionField(model, field, index)
|
||||||
|
|
||||||
|
self.save_model(mm, model)
|
||||||
|
|
||||||
|
|
||||||
|
@util.api()
|
||||||
|
def modelFieldAdd(self, modelName, fieldName, index=None):
|
||||||
|
#self.startEditing()
|
||||||
|
|
||||||
|
mm = self.collection().models
|
||||||
|
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
|
||||||
|
fieldMap = mm.fieldMap(model)
|
||||||
|
if fieldName not in fieldMap:
|
||||||
|
field = mm.newField(fieldName)
|
||||||
|
mm.addField(model, field)
|
||||||
|
|
||||||
|
# repositions, even if the field already exists
|
||||||
|
if index is not None:
|
||||||
|
fieldMap = mm.fieldMap(model)
|
||||||
|
newField = fieldMap[fieldName][1]
|
||||||
|
mm.repositionField(model, newField, index)
|
||||||
|
|
||||||
|
self.save_model(mm, model)
|
||||||
|
#self.stopEditing()
|
||||||
|
|
||||||
|
|
||||||
|
@util.api()
|
||||||
|
def modelFieldRemove(self, modelName, fieldName):
|
||||||
|
#self.startEditing()
|
||||||
|
|
||||||
|
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.removeField(model, field)
|
||||||
|
|
||||||
|
self.save_model(mm, model)
|
||||||
|
#self.stopEditing()
|
||||||
|
|
||||||
|
|
||||||
|
@util.api()
|
||||||
|
def editFieldNames(self, modelName, actions):
|
||||||
|
actionToFuncMap = {
|
||||||
|
'rename': self.modelFieldRename,
|
||||||
|
'reposition': self.modelFieldReposition,
|
||||||
|
'add': self.modelFieldAdd,
|
||||||
|
'remove': self.modelFieldRemove,
|
||||||
|
}
|
||||||
|
|
||||||
|
for actionDict in actions:
|
||||||
|
action = actionDict['action']
|
||||||
|
if action not in actionToFuncMap:
|
||||||
|
raise Exception('invalid edit field name action: {}'.format(action))
|
||||||
|
func = actionToFuncMap[action]
|
||||||
|
args = {k: v for k, v in actionDict.items() if k != 'action'}
|
||||||
|
func(modelName=modelName, **args)
|
||||||
|
|
||||||
|
|
||||||
@util.api()
|
@util.api()
|
||||||
def deckNameFromId(self, deckId):
|
def deckNameFromId(self, deckId):
|
||||||
deck = self.collection().decks.get(deckId)
|
deck = self.collection().decks.get(deckId)
|
||||||
|
@ -110,3 +110,99 @@ def test_findAndReplaceInModels(setup):
|
|||||||
assert ac.modelStyling(modelName="test_model") == {
|
assert ac.modelStyling(modelName="test_model") == {
|
||||||
"css": "* {color: blue;}"
|
"css": "* {color: blue;}"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class TestModelFieldNames:
|
||||||
|
def test_modelFieldRename(self, setup):
|
||||||
|
ac.modelFieldRename(
|
||||||
|
modelName="test_model",
|
||||||
|
oldFieldName="field1",
|
||||||
|
newFieldName="foo",
|
||||||
|
)
|
||||||
|
|
||||||
|
result = ac.modelFieldNames(modelName="test_model")
|
||||||
|
assert result == ["foo", "field2"]
|
||||||
|
|
||||||
|
def test_modelFieldReposition(self, setup):
|
||||||
|
ac.modelFieldReposition(
|
||||||
|
modelName="test_model",
|
||||||
|
fieldName="field1",
|
||||||
|
index=2,
|
||||||
|
)
|
||||||
|
|
||||||
|
result = ac.modelFieldNames(modelName="test_model")
|
||||||
|
assert result == ["field2", "field1"]
|
||||||
|
|
||||||
|
def test_modelFieldAdd(self, setup):
|
||||||
|
ac.modelFieldAdd(
|
||||||
|
modelName="test_model",
|
||||||
|
fieldName="Foo",
|
||||||
|
)
|
||||||
|
|
||||||
|
result = ac.modelFieldNames(modelName="test_model")
|
||||||
|
assert result == ["field1", "field2", "Foo"]
|
||||||
|
|
||||||
|
def test_modelFieldAddIndex(self, setup):
|
||||||
|
ac.modelFieldAdd(
|
||||||
|
modelName="test_model",
|
||||||
|
fieldName="Foo",
|
||||||
|
index=1,
|
||||||
|
)
|
||||||
|
|
||||||
|
result = ac.modelFieldNames(modelName="test_model")
|
||||||
|
assert result == ["field1", "Foo", "field2"]
|
||||||
|
|
||||||
|
def test_modelFieldRemove(self, setup):
|
||||||
|
# makes sure that the front template always has a field,
|
||||||
|
# and makes sure that the front template of the cards are not the same
|
||||||
|
ac.updateModelTemplates(model={
|
||||||
|
"name": "test_model",
|
||||||
|
"templates": {"Card 1": {"Front": "{{field2}} {{field2}}", "Back": "foo"}}
|
||||||
|
})
|
||||||
|
|
||||||
|
ac.modelFieldRemove(
|
||||||
|
modelName="test_model",
|
||||||
|
fieldName="field1",
|
||||||
|
)
|
||||||
|
|
||||||
|
result = ac.modelFieldNames(modelName="test_model")
|
||||||
|
assert result == ["field2"]
|
||||||
|
|
||||||
|
def test_editFieldNames(self, setup):
|
||||||
|
editFieldNames_kwargs = {
|
||||||
|
"modelName": "test_model",
|
||||||
|
"actions": [
|
||||||
|
{
|
||||||
|
"action": "add",
|
||||||
|
"fieldName": "field3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "add",
|
||||||
|
"fieldName": "field4",
|
||||||
|
"index": 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "add",
|
||||||
|
"fieldName": "field5",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "reposition",
|
||||||
|
"fieldName": "field3",
|
||||||
|
"index": 0,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "rename",
|
||||||
|
"oldFieldName": "field3",
|
||||||
|
"newFieldName": "renamed_field3",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"action": "remove",
|
||||||
|
"fieldName": "field5",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
ac.editFieldNames(**editFieldNames_kwargs)
|
||||||
|
|
||||||
|
result = ac.modelFieldNames(modelName="test_model")
|
||||||
|
assert result == ["renamed_field3", "field1", "field4", "field2"]
|
||||||
|
Loading…
Reference in New Issue
Block a user