The parameters of operations can be configured as a dictionary, or typed parameters can be used. The advantage of typed parameters is that they allow the user to choose or enter specific values, making it easier to configure operations or minimizing errors when entering data.
To begin with, there is an operation called helloworld (athentose/custom/operations/helloworld.py) that contains examples of the parameters. Before continuing to read, examine the contents of that file.
Here is an example of parameters:
The key of this dictionary is the name of the parameter that we can use to obtain its value.
The fields that must be filled in are:
- label: Label that the parameter will have in the operation.
- help_text: Help text for the user.
- type: Indicates the type of object that this parameter will return.
- subtype (optional): If the parameter is CHOICE, it indicates the type of choices that will be presented to the user. In this example, users are presented.
- is_multiple (optional): Indicates whether multiple values can be selected.
For the type field, the available options are:
TEXT: the parameter is a text.
INTEGER: the parameter is an integer number.
FLOAT: the parameter is a number with decimals.
EVALUABLE: the parameter is a text string that is evaluated as Python code.
BOOLEAN: the parameter is a boolean value (enabled/disabled).
DATE: the parameter is a date.
DATETIME: the parameter is a date and time.
CHOICE: the parameter presents a series of options to choose according to the subtype.
For the subtype field, the available options are:
CUSTOM: it allows defining within the parameter the available options.
METADATA_TYPE: it allows choosing among the available field types.
USER: it allows choosing among the available users.
DOCUMENT_TYPE: it allows choosing among the available forms
In the case of choosing CHOICE as type and CUSTOM as subtype, custom values can be defined by adding a “choices” key to the configuration of this parameter, as in this example:
'my-custom-choice-key': {
'label': 'Custom choice parameter',
'help_text': 'My custom choice parameter',
'choices': [{"label": "Custom label A", "value": "value_a"},
{"label": "Custom label B", "value": "value_b"}],
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.CUSTOM.value[0],
'is_multiple': False,
}
Where “label” is the label shown in the configuration interface for the user and “value” is the internal value that the operation will use.
To, from the operation code, obtain the value of a specific parameter you can do params.get(‘my-user-parameter-key’) and this returns a value whose type depends on the configuration (for the example, it returns an object of the User class).
There is no automatic method to change operation parameters to use this type of parameters, since each operation uses parameters in a different way.
Broadly speaking, the steps to follow are (I take the op_start_lifecycle operation as an example):
- Examine the parameters that exist
PARAMS = {'life_cycle_name': '', 'parallel':''} - Study in the operation code its type and its usage.
In this specific example we see that ‘life_cycle_name’ is the name of a life cycle, and ‘parallel’ is used as a boolean. - Write the parameters in a variable named CONFIGURATION_PARAMETERS.
CONFIGURATION_PARAMETERS = {
'life_cycle_name': {
'label': 'Life cycle Name',
'help_text': 'Name of the life cycle',
'type': ProcessOperationParameterType.TEXT.value[0],
},
'parallel': {
'label': 'Parallel',
'help_text': 'Select to indicate if the life cycle is parallel',
'type': ProcessOperationParameterType.BOOLEAN.value[0],
},
} - Use params.get() where appropriate, and check that the operation works.
Finally, it is possible that some operations that are previously installed on a server and whose parameters are changed do not display properly. The reason is that the parameters will be stored as a dictionary and they need to be updated.
The following command has been created that goes through each installed and configured operation, and tries to transform the PARAMS in dictionary form into configurable parameters. After executing it, it is advisable to review that the created parameters are correct since the type cannot always be properly inferred:
python manage.py migrate_operation_parameters