def values(self): """ Returns a list of values for this field for this instance. It's a list so we can accomodate many-to-many fields. """ # This import is deliberately inside the function because it causes # some settings to be imported, and we don't want to do that at the # module level. if self.field.rel: if isinstance(self.field.rel, models.ManyToOneRel): objs = getattr(self.instance.instance, self.field.name) elif isinstance(self.field.rel, models.ManyToManyRel): # ManyToManyRel return list(getattr(self.instance.instance, self.field.name).all()) elif self.field.choices: objs = dict(self.field.choices).get(self.raw_value, EMPTY_VALUE) elif isinstance(self.field, models.DateField) or isinstance(self.field, models.TimeField): if self.raw_value: if isinstance(self.field, models.DateTimeField): objs = capfirst(formats.date_format(self.raw_value, 'DATETIME_FORMAT')) elif isinstance(self.field, models.TimeField): objs = capfirst(formats.time_format(self.raw_value, 'TIME_FORMAT')) else: objs = capfirst(formats.date_format(self.raw_value, 'DATE_FORMAT')) else: objs = EMPTY_VALUE elif isinstance(self.field, models.BooleanField) or isinstance(self.field, models.NullBooleanField): objs = {True: 'Yes', False: 'No', None: 'Unknown'}[self.raw_value] else: objs = self.raw_value return [objs]
def date(value, arg=None): """Formats a date according to the given format.""" if not value: return '' if arg is None: arg = settings.DATE_FORMAT try: return formats.date_format(value, arg) except AttributeError: try: return format(value, arg) except AttributeError: return ''
def date_hierarchy(cl): """ Displays the date hierarchy for date drill-down functionality. """ if cl.date_hierarchy: field_name = cl.date_hierarchy year_field = "%s__year" % field_name month_field = "%s__month" % field_name day_field = "%s__day" % field_name field_generic = "%s__" % field_name year_lookup = cl.params.get(year_field) month_lookup = cl.params.get(month_field) day_lookup = cl.params.get(day_field) link = lambda d: cl.get_query_string(d, [field_generic]) if not (year_lookup or month_lookup or day_lookup): # select appropriate start level date_range = cl.query_set.aggregate(first=models.Min(field_name), last=models.Max(field_name)) if date_range["first"] and date_range["last"]: if date_range["first"].year == date_range["last"].year: year_lookup = date_range["first"].year if date_range["first"].month == date_range["last"].month: month_lookup = date_range["first"].month if year_lookup and month_lookup and day_lookup: day = datetime.date(int(year_lookup), int(month_lookup), int(day_lookup)) return { "show": True, "back": { "link": link({year_field: year_lookup, month_field: month_lookup}), "title": capfirst(formats.date_format(day, "YEAR_MONTH_FORMAT")), }, "choices": [{"title": capfirst(formats.date_format(day, "MONTH_DAY_FORMAT"))}], } elif year_lookup and month_lookup: days = cl.query_set.filter(**{year_field: year_lookup, month_field: month_lookup}).dates(field_name, "day") return { "show": True, "back": {"link": link({year_field: year_lookup}), "title": str(year_lookup)}, "choices": [ { "link": link({year_field: year_lookup, month_field: month_lookup, day_field: day.day}), "title": capfirst(formats.date_format(day, "MONTH_DAY_FORMAT")), } for day in days ], } elif year_lookup: months = cl.query_set.filter(**{year_field: year_lookup}).dates(field_name, "month") return { "show": True, "back": {"link": link({}), "title": _("All dates")}, "choices": [ { "link": link({year_field: year_lookup, month_field: month.month}), "title": capfirst(formats.date_format(month, "YEAR_MONTH_FORMAT")), } for month in months ], } else: years = cl.query_set.dates(field_name, "year") return { "show": True, "choices": [{"link": link({year_field: str(year.year)}), "title": str(year.year)} for year in years], }