Пример #1
0
 def _format_value(self, value):
     if self.is_localized and not self.manual_format:
         return formats.localize_input(value)
     elif hasattr(value, 'strftime'):
         value = datetime_safe.new_date(value)
         return value.strftime(self.format)
     return value
Пример #2
0
 def _format_value(self, value):
     if self.is_localized and not self.manual_format:
         return formats.localize_input(value)
     elif hasattr(value, 'strftime'):
         value = datetime_safe.new_date(value)
         return value.strftime(self.format)
     return value
Пример #3
0
 def value_from_datadict(self, data, files, name):
     y = data.get(self.year_field % name)
     m = data.get(self.month_field % name)
     d = data.get(self.day_field % name)
     if y == m == d == "0":
         return None
     if y and m and d:
         if settings.USE_L10N:
             input_format = get_format('DATE_INPUT_FORMATS')[0]
             try:
                 date_value = datetime.date(int(y), int(m), int(d))
             except ValueError:
                 return '%s-%s-%s' % (y, m, d)
             else:
                 date_value = datetime_safe.new_date(date_value)
                 return date_value.strftime(input_format)
         else:
             return '%s-%s-%s' % (y, m, d)
     return data.get(name, None)
Пример #4
0
def localize_input(value, default=None):
    """
    Checks if an input value is a localizable type and returns it
    formatted with the appropriate formatting string of the current locale.
    """
    if isinstance(value, (decimal.Decimal, float, int, long)):
        return number_format(value)
    elif isinstance(value, datetime.datetime):
        value = datetime_safe.new_datetime(value)
        format = smart_str(default or get_format('DATETIME_INPUT_FORMATS')[0])
        return value.strftime(format)
    elif isinstance(value, datetime.date):
        value = datetime_safe.new_date(value)
        format = smart_str(default or get_format('DATE_INPUT_FORMATS')[0])
        return value.strftime(format)
    elif isinstance(value, datetime.time):
        format = smart_str(default or get_format('TIME_INPUT_FORMATS')[0])
        return value.strftime(format)
    return value