fields: allow prefilling date fields from various date formats (#40513)
This commit is contained in:
parent
95d46c3910
commit
789cf1a53d
|
@ -5663,7 +5663,7 @@ def test_backoffice_cards_import_data_from_csv(pub, studio):
|
|||
b'x,map,data1,Yes,item1,invalid,,test@localhost,plop,1',
|
||||
'text/csv')
|
||||
resp = resp.forms[0].submit()
|
||||
assert 'time data \'invalid\' does not match format \'%Y-%m-%d\'' in resp.text
|
||||
assert 'time data \'invalid\' does not match format' in resp.text
|
||||
|
||||
data = [b'Table,Map,Test,Boolean,List,Date,File,Email,Long,List2']
|
||||
for i in range(1, 150):
|
||||
|
|
|
@ -551,6 +551,7 @@ def test_table_rows():
|
|||
|
||||
def test_date():
|
||||
assert fields.DateField().convert_value_from_str('2015-01-04') is not None
|
||||
assert fields.DateField().convert_value_from_str('04/01/2015') is not None
|
||||
assert fields.DateField().convert_value_from_str('') is None
|
||||
with pytest.raises(ValueError, match="does not match format '%Y-%m-%d'"):
|
||||
with pytest.raises(ValueError, match="does not match format"):
|
||||
fields.DateField().convert_value_from_str('not a date')
|
||||
|
|
|
@ -38,7 +38,7 @@ from django.utils.six.moves.html_parser import HTMLParser
|
|||
from .qommon import _, N_, force_str
|
||||
from .qommon import evalutils
|
||||
from .qommon.form import *
|
||||
from .qommon.misc import localstrftime, strftime, date_format, ellipsize, xml_node_text
|
||||
from .qommon.misc import localstrftime, strftime, date_format, ellipsize, xml_node_text, get_as_datetime
|
||||
from .qommon.template import Template, TemplateError
|
||||
from .qommon import get_cfg, get_logger
|
||||
|
||||
|
@ -1243,7 +1243,7 @@ class DateField(WidgetField):
|
|||
def convert_value_from_str(self, value):
|
||||
if not value:
|
||||
return None
|
||||
return time.strptime(value, date_format())
|
||||
return get_as_datetime(value).timetuple()
|
||||
|
||||
def convert_value_to_str(self, value):
|
||||
if value is None:
|
||||
|
|
|
@ -239,12 +239,13 @@ def get_as_datetime(s):
|
|||
formats.extend(value)
|
||||
for value in DATE_FORMATS.values():
|
||||
formats.extend(value)
|
||||
exception = ValueError()
|
||||
for format_string in formats:
|
||||
try:
|
||||
return datetime.datetime.strptime(s, format_string)
|
||||
except ValueError:
|
||||
pass
|
||||
raise ValueError()
|
||||
except ValueError as e:
|
||||
exception = e
|
||||
raise exception
|
||||
|
||||
|
||||
def site_encode(s):
|
||||
|
|
Loading…
Reference in New Issue