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 |
... | ... | |
119 | 120 |
formdata.store() |
120 | 121 |
pub.substitutions.feed(formdata) |
121 | 122 | |
122 |
item = JumpWorkflowStatusItem() |
|
123 | ||
124 | 123 |
# straight string |
125 |
assert item.compute('blah') == 'blah'
|
|
124 |
assert compute('blah') == 'blah' |
|
126 | 125 | |
127 | 126 |
# ezt string |
128 |
assert item.compute('[form_var_foo]') == 'hello'
|
|
127 |
assert compute('[form_var_foo]') == 'hello' |
|
129 | 128 |
# ezt string, but not ezt asked |
130 |
assert item.compute('[form_var_foo]', do_ezt=False) == '[form_var_foo]'
|
|
129 |
assert compute('[form_var_foo]', do_ezt=False) == '[form_var_foo]' |
|
131 | 130 |
# ezt string, with an error |
132 |
assert item.compute('[end]', raises=False) == '[end]'
|
|
131 |
assert compute('[end]', raises=False) == '[end]' |
|
133 | 132 |
with pytest.raises(Exception): |
134 |
item.compute('[end]', raises=True)
|
|
133 |
compute('[end]', raises=True) |
|
135 | 134 | |
136 | 135 |
# python expression |
137 |
assert item.compute('=form_var_foo') == 'hello'
|
|
136 |
assert compute('=form_var_foo') == 'hello' |
|
138 | 137 |
# python expression, with an error |
139 |
assert item.compute('=1/0', raises=False) == '=1/0'
|
|
138 |
assert compute('=1/0', raises=False) == '=1/0' |
|
140 | 139 |
with pytest.raises(Exception): |
141 |
item.compute('=1/0', raises=True)
|
|
140 |
compute('=1/0', raises=True) |
|
142 | 141 | |
143 | 142 |
def test_jump_nothing(pub): |
144 | 143 |
FormDef.wipe() |
wcs/qommon/misc.py | ||
---|---|---|
503 | 503 |
def parse_isotime(s): |
504 | 504 |
t = time.strptime(s, '%Y-%m-%dT%H:%M:%SZ') |
505 | 505 |
return calendar.timegm(t) |
506 | ||
507 | ||
508 |
def compute(expr, do_ezt=True, raises=False, context=None): |
|
509 |
if not isinstance(expr, basestring): |
|
510 |
return expr |
|
511 | ||
512 |
if not expr.startswith('=') and not do_ezt: |
|
513 |
return expr |
|
514 | ||
515 |
vars = get_publisher().substitutions.get_context_variables() |
|
516 |
vars.update(context or {}) |
|
517 | ||
518 |
if not expr.startswith('='): |
|
519 |
try: |
|
520 |
processor = ezt.Template(compress_whitespace=False) |
|
521 |
processor.parse(expr) |
|
522 |
fd = StringIO() |
|
523 |
processor.generate(fd, vars) |
|
524 |
return fd.getvalue() |
|
525 |
except ezt.EZTException: |
|
526 |
if raises: |
|
527 |
raise |
|
528 |
return expr |
|
529 | ||
530 |
try: |
|
531 |
return eval(expr[1:], get_publisher().get_global_eval_dict(), vars) |
|
532 |
except: |
|
533 |
if raises: |
|
534 |
raise |
|
535 |
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 | ||
---|---|---|
29 | 29 | |
30 | 30 |
from quixote import get_request, redirect |
31 | 31 | |
32 |
from qommon.misc import C_, get_as_datetime |
|
32 |
from qommon.misc import C_, get_as_datetime, compute
|
|
33 | 33 |
from qommon.storage import StorableObject |
34 | 34 |
from qommon.form import * |
35 | 35 |
from qommon.humantime import seconds2humanduration |
... | ... | |
1606 | 1606 |
value = getattr(self, '%s_parse' % f)(value) |
1607 | 1607 |
setattr(self, f, value) |
1608 | 1608 | |
1609 |
@classmethod |
|
1610 |
def compute(cls, var, do_ezt=True, raises=False): |
|
1611 |
if not isinstance(var, basestring): |
|
1612 |
return var |
|
1613 | ||
1614 |
if not var.startswith('=') and not do_ezt: |
|
1615 |
return var |
|
1616 | ||
1617 |
vars = get_publisher().substitutions.get_context_variables() |
|
1618 |
if not var.startswith('='): |
|
1619 |
try: |
|
1620 |
processor = ezt.Template(compress_whitespace=False) |
|
1621 |
processor.parse(var) |
|
1622 |
fd = StringIO() |
|
1623 |
processor.generate(fd, vars) |
|
1624 |
return fd.getvalue() |
|
1625 |
except ezt.EZTException: |
|
1626 |
if raises: |
|
1627 |
raise |
|
1628 |
return var |
|
1629 | ||
1630 |
try: |
|
1631 |
return eval(var[1:], get_publisher().get_global_eval_dict(), vars) |
|
1632 |
except: |
|
1633 |
if raises: |
|
1634 |
raise |
|
1635 |
return var |
|
1636 | ||
1637 | 1609 |
def get_substitution_variables(self, formdata): |
1638 | 1610 |
return {} |
1639 | 1611 | |
... | ... | |
1883 | 1855 |
return _('Change Status (not completed)') |
1884 | 1856 | |
1885 | 1857 |
def fill_form(self, form, formdata, user): |
1886 |
form.add_submit('button%s' % self.id, self.compute(self.label))
|
|
1858 |
form.add_submit('button%s' % self.id, compute(self.label)) |
|
1887 | 1859 |
if self.require_confirmation: |
1888 | 1860 |
get_response().add_javascript(['jquery.js', '../../i18n.js', 'qommon.js']) |
1889 | 1861 |
widget = form.get_widget('button%s' % self.id) |
... | ... | |
2032 | 2004 | |
2033 | 2005 |
url = formdata.get_url() |
2034 | 2006 |
try: |
2035 |
mail_body = template_on_formdata(formdata, self.compute(self.body, do_ezt=False))
|
|
2007 |
mail_body = template_on_formdata(formdata, compute(self.body, do_ezt=False)) |
|
2036 | 2008 |
except ezt.EZTException: |
2037 | 2009 |
get_logger().error('error in template for email body [%s], mail could not be generated' % url) |
2038 | 2010 |
return |
2039 | 2011 | |
2040 | 2012 |
try: |
2041 |
mail_subject = template_on_formdata(formdata, self.compute(self.subject, do_ezt=False))
|
|
2013 |
mail_subject = template_on_formdata(formdata, compute(self.subject, do_ezt=False)) |
|
2042 | 2014 |
except ezt.EZTException: |
2043 | 2015 |
get_logger().error('error in template for email subject [%s], mail could not be generated' % url) |
2044 | 2016 |
return |
... | ... | |
2054 | 2026 |
addresses = [] |
2055 | 2027 |
for dest in self.to: |
2056 | 2028 |
try: |
2057 |
dest = self.compute(dest, raises=True)
|
|
2029 |
dest = compute(dest, raises=True) |
|
2058 | 2030 |
except: |
2059 | 2031 |
continue |
2060 | 2032 | |
... | ... | |
2189 | 2161 |
if not self.body: |
2190 | 2162 |
return |
2191 | 2163 | |
2192 |
destinations = [self.compute(x) for x in self.to]
|
|
2164 |
destinations = [compute(x) for x in self.to] |
|
2193 | 2165 |
destinations = [x for x in destinations if x] # ignore empty elements |
2194 | 2166 |
if not destinations: |
2195 | 2167 |
return |
2196 | 2168 | |
2197 | 2169 |
try: |
2198 |
sms_body = template_on_formdata(formdata, self.compute(self.body, do_ezt=False))
|
|
2170 |
sms_body = template_on_formdata(formdata, compute(self.body, do_ezt=False)) |
|
2199 | 2171 |
except ezt.EZTException: |
2200 | 2172 |
url = formdata.get_url() |
2201 | 2173 |
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) |
... | ... | |
58 | 58 |
qs = list(urlparse.parse_qsl(parsed.query)) |
59 | 59 |
for key, value in qs_data.iteritems(): |
60 | 60 |
try: |
61 |
value = WorkflowStatusItem.compute(value, raises=True)
|
|
61 |
value = compute(value, raises=True) |
|
62 | 62 |
value = str(value) |
63 | 63 |
except: |
64 | 64 |
get_publisher().notify_of_exception(sys.exc_info()) |
... | ... | |
70 | 70 |
url = urlparse.urlunparse(parsed[:4] + (qs,) + parsed[5:6]) |
71 | 71 | |
72 | 72 |
if request_signature_key: |
73 |
signature_key = WorkflowStatusItem.compute(request_signature_key)
|
|
73 |
signature_key = compute(request_signature_key) |
|
74 | 74 |
if signature_key: |
75 | 75 |
url = sign_url(url, signature_key) |
76 | 76 | |
... | ... | |
82 | 82 |
payload = {} |
83 | 83 |
for (key, value) in post_data.items(): |
84 | 84 |
try: |
85 |
payload[key] = WorkflowStatusItem.compute(value, raises=True)
|
|
85 |
payload[key] = compute(value, raises=True) |
|
86 | 86 |
except: |
87 | 87 |
get_publisher().notify_of_exception(sys.exc_info()) |
88 | 88 | |
89 |
- |