Exemple #1
0
 def compress(self, data_list):
     if data_list:
         # Raise a validation error if time or date is empty
         # (possible if SplitDateTimeField has required=False).
         if data_list[0] in validators.EMPTY_VALUES:
             raise ValidationError(self.error_messages['invalid_date'])
         if data_list[1] in validators.EMPTY_VALUES:
             raise ValidationError(self.error_messages['invalid_time'])
         result = datetime.datetime.combine(*data_list)
         return from_current_timezone(result)
     return None
Exemple #2
0
 def to_python(self, value):
     """
     Validates that the input can be converted to a datetime. Returns a
     Python datetime.datetime object.
     """
     if value in validators.EMPTY_VALUES:
         return None
     if isinstance(value, datetime.datetime):
         return from_current_timezone(value)
     if isinstance(value, datetime.date):
         result = datetime.datetime(value.year, value.month, value.day)
         return from_current_timezone(result)
     if isinstance(value, list):
         # Input comes from a SplitDateTimeWidget, for example. So, it's two
         # components: date and time.
         if len(value) != 2:
             raise ValidationError(self.error_messages['invalid'])
         if value[0] in validators.EMPTY_VALUES and value[1] in validators.EMPTY_VALUES:
             return None
         value = '%s %s' % tuple(value)
     result = super(DateTimeField, self).to_python(value)
     return from_current_timezone(result)