From a30a55fe1257af6933f0ed0f3a1e607c27dd2b62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Mon, 17 Jul 2017 13:00:56 +0200 Subject: [PATCH] misc: expose date() and days() as global functions (#17658) --- tests/test_workflows.py | 18 ++++++++++++++++++ wcs/qommon/evalutils.py | 8 ++++++++ wcs/qommon/publisher.py | 2 ++ 3 files changed, 28 insertions(+) diff --git a/tests/test_workflows.py b/tests/test_workflows.py index bb23c8ed..de9391d6 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -149,6 +149,24 @@ def test_variable_compute(pub): assert item.compute('[form_var_foo] [bar]', context={'bar': 'world'}) == 'hello world' assert item.compute('=form_var_foo + " " + bar', context={'bar': 'world'}) == 'hello world' +def test_variable_compute_dates(pub): + FormDef.wipe() + formdef = FormDef() + formdef.name = 'foobar' + formdef.fields = [StringField(id='1', label='Test', type='string', varname='foo'),] + formdef.store() + + formdata = formdef.data_class()() + formdata.data = {'1': '2017-07-17'} + formdata.store() + pub.substitutions.feed(formdata) + + item = JumpWorkflowStatusItem() + + assert item.compute('=date(form_var_foo)') == datetime.date(2017, 7, 17) + assert item.compute('=date(form_var_foo) + days(1)') == datetime.date(2017, 7, 18) + assert item.compute('=date(2017, 7, 18)') == datetime.date(2017, 7, 18) + def test_jump_nothing(pub): FormDef.wipe() formdef = FormDef() diff --git a/wcs/qommon/evalutils.py b/wcs/qommon/evalutils.py index 248ae03e..fdb65764 100644 --- a/wcs/qommon/evalutils.py +++ b/wcs/qommon/evalutils.py @@ -55,6 +55,14 @@ def make_datetime(datetime_var): return get_as_datetime(datetime_var) raise ValueError('invalid datetime value: %s' % datetime_var) +def date(var, month=None, day=None): + if var and month and day: + return datetime.date(int(var), int(month), int(day)) + return make_date(var) + +def days(count): + return datetime.timedelta(days=int(count)) + def time_delta(t1, t2): return make_date(t1) - make_date(t2) diff --git a/wcs/qommon/publisher.py b/wcs/qommon/publisher.py index d6b25285..23bddbfa 100644 --- a/wcs/qommon/publisher.py +++ b/wcs/qommon/publisher.py @@ -146,6 +146,8 @@ class QommonPublisher(Publisher, object): 'Decimal': Decimal, 'random': random.SystemRandom(), 're': re, + 'date': utils.date, + 'days': utils.days, 'utils': utils,} def format_publish_error(self, exc): -- 2.13.2