Ticket #2386: date_based.py.diff

File date_based.py.diff, 3.8 KB (added by Pete Crosier, 9 years ago)
  • django/views/generic/date_based.py

     
    133133    object_list = queryset.filter(**lookup_kwargs)
    134134    if not object_list and not allow_empty:
    135135        raise Http404
     136
     137    # Get the closest previous post's date for the year, if it exists.
     138    if date.month == 1:
     139        previous_month = None
     140    else:
     141        lookup_kwargs = {'%s__range' % date_field: (date.replace(month=1, day=1), date.replace(month=date.month - 1))}
     142        previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
     143        if previous_list:
     144            previous_month = getattr(previous_list[0], date_field).date()
     145        else:
     146            previous_month = None
     147
     148    # Get the closest next post's date for the year, if it exists.
     149    if date.month == 12:
     150        next_month = None
     151    else:
     152        lookup_kwargs = {'%s__range' % date_field: (date.replace(month=date.month + 1), date.replace(month=12, day=31))}
     153        next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
     154        if next_list:
     155            next_month = getattr(next_list[0], date_field).date()
     156        else:
     157            next_month = None
     158
    136159    if not template_name:
    137160        template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
    138161    t = template_loader.get_template(template_name)
    139162    c = RequestContext(request, {
    140163        '%s_list' % template_object_name: object_list,
    141164        'month': date,
    142         'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None,
    143         'previous_month': first_day - datetime.timedelta(days=1),
     165        'next_month': next_month,
     166        'previous_month': previous_month,
    144167    }, context_processors)
    145168    for key, value in extra_context.items():
    146169        if callable(value):
     
    235258    object_list = queryset.filter(**lookup_kwargs)
    236259    if not allow_empty and not object_list:
    237260        raise Http404
     261
     262    # Get the closest previous post's date for the month, if it exists.
     263    if date.day == 1:
     264        previous_day = None
     265    else:
     266        lookup_kwargs = {'%s__range' % date_field: (date.replace(day=1), date.replace(day=date.day - 1))}
     267        previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
     268        if previous_list:
     269            previous_day = getattr(previous_list[0], date_field).date()
     270        else:
     271            previous_day = None
     272
     273    # Get the closest next post's date for the month, if it exists.
     274    if date.month != 12:
     275        last_day = (date.replace(month=date.month + 1, day=1) - datetime.timedelta(days=1)).day
     276    else:
     277        last_day = 31
     278
     279    if date.day == last_day:
     280        next_day = None
     281    else:
     282        lookup_kwargs = {'%s__range' % date_field: (date.replace(day=date.day + 1), date.replace(day=last_day))}
     283        next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
     284        if next_list:
     285            next_day = getattr(next_list[0], date_field).date()
     286        else:
     287            next_day = None
     288
    238289    if not template_name:
    239290        template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
    240291    t = template_loader.get_template(template_name)
    241292    c = RequestContext(request, {
    242293        '%s_list' % template_object_name: object_list,
    243294        'day': date,
    244         'previous_day': date - datetime.timedelta(days=1),
    245         'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None,
     295        'previous_day': previous_day,
     296        'next_day': next_day,
    246297    }, context_processors)
    247298    for key, value in extra_context.items():
    248299        if callable(value):
Back to Top