Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Simplified date-based generic views.

Removed a confusing helper function that was confusing -- it used
last_day to store the first day of the next month.
  • Loading branch information...
commit e4d4cb6130b046d145ab1069df0c04d78786eedd 1 parent c09f6ff
@aaugustin aaugustin authored
Showing with 12 additions and 18 deletions.
  1. +12 −18 django/views/generic/dates.py
View
30 django/views/generic/dates.py
@@ -62,16 +62,19 @@ def get_next_month(self, date):
"""
Get the next valid month.
"""
- first_day, last_day = _month_bounds(date)
- next = (last_day + datetime.timedelta(days=1)).replace(day=1)
+ # next must be the first day of the next month.
+ if date.month == 12:
+ next = date.replace(year=date.year + 1, month=1, day=1)
+ else:
+ next = date.replace(month=date.month + 1, day=1)
return _get_next_prev_month(self, next, is_previous=False, use_first_day=True)
def get_previous_month(self, date):
"""
Get the previous valid month.
"""
- first_day, last_day = _month_bounds(date)
- prev = (first_day - datetime.timedelta(days=1))
+ # prev must be the last day of the previous month.
+ prev = date.replace(day=1) - datetime.timedelta(days=1)
return _get_next_prev_month(self, prev, is_previous=True, use_first_day=True)
@@ -309,7 +312,11 @@ def get_dated_items(self):
month, self.get_month_format())
# Construct a date-range lookup.
- first_day, last_day = _month_bounds(date)
+ first_day = date.replace(day=1)
+ if first_day.month == 12:
+ last_day = first_day.replace(year=first_day.year + 1, month=1)
+ else:
+ last_day = first_day.replace(month=first_day.month + 1)
lookup_kwargs = {
'%s__gte' % date_field: first_day,
'%s__lt' % date_field: last_day,
@@ -499,19 +506,6 @@ def _date_from_string(year, year_format, month, month_format, day='', day_format
})
-def _month_bounds(date):
- """
- Helper: return the first and last days of the month for the given date.
- """
- first_day = date.replace(day=1)
- if first_day.month == 12:
- last_day = first_day.replace(year=first_day.year + 1, month=1)
- else:
- last_day = first_day.replace(month=first_day.month + 1)
-
- return first_day, last_day
-
-
def _get_next_prev_month(generic_view, naive_result, is_previous, use_first_day):
"""
Helper: Get the next or the previous valid date. The idea is to allow
Please sign in to comment.
Something went wrong with that request. Please try again.