fields: make it possible to prefill with a date object (#13787)

This commit is contained in:
Frédéric Péters 2016-10-27 19:24:28 +02:00
parent e70246d50e
commit b5eb00cd1d
2 changed files with 14 additions and 0 deletions

View File

@ -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)

View File

@ -14,6 +14,7 @@
# You should have received a copy of the GNU General Public License
# along with this program; if not, see <http://www.gnu.org/licenses/>.
import datetime
import json
import time
import random
@ -301,6 +302,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: