[IMP] fields.Date[Time]: convert_to_cache: avoid costly parsing when not validating
The extra parsing check is not necessary when we're not validating inputs, because in that case the values come from the database and are valid. The validation is quite expensive due to calls to strptime() + strftime().
This commit is contained in:
+13
-4
@@ -1041,8 +1041,11 @@ class Date(Field):
|
||||
if not value:
|
||||
return False
|
||||
if isinstance(value, basestring):
|
||||
value = self.from_string(value)
|
||||
return value.strftime(DATE_FORMAT)
|
||||
if validate:
|
||||
# force parsing for validation
|
||||
self.from_string(value)
|
||||
return value[:DATE_LENGTH]
|
||||
return self.to_string(value)
|
||||
|
||||
def convert_to_export(self, value, env):
|
||||
if value and env.context.get('export_raw_data'):
|
||||
@@ -1106,8 +1109,14 @@ class Datetime(Field):
|
||||
if not value:
|
||||
return False
|
||||
if isinstance(value, basestring):
|
||||
value = self.from_string(value)
|
||||
return value.strftime(DATETIME_FORMAT)
|
||||
if validate:
|
||||
# force parsing for validation
|
||||
self.from_string(value)
|
||||
value = value[:DATETIME_LENGTH]
|
||||
if len(value) == DATE_LENGTH:
|
||||
value += " 00:00:00"
|
||||
return value
|
||||
return self.to_string(value)
|
||||
|
||||
def convert_to_export(self, value, env):
|
||||
if value and env.context.get('export_raw_data'):
|
||||
|
||||
Reference in New Issue
Block a user