Permalink
Browse files

Fixed #17508 Now, DateDetail takes a attribute to set the unity of da…

…tes named date_separate_unit.

The attribute allows a vaule, which daily, monthly or yearly:
* daily: /2012/jan/06/blog-entry-name/
* monthly: /2012/jan/blog-entry-name/
* yearly: /2012/blog-entry-name/
  • Loading branch information...
1 parent 0e18fb0 commit d9c1ae5e1b99edf1a3864794867aba6935d3d626 @hirokiky committed Feb 18, 2013
Showing with 26 additions and 7 deletions.
  1. +26 −7 django/views/generic/dates.py
@@ -628,16 +628,28 @@ class BaseDateDetailView(YearMixin, MonthMixin, DayMixin, DateMixin, BaseDetailV
Detail view of a single object on a single date; this differs from the
standard DetailView by accepting a year/month/day in the URL.
"""
+ date_separate_unity = "daily"
+
def get_object(self, queryset=None):
"""
Get the object this request displays.
"""
- year = self.get_year()
- month = self.get_month()
- day = self.get_day()
- date = _date_from_string(year, self.get_year_format(),
- month, self.get_month_format(),
- day, self.get_day_format())
+ date_field= self.get_date_field()
+
+ if self.date_separate_unity == "yearly":
+ date = _date_from_string(self.get_year(), self.get_year_format())
+ get_until_date = self._get_next_year
+ elif self.date_separate_unity == "monthly":
+ date = _date_from_string(self.get_year(), self.get_year_format(),
+ self.get_month(), self.get_month_format())
+ get_until_date = self._get_next_month
+ elif self.date_separate_unity == "daily":
+ date = _date_from_string(self.get_year(), self.get_year_format(),
+ self.get_month(), self.get_month_format(),
+ self.get_day(), self.get_day_format())
+ get_until_date = self._get_next_day
+ else:
+ raise ValueError("Unknown date unit.")
# Use a custom queryset if provided
qs = queryset or self.get_queryset()
@@ -651,7 +663,14 @@ def get_object(self, queryset=None):
# Filter down a queryset from self.queryset using the date from the
# URL. This'll get passed as the queryset to DetailView.get_object,
# which'll handle the 404
- lookup_kwargs = self._make_single_date_lookup(date)
+ since = self._make_date_lookup_arg(date)
+ until = self._make_date_lookup_arg(get_until_date(date))
+
+ lookup_kwargs = {
+ '%s__gte' % date_field: since,
+ '%s__lt' % date_field: until,
+ }
+
qs = qs.filter(**lookup_kwargs)
return super(BaseDetailView, self).get_object(queryset=qs)

0 comments on commit d9c1ae5

Please sign in to comment.