From 0f02f62734aa71fa9a500e94011f216b1ac95441 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 12 May 2016 10:06:03 +0200 Subject: [PATCH] misc: introduce a module with some utility function for eval() calls (#10898) --- tests/test_workflows.py | 36 ++++++++++++++++++++++++++++++++++++ wcs/qommon/evalutils.py | 42 ++++++++++++++++++++++++++++++++++++++++++ wcs/qommon/publisher.py | 4 +++- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 wcs/qommon/evalutils.py diff --git a/tests/test_workflows.py b/tests/test_workflows.py index 6576380..a87e2b0 100644 --- a/tests/test_workflows.py +++ b/tests/test_workflows.py @@ -118,6 +118,42 @@ def test_jump_datetime_condition(pub): tomorrow.timetuple()[:3] assert item.must_jump(formdata) is False +def test_jump_date_conditions(pub): + FormDef.wipe() + formdef = FormDef() + formdef.name = 'foobar' + formdef.fields = [DateField(id='2', label='Date', type='date', varname='date')] + formdef.store() + + # create/store/get, to make sure the date format is acceptable + formdata = formdef.data_class()() + formdata.data = {'2': DateField().convert_value_from_str('2015-01-04')} + formdata.store() + formdata = formdef.data_class().get(formdata.id) + + pub.substitutions.feed(formdata) + + item = JumpWorkflowStatusItem() + item.condition = 'utils.make_date(form_var_date) == utils.make_date("2015-01-04")' + assert item.must_jump(formdata) is True + + item = JumpWorkflowStatusItem() + item.condition = 'utils.time_delta(form_var_date, "2015-01-04").days == 0' + assert item.must_jump(formdata) is True + + item = JumpWorkflowStatusItem() + item.condition = 'utils.time_delta(utils.today(), "2015-01-04").days > 0' + assert item.must_jump(formdata) is True + + item = JumpWorkflowStatusItem() + item.condition = 'utils.time_delta(datetime.datetime.now(), "2015-01-04").days > 0' + assert item.must_jump(formdata) is True + + item = JumpWorkflowStatusItem() + item.condition = 'utils.time_delta(utils.time.localtime(), "2015-01-04").days > 0' + assert item.must_jump(formdata) is True + + def test_jump_count_condition(pub): FormDef.wipe() formdef = FormDef() diff --git a/wcs/qommon/evalutils.py b/wcs/qommon/evalutils.py new file mode 100644 index 0000000..645b5d9 --- /dev/null +++ b/wcs/qommon/evalutils.py @@ -0,0 +1,42 @@ +# w.c.s. - web application for online forms +# Copyright (C) 2005-2016 Entr'ouvert +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, see . + +""" +Collection of utility functions to be used in the context of calls to +eval(). They are made available in the "utils" namespace of +get_global_eval_dict. +""" + +import datetime +import time + +from .misc import get_as_datetime + +today = datetime.date.today +now = datetime.datetime.now + +def make_date(date_var): + if isinstance(date_var, datetime.datetime): + return date_var.date() + if isinstance(date_var, datetime.date): + return date_var + if isinstance(date_var, time.struct_time): + return datetime.date(*date_var[:3]) + return get_as_datetime(date_var).date() + + +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 4555b57..52a6940 100644 --- a/wcs/qommon/publisher.py +++ b/wcs/qommon/publisher.py @@ -133,9 +133,11 @@ class QommonPublisher(Publisher): import datetime import re from decimal import Decimal + from . import evalutils as utils return {'datetime': datetime, 'Decimal': Decimal, - 're': re} + 're': re, + 'utils': utils,} def format_publish_error(self, exc): get_response().filter = {} -- 2.8.1