misc: expose date() and days() as global functions (#17658)
This commit is contained in:
parent
6629cbc156
commit
a97c2ebbea
|
@ -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()
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -145,6 +145,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):
|
||||
|
|
Loading…
Reference in New Issue