From 648b118c5b9512c00428bea32ee94d42399719ca Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Thu, 16 Mar 2017 16:23:27 +0100 Subject: [PATCH 2/4] replace use of self.compute(..) by misc.compute(..) (#14510) --- tests/test_workflows.py | 19 +++++++++---------- wcs/wf/backoffice_fields.py | 3 ++- wcs/wf/geolocate.py | 8 ++++---- wcs/wf/jump.py | 3 ++- wcs/wf/profile.py | 4 ++-- wcs/workflows.py | 12 ++++++------ 6 files changed, 25 insertions(+), 24 deletions(-) diff --git a/tests/test_workflows.py b/tests/test_workflows.py index 04ee659..368fa46 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -11,6 +11,7 @@ import mock from quixote import cleanup, get_response from wcs.qommon.http_request import HTTPRequest from qommon.form import * +from qommon.misc import compute from wcs.formdef import FormDef from wcs import sessions @@ -120,26 +121,24 @@ def test_variable_compute(pub): formdata.store() pub.substitutions.feed(formdata) - item = JumpWorkflowStatusItem() - # straight string - assert item.compute('blah') == 'blah' + assert compute('blah') == 'blah' # ezt string - assert item.compute('[form_var_foo]') == 'hello' + assert compute('[form_var_foo]') == 'hello' # ezt string, but not ezt asked - assert item.compute('[form_var_foo]', do_ezt=False) == '[form_var_foo]' + assert compute('[form_var_foo]', do_ezt=False) == '[form_var_foo]' # ezt string, with an error - assert item.compute('[end]', raises=False) == '[end]' + assert compute('[end]', raises=False) == '[end]' with pytest.raises(Exception): - item.compute('[end]', raises=True) + compute('[end]', raises=True) # python expression - assert item.compute('=form_var_foo') == 'hello' + assert compute('=form_var_foo') == 'hello' # python expression, with an error - assert item.compute('=1/0', raises=False) == '=1/0' + assert compute('=1/0', raises=False) == '=1/0' with pytest.raises(Exception): - item.compute('=1/0', raises=True) + compute('=1/0', raises=True) def test_jump_nothing(pub): FormDef.wipe() diff --git a/wcs/wf/backoffice_fields.py b/wcs/wf/backoffice_fields.py index 5b0a683..c488102 100644 --- a/wcs/wf/backoffice_fields.py +++ b/wcs/wf/backoffice_fields.py @@ -22,6 +22,7 @@ from quixote import get_publisher from qommon import _ from qommon import get_logger +from qommon.misc import compute from qommon.form import (WidgetListAsTable, CompositeWidget, SingleSelectWidget, ComputedExpressionWidget, PicklableUpload) from wcs.fields import WidgetField @@ -122,7 +123,7 @@ class SetBackofficeFieldsWorkflowStatusItem(WorkflowStatusItem): continue try: - new_value = self.compute(field['value'], raises=True) + new_value = compute(field['value'], raises=True) except: get_publisher().notify_of_exception(sys.exc_info()) continue diff --git a/wcs/wf/geolocate.py b/wcs/wf/geolocate.py index cde5b42..9277fa9 100644 --- a/wcs/wf/geolocate.py +++ b/wcs/wf/geolocate.py @@ -29,7 +29,7 @@ from quixote import get_publisher from qommon import _ from qommon import get_logger from qommon.form import RadiobuttonsWidget, ComputedExpressionWidget, CheckboxWidget -from qommon.misc import http_get_page +from qommon.misc import http_get_page, compute from wcs.workflows import WorkflowStatusItem, register_item_class class GeolocateWorkflowStatusItem(WorkflowStatusItem): @@ -107,7 +107,7 @@ class GeolocateWorkflowStatusItem(WorkflowStatusItem): nominatim_url = 'http://nominatim.openstreetmap.org' try: - address = self.compute(self.address_string, raises=True) + address = compute(self.address_string, raises=True) except Exception, e: get_logger().error('error in template for address string [%r]', e) return @@ -129,7 +129,7 @@ class GeolocateWorkflowStatusItem(WorkflowStatusItem): return {'lon': float(coords['lon']), 'lat': float(coords['lat'])} def geolocate_map_variable(self, formdata): - value = self.compute(self.map_variable) + value = compute(self.map_variable) if not value: return @@ -146,7 +146,7 @@ class GeolocateWorkflowStatusItem(WorkflowStatusItem): get_logger().error('error geolocating from file (missing PIL)') return - value = self.compute(self.photo_variable) + value = compute(self.photo_variable) if not hasattr(value, 'get_file_pointer'): get_logger().error('error geolocating from photo, invalid variable') return diff --git a/wcs/wf/jump.py b/wcs/wf/jump.py index af4dcf4..9f953de 100644 --- a/wcs/wf/jump.py +++ b/wcs/wf/jump.py @@ -27,6 +27,7 @@ from qommon.form import * from qommon import errors from qommon.publisher import get_publisher_class from qommon.cron import CronJob +from qommon.misc import compute from wcs.workflows import Workflow, WorkflowStatusJumpItem, register_item_class from wcs.api import get_user_from_api_query_string, is_url_signed @@ -215,7 +216,7 @@ class JumpWorkflowStatusItem(WorkflowStatusJumpItem): must_jump = must_jump and triggered if self.timeout: - timeout = int(self.compute(self.timeout)) + timeout = int(compute(self.timeout)) if formdata.evolution: last = formdata.evolution[-1].time else: diff --git a/wcs/wf/profile.py b/wcs/wf/profile.py index ec593b8..b0cf79e 100644 --- a/wcs/wf/profile.py +++ b/wcs/wf/profile.py @@ -24,7 +24,7 @@ from qommon import _ from qommon.form import (CompositeWidget, SingleSelectWidget, WidgetListAsTable, ComputedExpressionWidget) from qommon.ident.idp import is_idp_managing_user_attributes -from qommon.misc import http_patch_request +from qommon.misc import http_patch_request, compute from qommon.publisher import get_cfg, get_logger from wcs.api_utils import sign_url, get_secret_and_orig, MissingSecret @@ -145,7 +145,7 @@ class UpdateUserProfileStatusItem(WorkflowStatusItem): get_publisher().substitutions.feed(formdata) new_data = {} for field in self.fields: - new_data[field.get('field_id')] = self.compute(field.get('value')) + new_data[field.get('field_id')] = compute(field.get('value')) user_formdef = get_publisher().user_class.get_formdef() new_user_data = {} diff --git a/wcs/workflows.py b/wcs/workflows.py index 0396541..a4ac894 100644 --- a/wcs/workflows.py +++ b/wcs/workflows.py @@ -1856,7 +1856,7 @@ class ChoiceWorkflowStatusItem(WorkflowStatusJumpItem): return _('Change Status (not completed)') def fill_form(self, form, formdata, user): - form.add_submit('button%s' % self.id, self.compute(self.label)) + form.add_submit('button%s' % self.id, compute(self.label)) if self.require_confirmation: get_response().add_javascript(['jquery.js', '../../i18n.js', 'qommon.js']) widget = form.get_widget('button%s' % self.id) @@ -2010,13 +2010,13 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem): url = formdata.get_url() try: - mail_body = template_on_formdata(formdata, self.compute(self.body, do_ezt=False)) + mail_body = template_on_formdata(formdata, compute(self.body, do_ezt=False)) except ezt.EZTException: get_logger().error('error in template for email body [%s], mail could not be generated' % url) return try: - mail_subject = template_on_formdata(formdata, self.compute(self.subject, do_ezt=False)) + mail_subject = template_on_formdata(formdata, compute(self.subject, do_ezt=False)) except ezt.EZTException: get_logger().error('error in template for email subject [%s], mail could not be generated' % url) return @@ -2032,7 +2032,7 @@ class SendmailWorkflowStatusItem(WorkflowStatusItem): addresses = [] for dest in self.to: try: - dest = self.compute(dest, raises=True) + dest = compute(dest, raises=True) except: continue @@ -2171,13 +2171,13 @@ class SendSMSWorkflowStatusItem(WorkflowStatusItem): if not self.body: return - destinations = [self.compute(x) for x in self.to] + destinations = [compute(x) for x in self.to] destinations = [x for x in destinations if x] # ignore empty elements if not destinations: return try: - sms_body = template_on_formdata(formdata, self.compute(self.body, do_ezt=False)) + sms_body = template_on_formdata(formdata, compute(self.body, do_ezt=False)) except ezt.EZTException: url = formdata.get_url() get_logger().error('error in template for sms [%s], sms could not be generated' % url) -- 2.1.4