Si necesitas una funcionalidad específica que no está en la biblioteca de automatismos o que el producto haga algo muy concreto para tu proceso de negocio, puedes desarrollar tu propia operación en Python. Las operaciones o automatismos personalizados son atómicos y están desacopladas del núcleo de Athento, lo que significa que puedes instalarlas o desinstalarlas sin afectar el funcionamiento global de la plataforma.
Para escribir el código de tu operación, puedes guiarte por el código de ejemplo a continuación.
Ejemplo del código de una operación en Athento
class HelloWorldDocumentOperation(DocumentOperation):
# Version of the operation. The version of an operation is defined with X.Y format:
# - X: Is "mayor" and only changes if the behavior of the operation changes or are breaking
# changes (including new parameters). If changes, a new version of the operation file
# must be created (e.g.: hello_world_v3.py) and the old version must be deprecated (indicated in
# the description and name).
# - Y: Is "minor" and changes if small changes, bug fixes, etc. are applied. Not is required to create a new file.
version = '2.1'
name = 'Hello World Document Operation!'
documentation = 'https://athento.zendesk.com/knowledge/articles/360024905974/'
description = """
This operation is the "Hello World Document Operation!" of operations and can be used to test the operation
system.
"""
deprecated = False # If it's true,the operations doesn't appear in the marketplace
configuration_parameters = {
'my-text-parameter-key': {
'label': 'Text parameter',
'help_text': 'My text parameter',
'type': ProcessOperationParameterType.TEXT.value[0],
'default_value': 'Example default value',
},
'my-integer-parameter-key': {
'label': 'Integer parameter',
'help_text': 'My integer parameter',
'type': ProcessOperationParameterType.INTEGER.value[0],
'default_value': 1,
},
'my-float-parameter-key': {
'label': 'Float parameter',
'help_text': 'My float parameter',
'type': ProcessOperationParameterType.FLOAT.value[0],
'default_value': 1.1,
},
'my-evaluable-parameter-key': {
'label': 'Evaluable parameter',
'help_text': 'My evaluable parameter',
'type': ProcessOperationParameterType.EVALUABLE.value[0],
'default_value': '"Default evaluable value"',
},
'my-bool-parameter-key': {
'label': 'Boolean parameter',
'help_text': 'If unchecked, the operation will throw an exception',
'type': ProcessOperationParameterType.BOOLEAN.value[0],
'default_value': True,
},
'my-date-parameter-key': {
'label': 'Date parameter',
'help_text': 'My date parameter',
'type': ProcessOperationParameterType.DATE.value[0],
'default_value': '2022-01-01',
},
'my-datetime-parameter-key': {
'label': 'Datetime parameter',
'help_text': 'My date parameter',
'type': ProcessOperationParameterType.DATETIME.value[0],
'default_value': '2022-01-01 12:00:00',
},
'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,
'default_value': {"label": "Custom label B", "value": "value_b"},
},
'my-metadata-type-parameter-key': {
'label': 'Metadata type parameter',
'help_text': 'My metadata type parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.METADATA_TYPE.value[0],
'is_multiple': False,
},
'my-user-parameter-key': {
'label': 'User parameter',
'help_text': 'My user parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.USER.value[0],
'is_multiple': False,
},
'my-document-type-parameter-key': {
'label': 'Document type parameter',
'help_text': 'My document type parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.DOCUMENT_TYPE.value[0],
'is_multiple': False,
},
'my-serie-parameter-key': {
'label': 'Space parameter',
'help_text': 'My space type parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.SERIE.value[0],
'is_multiple': False,
},
'my-custom-choice-multiple-key': {
'label': 'Custom choice multiple 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],
'default_value': [{"label": "Custom label A", "value": "value_a"},
{"label": "Custom label B", "value": "value_b"}],
'is_multiple': True,
},
'my-metadata-type-parameter-multiple-key': {
'label': 'Metadata type multiple parameter',
'help_text': 'My metadata type parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.METADATA_TYPE.value[0],
'is_multiple': True,
},
'my-user-parameter-multiple-key': {
'label': 'User multiple parameter',
'help_text': 'My user parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.USER.value[0],
'is_multiple': True,
},
'my-serie-parameter-multiple-key': {
'label': 'Space multiple parameter',
'help_text': 'My Space parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.SERIE.value[0],
'is_multiple': True,
},
'my-document-workflow-task-type-parameter-multiple-key': {
'label': 'Document workflow task type multiple parameter',
'help_text': 'My Document workflow task type multiple parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.DOCUMENT_WORKFLOW_TASK_TYPE.value[0],
'is_multiple': True,
},
'my-life-cycle-state-parameter-key': {
'label': 'Life cycle state parameter',
'help_text': 'My life cycle state parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.LIFE_CYCLE_STATE.value[0],
'is_multiple': False,
},
'my-life-cycle-state-parameter-multiple-key': {
'label': 'Life cycle state multiple parameter',
'help_text': 'My life cycle state multiple parameter',
'type': ProcessOperationParameterType.CHOICE.value[0],
'subtype': ProcessOperationParameterChoiceType.LIFE_CYCLE_STATE.value[0],
'is_multiple': True,
}
}
def execute(self, *args, **kwargs):
"""Here is the code that will be executed by the operation."""
# E.g.: Sum A and B
a = 10
b = 32
result = a + b
# Properties of the operation
# Current operation instance
operation = self.operation
# Execution id
execution_id = self.execution_id
# Operation parameters
parameters = self.parameters
# Current document
document = self.document
# Information debug
self.logger.info('Debug message')
# Information logger
self.logger.info('Info message')
# Error logger
self.logger.error('Error message')
# Warning logger
self.logger.warning('Warning message')
# Exception logger
self.logger.exception('Exception message')
# Critical logger
self.logger.critical('Critical message')
# Raise custom operation error example
is_check = parameters.get('my-bool-parameter-key')
if not is_check:
self.warning(_('Hello world, this is an operation warning exception'))
# self.error(_('Hello world, this is an operation error exception'))
# self.exception(_('Hello world, this is an operation error exception'), type=OperationExceptionType.ERROR)
# The response should be a JSON with some common structure.
return {
'msg_type': 'warning',
'msg': 'The sum of {} + {} is: {}'.format(a, b, result),
}
VERSION = HelloWorldDocumentOperation.version
NAME = HelloWorldDocumentOperation.name
DESCRIPTION = HelloWorldDocumentOperation.description
DOCUMENTATION = HelloWorldDocumentOperation.documentation
DEPRECATED = HelloWorldDocumentOperation.deprecated
ORDER = 100
CATEGORY = ""
POSTLOAD = True
POSTCHARACT = False
POSTCLASSIF = False
POSTEXTRACTION = False
CONFIGURATION_PARAMETERS = HelloWorldDocumentOperation.configuration_parameters
def run(uuid=None, **params):
return HelloWorldDocumentOperation(uuid, **params).run()
Información, métodos y utilidades para usar en tus operaciones personalizadas
To get metadata value: fil.gmv('metadata.name')
To set metadata value: fil.set_metadata('metadata.name', value, overwrite=True)
To get feature value: fil.gfv('feature.name')
To set feature: fil.set_feature('feature.name', value)
Useful methods:
- fil.set_state('new_state')
- fil.validate()
- fil.change_life_cycle()
To get useful File attributes:
- fil.numpages -> gets the number of pages of the document
- fil.state
- fil.file.path()
- fil.serie -> gets the space of the document
- fil.author -> gets the creator of the document
- fil.doctype -> gets the form of the document
- fil.life_cycle_state -> gests the lifecycle state
To give feedback to the final user for operations run from UI, return a dictionary:
{'msg_type':'error',
'msg':'A message for the user',
'document':
{'metadata':
{'metadata.example':'new value',
'metadata.example2':'new value 2'},
'status': 'new status'
}
'redirect_url':'https://a_url_to_redirect_to_the_user'
}
For msg_type options are:
error, warning, success, info (Taken from toastr.js)
Una guía más completa de métodos, expresiones, condiciones y otras utilidades que puedes usar en el desarrollo de una operación personalizada, se encuentra en el artículo:
¿Qué API se puede usar en las operaciones personalizadas y condiciones avanzadas?
Comentarios
0 comentarios
Inicie sesión para dejar un comentario.