Skip to content

Commit e4d4cb6

Browse files
committed
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.
1 parent c09f6ff commit e4d4cb6

File tree

1 file changed

+12
-18
lines changed

1 file changed

+12
-18
lines changed

django/views/generic/dates.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,19 @@ def get_next_month(self, date):
6262
"""
6363
Get the next valid month.
6464
"""
65-
first_day, last_day = _month_bounds(date)
66-
next = (last_day + datetime.timedelta(days=1)).replace(day=1)
65+
# next must be the first day of the next month.
66+
if date.month == 12:
67+
next = date.replace(year=date.year + 1, month=1, day=1)
68+
else:
69+
next = date.replace(month=date.month + 1, day=1)
6770
return _get_next_prev_month(self, next, is_previous=False, use_first_day=True)
6871

6972
def get_previous_month(self, date):
7073
"""
7174
Get the previous valid month.
7275
"""
73-
first_day, last_day = _month_bounds(date)
74-
prev = (first_day - datetime.timedelta(days=1))
76+
# prev must be the last day of the previous month.
77+
prev = date.replace(day=1) - datetime.timedelta(days=1)
7578
return _get_next_prev_month(self, prev, is_previous=True, use_first_day=True)
7679

7780

@@ -309,7 +312,11 @@ def get_dated_items(self):
309312
month, self.get_month_format())
310313

311314
# Construct a date-range lookup.
312-
first_day, last_day = _month_bounds(date)
315+
first_day = date.replace(day=1)
316+
if first_day.month == 12:
317+
last_day = first_day.replace(year=first_day.year + 1, month=1)
318+
else:
319+
last_day = first_day.replace(month=first_day.month + 1)
313320
lookup_kwargs = {
314321
'%s__gte' % date_field: first_day,
315322
'%s__lt' % date_field: last_day,
@@ -499,19 +506,6 @@ def _date_from_string(year, year_format, month, month_format, day='', day_format
499506
})
500507

501508

502-
def _month_bounds(date):
503-
"""
504-
Helper: return the first and last days of the month for the given date.
505-
"""
506-
first_day = date.replace(day=1)
507-
if first_day.month == 12:
508-
last_day = first_day.replace(year=first_day.year + 1, month=1)
509-
else:
510-
last_day = first_day.replace(month=first_day.month + 1)
511-
512-
return first_day, last_day
513-
514-
515509
def _get_next_prev_month(generic_view, naive_result, is_previous, use_first_day):
516510
"""
517511
Helper: Get the next or the previous valid date. The idea is to allow

0 commit comments

Comments
 (0)