def render(self, name, value, attrs=None): try: year_val, month_val = value.year, value.month except AttributeError: year_val = month_val = None if isinstance(value, basestring): if settings.USE_L10N: try: input_format = get_format('DATE_INPUT_FORMATS')[0] # Python 2.4 compatibility: # v = datetime.datetime.strptime(value, input_format) # would be clearer, but datetime.strptime was added in # Python 2.5 v = datetime.datetime(*(time.strptime(value, input_format)[0:6])) year_val, month_val = v.year, v.month except ValueError: pass else: match = RE_DATE.match(value) if match: year_val, month_val, day_val = [int(v) for v in match.groups()] choices = [(i, i) for i in self.years] year_html = self.create_select(name, self.year_field, value, year_val, choices, none_value=(0, _('Year'))) choices = [(i, '%02d' % i) for i in range(1,13)] month_html = self.create_select(name, self.month_field, value, month_val, choices, none_value=(0, _('Month'))) output = [] for field in _parse_date_fmt(): if field == 'year': output.append(year_html) elif field == 'month': output.append(month_html) return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None): try: year_val, month_val, day_val = value.year, value.month, value.day except AttributeError: year_val = month_val = day_val = None if isinstance(value, six.string_types): if settings.USE_L10N: try: input_format = get_format('DATE_INPUT_FORMATS')[0] v = datetime.datetime.strptime(force_str(value), input_format) year_val, month_val, day_val = v.year, v.month, v.day except ValueError: pass else: match = RE_DATE.match(value) if match: year_val, month_val, day_val = [ int(v) for v in match.groups() ] html = {} choices = [(i, i) for i in self.years] html['year'] = self.create_select(name, self.year_field, value, year_val, choices, self.year_none_value) choices = list(six.iteritems(self.months)) html['month'] = self.create_select(name, self.month_field, value, month_val, choices, self.month_none_value) choices = [(i, i) for i in range(1, 32)] html['day'] = self.create_select(name, self.day_field, value, day_val, choices, self.day_none_value) output = [] for field in _parse_date_fmt(): if self.ignore_day and field == 'day': output.append( '<span class="select-date-wrapper hidden">%s</span>' % html[field]) else: output.append('<span class="select-date-wrapper">%s</span>' % html[field]) return mark_safe('\n'.join(output))
def render(self, name, value, attrs=None): try: year_val, month_val, day_val = value.year, value.month, value.day except AttributeError: year_val = month_val = day_val = None if isinstance(value, basestring): match = RE_DATE.match(value) if match: year_val, month_val, day_val = [ int(v) for v in match.groups() ] output = [] if 'id' in self.attrs: id_ = self.attrs['id'] else: id_ = 'id_%s' % name day_choices = [(i, i) for i in range(1, 32)] local_attrs = self.build_attrs(id=self.day_field % id_) select_html = Select(choices=day_choices).render( self.day_field % name, day_val, local_attrs) output.append(select_html) month_choices = MONTHS.items() month_choices.sort() local_attrs['id'] = self.month_field % id_ select_html = Select(choices=month_choices).render( self.month_field % name, month_val, local_attrs) output.append(select_html) year_choices = [(i, i) for i in self.years] local_attrs['id'] = self.year_field % id_ if self.reverse_years: year_choices.reverse() select_html = Select(choices=year_choices).render( self.year_field % name, year_val, local_attrs) output.append(select_html) return mark_safe(u'\n'.join(output))
def render(self, name, value, attrs=None): try: year_val, month_val, day_val = value.year, value.month, value.day except AttributeError: year_val = month_val = day_val = None if isinstance(value, basestring): match = RE_DATE.match(value) if match: year_val, month_val, day_val = [int(v) for v in match.groups()] output = [] if "id" in self.attrs: id_ = self.attrs["id"] else: id_ = "id_%s" % name day_choices = [(i, i) for i in range(1, 32)] local_attrs = self.build_attrs(id=self.day_field % id_) select_html = Select(choices=day_choices).render(self.day_field % name, day_val, local_attrs) output.append(select_html) month_choices = MONTHS.items() month_choices.sort() local_attrs["id"] = self.month_field % id_ select_html = Select(choices=month_choices).render(self.month_field % name, month_val, local_attrs) output.append(select_html) year_choices = [(i, i) for i in self.years] local_attrs["id"] = self.year_field % id_ if self.reverse_years: year_choices.reverse() select_html = Select(choices=year_choices).render(self.year_field % name, year_val, local_attrs) output.append(select_html) return mark_safe(u"\n".join(output))