Add error logging to the HTTP POST workflow action.

Signed-off-by: Roberto Rosario <roberto.rosario.gonzalez@gmail.com>
This commit is contained in:
Roberto Rosario
2017-08-25 04:14:58 -04:00
parent 60d157717f
commit 7de4ff2740
3 changed files with 39 additions and 14 deletions

View File

@@ -12,7 +12,8 @@ from common import (
MayanAppConfig, menu_facet, menu_main, menu_object, menu_secondary,
menu_setup, menu_sidebar, menu_tools
)
from common.classes import ModelAttribute
from common.classes import ErrorLogNamespace, ModelAttribute
from common.links import link_error_list
from common.widgets import two_state_template
from mayan.celery import app
from navigation import SourceColumn
@@ -62,9 +63,8 @@ class DocumentStatesApp(MayanAppConfig):
Document = apps.get_model(
app_label='documents', model_name='Document'
)
Document.add_to_class(
'workflow', DocumentStateHelper.constructor
ErrorLogEntry = apps.get_model(
app_label='common', model_name='ErrorLogEntry'
)
Workflow = self.get_model('Workflow')
@@ -76,6 +76,12 @@ class DocumentStatesApp(MayanAppConfig):
WorkflowStateRuntimeProxy = self.get_model('WorkflowStateRuntimeProxy')
WorkflowTransition = self.get_model('WorkflowTransition')
Document.add_to_class(
'workflow', DocumentStateHelper.constructor
)
ErrorLogEntry.objects.register(model=WorkflowStateAction)
WorkflowAction.initialize()
ModelAttribute(
@@ -258,6 +264,7 @@ class DocumentStatesApp(MayanAppConfig):
menu_object.bind_links(
links=(
link_setup_workflow_state_action_edit,
link_error_list,
link_setup_workflow_state_action_delete,
), sources=(WorkflowStateAction,)
)

View File

@@ -454,10 +454,10 @@ class WorkflowInstanceLogEntry(models.Model):
result = super(WorkflowInstanceLogEntry, self).save(*args, **kwargs)
for action in self.transition.origin_state.exit_actions.all():
action.execute(context={'entry_log': self})
action.execute(context={'action': action, 'entry_log': self})
for action in self.transition.destination_state.entry_actions.all():
action.execute(context={'entry_log': self})
action.execute(context={'action': action, 'entry_log': self})
return result

View File

@@ -65,20 +65,38 @@ class HTTPPostAction(WorkflowAction):
}
def execute(self, context):
try:
url = Template(self.url).render(
context=Context(context)
)
except Exception as exception:
context['action'].error_logs.create(
result='URL template error: {}'.format(exception)
)
return
logger.debug('URL template result: %s', url)
try:
result = Template(self.payload or '{}').render(
context=Context(context)
)
except Exception as exception:
context['action'].error_logs.create(
result='Payload template error: {}'.format(exception)
)
return
logger.debug('payload template result: %s', result)
payload = json.loads(result)
try:
payload = json.loads(result, strict=False)
except Exception as exception:
context['action'].error_logs.create(
result='Payload JSON error: {}'.format(exception)
)
return
logger.debug('payload json result: %s', payload)
response = requests.post(url=url, data=payload)
requests.post(url=url, data=payload)