From fa7be0d75c0f1535298e5eec4c1084f205d3a4a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20P=C3=A9ters?= Date: Thu, 27 Oct 2016 19:24:28 +0200 Subject: [PATCH] fields: make it possible to prefill with a date object (#13787) --- tests/test_prefill.py | 11 +++++++++++ wcs/fields.py | 3 +++ 2 files changed, 14 insertions(+) diff --git a/tests/test_prefill.py b/tests/test_prefill.py index c0c720a..3257ad3 100644 --- a/tests/test_prefill.py +++ b/tests/test_prefill.py @@ -1,3 +1,4 @@ +import datetime import sys import shutil @@ -86,3 +87,13 @@ def test_prefill_formula_substitution_variable(): field = fields.Field() field.prefill = {'type': 'formula', 'value': 'test'} assert field.get_prefill_value() == ('value', False) + +def test_prefill_formula_date_value(): + pub.substitutions.get_context_variables = lambda: {} + field = fields.Field() + field.prefill = {'type': 'formula', 'value': 'utils.add_days(utils.today(), 10)'} + in_ten_days = (datetime.date.today() + datetime.timedelta(days=10)).strftime('%Y-%m-%d') + assert field.get_prefill_value() == (in_ten_days, False) + + field.prefill = {'type': 'formula', 'value': 'utils.add_days("2016-01-01", 10)'} + assert field.get_prefill_value() == ('2016-01-11', False) diff --git a/wcs/fields.py b/wcs/fields.py index 857b694..37614a4 100644 --- a/wcs/fields.py +++ b/wcs/fields.py @@ -14,6 +14,7 @@ # You should have received a copy of the GNU General Public License # along with this program; if not, see . +import datetime import json import time import random @@ -302,6 +303,8 @@ class Field(object): ret = eval(formula, get_publisher().get_global_eval_dict(), get_publisher().substitutions.get_context_variables()) + if isinstance(ret, datetime.date): + ret = ret.strftime(date_format()) if ret: return (str(ret), False) except: -- 2.10.1