fields: make it possible to prefill with a date object (#13787)
This commit is contained in:
parent
e70246d50e
commit
b5eb00cd1d
|
@ -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)
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in New Issue