Projet

Général

Profil

0001-misc-introduce-a-module-with-some-utility-function-f.patch

Frédéric Péters, 12 mai 2016 10:07

Télécharger (4,37 ko)

Voir les différences:

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
tests/test_workflows.py
118 118
            tomorrow.timetuple()[:3]
119 119
    assert item.must_jump(formdata) is False
120 120

  
121
def test_jump_date_conditions(pub):
122
    FormDef.wipe()
123
    formdef = FormDef()
124
    formdef.name = 'foobar'
125
    formdef.fields = [DateField(id='2', label='Date', type='date', varname='date')]
126
    formdef.store()
127

  
128
    # create/store/get, to make sure the date format is acceptable
129
    formdata = formdef.data_class()()
130
    formdata.data = {'2': DateField().convert_value_from_str('2015-01-04')}
131
    formdata.store()
132
    formdata = formdef.data_class().get(formdata.id)
133

  
134
    pub.substitutions.feed(formdata)
135

  
136
    item = JumpWorkflowStatusItem()
137
    item.condition = 'utils.make_date(form_var_date) == utils.make_date("2015-01-04")'
138
    assert item.must_jump(formdata) is True
139

  
140
    item = JumpWorkflowStatusItem()
141
    item.condition = 'utils.time_delta(form_var_date, "2015-01-04").days == 0'
142
    assert item.must_jump(formdata) is True
143

  
144
    item = JumpWorkflowStatusItem()
145
    item.condition = 'utils.time_delta(utils.today(), "2015-01-04").days > 0'
146
    assert item.must_jump(formdata) is True
147

  
148
    item = JumpWorkflowStatusItem()
149
    item.condition = 'utils.time_delta(datetime.datetime.now(), "2015-01-04").days > 0'
150
    assert item.must_jump(formdata) is True
151

  
152
    item = JumpWorkflowStatusItem()
153
    item.condition = 'utils.time_delta(utils.time.localtime(), "2015-01-04").days > 0'
154
    assert item.must_jump(formdata) is True
155

  
156

  
121 157
def test_jump_count_condition(pub):
122 158
    FormDef.wipe()
123 159
    formdef = FormDef()
wcs/qommon/evalutils.py
1
# w.c.s. - web application for online forms
2
# Copyright (C) 2005-2016  Entr'ouvert
3
#
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
8
#
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
# GNU General Public License for more details.
13
#
14
# You should have received a copy of the GNU General Public License
15
# along with this program; if not, see <http://www.gnu.org/licenses/>.
16

  
17
"""
18
Collection of utility functions to be used in the context of calls to
19
eval(). They are made available in the "utils" namespace of
20
get_global_eval_dict.
21
"""
22

  
23
import datetime
24
import time
25

  
26
from .misc import get_as_datetime
27

  
28
today = datetime.date.today
29
now = datetime.datetime.now
30

  
31
def make_date(date_var):
32
    if isinstance(date_var, datetime.datetime):
33
        return date_var.date()
34
    if isinstance(date_var, datetime.date):
35
        return date_var
36
    if isinstance(date_var, time.struct_time):
37
        return datetime.date(*date_var[:3])
38
    return get_as_datetime(date_var).date()
39

  
40

  
41
def time_delta(t1, t2):
42
    return make_date(t1) - make_date(t2)
wcs/qommon/publisher.py
133 133
        import datetime
134 134
        import re
135 135
        from decimal import Decimal
136
        from . import evalutils as utils
136 137
        return {'datetime': datetime,
137 138
                'Decimal': Decimal,
138
                're': re}
139
                're': re,
140
                'utils': utils,}
139 141

  
140 142
    def format_publish_error(self, exc):
141 143
        get_response().filter = {}
142
-