0001-factorize-WorklowStatusItem.compute.patch
tests/test_workflows.py | ||
---|---|---|
11 | 11 |
from quixote import cleanup, get_response |
12 | 12 |
from wcs.qommon.http_request import HTTPRequest |
13 | 13 |
from qommon.form import * |
14 |
from qommon.misc import compute |
|
14 | 15 | |
15 | 16 |
from wcs.formdef import FormDef |
16 | 17 |
from wcs import sessions |
... | ... | |
120 | 121 |
formdata.store() |
121 | 122 |
pub.substitutions.feed(formdata) |
122 | 123 | |
123 |
item = JumpWorkflowStatusItem() |
|
124 | ||
125 | 124 |
# straight string |
126 |
assert item.compute('blah') == 'blah'
|
|
125 |
assert compute('blah') == 'blah' |
|
127 | 126 | |
128 | 127 |
# ezt string |
129 |
assert item.compute('[form_var_foo]') == 'hello'
|
|
128 |
assert compute('[form_var_foo]') == 'hello' |
|
130 | 129 |
# ezt string, but not ezt asked |
131 |
assert item.compute('[form_var_foo]', do_ezt=False) == '[form_var_foo]'
|
|
130 |
assert compute('[form_var_foo]', do_ezt=False) == '[form_var_foo]' |
|
132 | 131 |
# ezt string, with an error |
133 |
assert item.compute('[end]', raises=False) == '[end]'
|
|
132 |
assert compute('[end]', raises=False) == '[end]' |
|
134 | 133 |
with pytest.raises(Exception): |
135 |
item.compute('[end]', raises=True)
|
|
134 |
compute('[end]', raises=True) |
|
136 | 135 | |
137 | 136 |
# python expression |
138 |
assert item.compute('=form_var_foo') == 'hello'
|
|
137 |
assert compute('=form_var_foo') == 'hello' |
|
139 | 138 |
# python expression, with an error |
140 |
assert item.compute('=1/0', raises=False) == '=1/0'
|
|
139 |
assert compute('=1/0', raises=False) == '=1/0' |
|
141 | 140 |
with pytest.raises(Exception): |
142 |
item.compute('=1/0', raises=True)
|
|
141 |
compute('=1/0', raises=True) |
|
143 | 142 | |
144 | 143 |
def test_jump_nothing(pub): |
145 | 144 |
FormDef.wipe() |
wcs/qommon/misc.py | ||
---|---|---|
519 | 519 |
for chunk in iter(read_chunk, ''): |
520 | 520 |
digest.update(chunk) |
521 | 521 |
return digest.hexdigest() |
522 | ||
523 | ||
524 |
def compute(expr, do_ezt=True, raises=False, context=None): |
|
525 |
if not isinstance(expr, basestring): |
|
526 |
return expr |
|
527 | ||
528 |
if not expr.startswith('=') and not do_ezt: |
|
529 |
return expr |
|
530 | ||
531 |
vars = get_publisher().substitutions.get_context_variables() |
|
532 |
vars.update(context or {}) |
|
533 | ||
534 |
if not expr.startswith('='): |
|
535 |
try: |
|
536 |
processor = ezt.Template(compress_whitespace=False) |
|
537 |
processor.parse(expr) |
|
538 |
fd = StringIO() |
|
539 |
processor.generate(fd, vars) |
|
540 |
return fd.getvalue() |
|
541 |
except ezt.EZTException: |
|
542 |
if raises: |
|
543 |
raise |
|
544 |
return expr |
|
545 | ||
546 |
try: |
|
547 |
return eval(expr[1:], get_publisher().get_global_eval_dict(), vars) |
|
548 |
except: |
|
549 |
if raises: |
|
550 |
raise |
|
551 |
return expr |
wcs/wf/backoffice_fields.py | ||
---|---|---|
21 | 21 |
from quixote import get_publisher |
22 | 22 | |
23 | 23 |
from qommon import get_logger |
24 |
from qommon.misc import compute |
|
24 | 25 |
from qommon.form import (WidgetListAsTable, CompositeWidget, SingleSelectWidget, |
25 | 26 |
ComputedExpressionWidget, PicklableUpload) |
26 | 27 |
from wcs.fields import WidgetField |
... | ... | |
121 | 122 |
continue |
122 | 123 | |
123 | 124 |
try: |
124 |
new_value = self.compute(field['value'], raises=True)
|
|
125 |
new_value = compute(field['value'], raises=True) |
|
125 | 126 |
except: |
126 | 127 |
get_publisher().notify_of_exception(sys.exc_info()) |
127 | 128 |
continue |
wcs/wf/geolocate.py | ||
---|---|---|
28 | 28 | |
29 | 29 |
from qommon import get_logger |
30 | 30 |
from qommon.form import RadiobuttonsWidget, ComputedExpressionWidget, CheckboxWidget |
31 |
from qommon.misc import http_get_page |
|
31 |
from qommon.misc import http_get_page, compute
|
|
32 | 32 |
from wcs.workflows import WorkflowStatusItem, register_item_class |
33 | 33 | |
34 | 34 |
class GeolocateWorkflowStatusItem(WorkflowStatusItem): |
... | ... | |
106 | 106 |
nominatim_url = 'http://nominatim.openstreetmap.org' |
107 | 107 | |
108 | 108 |
try: |
109 |
address = self.compute(self.address_string, raises=True)
|
|
109 |
address = compute(self.address_string, raises=True) |
|
110 | 110 |
except Exception, e: |
111 | 111 |
get_logger().error('error in template for address string [%r]', e) |
112 | 112 |
return |
... | ... | |
128 | 128 |
return {'lon': float(coords['lon']), 'lat': float(coords['lat'])} |
129 | 129 | |
130 | 130 |
def geolocate_map_variable(self, formdata): |
131 |
value = self.compute(self.map_variable)
|
|
131 |
value = compute(self.map_variable) |
|
132 | 132 |
if not value: |
133 | 133 |
return |
134 | 134 | |
... | ... | |
145 | 145 |
get_logger().error('error geolocating from file (missing PIL)') |
146 | 146 |
return |
147 | 147 | |
148 |
value = self.compute(self.photo_variable)
|
|
148 |
value = compute(self.photo_variable) |
|
149 | 149 |
if not hasattr(value, 'get_file_pointer'): |
150 | 150 |
get_logger().error('error geolocating from photo, invalid variable') |
151 | 151 |
return |
wcs/wf/jump.py | ||
---|---|---|
26 | 26 |
from qommon import errors |
27 | 27 |
from qommon.publisher import get_publisher_class |
28 | 28 |
from qommon.cron import CronJob |
29 |
from qommon.misc import compute |
|
29 | 30 | |
30 | 31 |
from wcs.workflows import Workflow, WorkflowStatusJumpItem, register_item_class |
31 | 32 |
from wcs.api import get_user_from_api_query_string, is_url_signed |
... | ... | |
214 | 215 |
must_jump = must_jump and triggered |
215 | 216 | |
216 | 217 |
if self.timeout: |
217 |
timeout = int(self.compute(self.timeout))
|
|
218 |
timeout = int(compute(self.timeout)) |
|
218 | 219 |
if formdata.evolution: |
219 | 220 |
last = formdata.evolution[-1].time |
220 | 221 |
else: |
wcs/wf/profile.py | ||
---|---|---|
23 | 23 |
from qommon.form import (CompositeWidget, SingleSelectWidget, |
24 | 24 |
WidgetListAsTable, ComputedExpressionWidget) |
25 | 25 |
from qommon.ident.idp import is_idp_managing_user_attributes |
26 |
from qommon.misc import http_patch_request |
|
26 |
from qommon.misc import http_patch_request, compute
|
|
27 | 27 |
from qommon.publisher import get_cfg, get_logger |
28 | 28 | |
29 | 29 |
from wcs.api_utils import sign_url, get_secret_and_orig, MissingSecret |
... | ... | |
144 | 144 |
get_publisher().substitutions.feed(formdata) |
145 | 145 |
new_data = {} |
146 | 146 |
for field in self.fields: |
147 |
new_data[field.get('field_id')] = self.compute(field.get('value'))
|
|
147 |
new_data[field.get('field_id')] = compute(field.get('value')) |
|
148 | 148 | |
149 | 149 |
user_formdef = get_publisher().user_class.get_formdef() |
150 | 150 |
new_user_data = {} |
wcs/workflows.py | ||
---|---|---|
30 | 30 | |
31 | 31 |
from quixote import get_request, redirect |
32 | 32 | |
33 |
from qommon.misc import C_, get_as_datetime, file_digest |
|
33 |
from qommon.misc import C_, get_as_datetime, file_digest, compute
|
|
34 | 34 |
from qommon.storage import StorableObject, atomic_write |
35 | 35 |
from qommon.form import * |
36 | 36 |
from qommon.humantime import seconds2humanduration |
... | ... | |
1602 | 1602 |
value = getattr(self, '%s_parse' % f)(value) |
1603 | 1603 |
setattr(self, f, value) |
1604 | 1604 | |
1605 |
@classmethod |
|
1606 |
def compute(cls, var, do_ezt=True, raises=False): |
|
1607 |
if not isinstance(var, basestring): |
|
1608 |
return var |
|
1609 | ||
1610 |
if not var.startswith('=') and not do_ezt: |
|
1611 |
return var |
|
1612 | ||
1613 |
vars = get_publisher().substitutions.get_context_variables() |
|
1614 |
if not var.startswith('='): |
|
1615 |
try: |
|
1616 |
processor = ezt.Template(compress_whitespace=False) |
|
1617 |
processor.parse(var) |
|
1618 |
fd = StringIO() |
|
1619 |
processor.generate(fd, vars) |
|
1620 |
return fd.getvalue() |
|
1621 |
except ezt.EZTException: |
|
1622 |
if raises: |
|
1623 |
raise |
|
1624 |
return var |
|
1625 | ||
1626 |
try: |
|
1627 |
return eval(var[1:], get_publisher().get_global_eval_dict(), vars) |
|
1628 |
except: |
|
1629 |
if raises: |
|
1630 |
raise |
|
1631 |
return var |
|
1632 | ||
1633 | 1605 |
def get_substitution_variables(self, formdata): |
1634 | 1606 |
return {} |
1635 | 1607 | |
... | ... | |
1879 | 1851 |
return _('Change Status (not completed)') |
1880 | 1852 | |
1881 | 1853 |
def fill_form(self, form, formdata, user): |
1882 |
form.add_submit('button%s' % self.id, self.compute(self.label))
|
|
1854 |
form.add_submit('button%s' % self.id, compute(self.label)) |
|
1883 | 1855 |
if self.require_confirmation: |
1884 | 1856 |
get_response().add_javascript(['jquery.js', '../../i18n.js', 'qommon.js']) |
1885 | 1857 |
widget = form.get_widget('button%s' % self.id) |
... | ... | |
2028 | 2000 | |
2029 | 2001 |
url = formdata.get_url() |
2030 | 2002 |
try: |
2031 |
mail_body = template_on_formdata(formdata, self.compute(self.body, do_ezt=False))
|
|
2003 |
mail_body = template_on_formdata(formdata, compute(self.body, do_ezt=False)) |
|
2032 | 2004 |
except ezt.EZTException: |
2033 | 2005 |
get_logger().error('error in template for email body [%s], mail could not be generated' % url) |
2034 | 2006 |
return |
2035 | 2007 | |
2036 | 2008 |
try: |
2037 |
mail_subject = template_on_formdata(formdata, self.compute(self.subject, do_ezt=False))
|
|
2009 |
mail_subject = template_on_formdata(formdata, compute(self.subject, do_ezt=False)) |
|
2038 | 2010 |
except ezt.EZTException: |
2039 | 2011 |
get_logger().error('error in template for email subject [%s], mail could not be generated' % url) |
2040 | 2012 |
return |
... | ... | |
2050 | 2022 |
addresses = [] |
2051 | 2023 |
for dest in self.to: |
2052 | 2024 |
try: |
2053 |
dest = self.compute(dest, raises=True)
|
|
2025 |
dest = compute(dest, raises=True) |
|
2054 | 2026 |
except: |
2055 | 2027 |
continue |
2056 | 2028 | |
... | ... | |
2185 | 2157 |
if not self.body: |
2186 | 2158 |
return |
2187 | 2159 | |
2188 |
destinations = [self.compute(x) for x in self.to]
|
|
2160 |
destinations = [compute(x) for x in self.to] |
|
2189 | 2161 |
destinations = [x for x in destinations if x] # ignore empty elements |
2190 | 2162 |
if not destinations: |
2191 | 2163 |
return |
2192 | 2164 | |
2193 | 2165 |
try: |
2194 |
sms_body = template_on_formdata(formdata, self.compute(self.body, do_ezt=False))
|
|
2166 |
sms_body = template_on_formdata(formdata, compute(self.body, do_ezt=False)) |
|
2195 | 2167 |
except ezt.EZTException: |
2196 | 2168 |
url = formdata.get_url() |
2197 | 2169 |
get_logger().error('error in template for sms [%s], sms could not be generated' % url) |
wcs/wscalls.py | ||
---|---|---|
24 | 24 |
from quixote import get_publisher |
25 | 25 | |
26 | 26 |
from qommon.misc import (simplify, http_get_page, http_post_request, |
27 |
get_variadic_url, JSONEncoder, json_loads) |
|
27 |
get_variadic_url, JSONEncoder, json_loads, compute)
|
|
28 | 28 |
from qommon.xml_storage import XmlStorableObject |
29 | 29 |
from qommon.form import (CompositeWidget, StringWidget, WidgetDict, |
30 | 30 |
ComputedExpressionWidget, RadiobuttonsWidget, CheckboxWidget) |
... | ... | |
59 | 59 |
qs = list(urlparse.parse_qsl(parsed.query)) |
60 | 60 |
for key, value in qs_data.iteritems(): |
61 | 61 |
try: |
62 |
value = WorkflowStatusItem.compute(value, raises=True)
|
|
62 |
value = compute(value, raises=True) |
|
63 | 63 |
value = str(value) |
64 | 64 |
except: |
65 | 65 |
get_publisher().notify_of_exception(sys.exc_info()) |
... | ... | |
71 | 71 |
url = urlparse.urlunparse(parsed[:4] + (qs,) + parsed[5:6]) |
72 | 72 | |
73 | 73 |
if request_signature_key: |
74 |
signature_key = WorkflowStatusItem.compute(request_signature_key)
|
|
74 |
signature_key = compute(request_signature_key) |
|
75 | 75 |
if signature_key: |
76 | 76 |
url = sign_url(url, signature_key) |
77 | 77 | |
... | ... | |
83 | 83 |
payload = {} |
84 | 84 |
for (key, value) in post_data.items(): |
85 | 85 |
try: |
86 |
payload[key] = WorkflowStatusItem.compute(value, raises=True)
|
|
86 |
payload[key] = compute(value, raises=True) |
|
87 | 87 |
except: |
88 | 88 |
get_publisher().notify_of_exception(sys.exc_info()) |
89 | 89 | |
90 |
- |