def filter_queryset(self, queryset, column, value): if value: value = { "min": try_parse_date(value.get("min")), "max": try_parse_date(value.get("max")), } return super(DateRangeFilter, self).filter_queryset(queryset, column, value)
def parse_date_range(value): output = parse_date_range_preset(value) if output: return output if isinstance(value, six.string_types): value = value.strip() if ".." in value: start, end = value.split("..", 1) elif value.count("-") == 1: start, end = value.split("-", 1) else: start, end = value.split(None, 1) elif isinstance(value, (list, tuple)): start, end = value[:2] else: raise ValueError("Can't split date range: %r" % value) date_range = (try_parse_date(start), try_parse_date(end)) if any(p is None for p in date_range): raise ValueError("Invalid date range: %r (parsed as %r)" % (value, date_range)) return date_range
def test_parse_date(): now = datetime.now() today = date.today() date_fmt1 = "2016-12-31" date_fmt2 = "2016-12-31 15:40:34.404540" date_fmt3 = "12/31/2016" expected_date = date(2016, 12, 31) assert parse_date(now) == now.date() assert parse_date(today) == today assert parse_date(date_fmt1) == expected_date assert parse_date(date_fmt2) == expected_date assert parse_date(date_fmt3) == expected_date assert try_parse_date(1) is None