Ticket #2386: date_based.py.2.diff

File date_based.py.2.diff, 3.3 KB (added by Pete Crosier, 9 years ago)

Works a bit more logically.

  • 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, if it exists.
     138    lookup_kwargs = {'%s__lte' % date_field: date.replace(day=1) - datetime.timedelta(days=1)}
     139    previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
     140    if previous_list:
     141        previous_month = getattr(previous_list[0], date_field).date()
     142    else:
     143        previous_month = None
     144
     145    # Get the closest next post's date, if it exists.
     146    if date.month == 12:
     147        lookup_kwargs = {'%s__gte' % date_field: date.replace(day=1, month=1, year=date.year + 1)}
     148    else:
     149        lookup_kwargs = {'%s__gte' % date_field: date.replace(day=1, month=date.month + 1)}
     150    next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
     151    if next_list:
     152        next_month = getattr(next_list[0], date_field).date()
     153    else:
     154        next_month = None
     155
    136156    if not template_name:
    137157        template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
    138158    t = template_loader.get_template(template_name)
    139159    c = RequestContext(request, {
    140160        '%s_list' % template_object_name: object_list,
    141161        '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),
     162        'next_month': next_month,
     163        'previous_month': previous_month,
    144164    }, context_processors)
    145165    for key, value in extra_context.items():
    146166        if callable(value):
     
    235255    object_list = queryset.filter(**lookup_kwargs)
    236256    if not allow_empty and not object_list:
    237257        raise Http404
     258
     259    # Get the closest previous post's date, if it exists.
     260    lookup_kwargs = {'%s__lte' % date_field: date - datetime.timedelta(days=1)}
     261    previous_list = queryset.filter(**lookup_kwargs).order_by('-%s' % date_field)
     262    if previous_list:
     263        previous_day = getattr(previous_list[0], date_field).date()
     264    else:
     265        previous_day = None
     266
     267    # Get the closest next post's date, if it exists.
     268    lookup_kwargs = {'%s__gte' % date_field: date + datetime.timedelta(days=1)}
     269    next_list = queryset.filter(**lookup_kwargs).order_by(date_field)
     270    if next_list:
     271        next_day = getattr(next_list[0], date_field).date()
     272    else:
     273        next_day = None
     274
    238275    if not template_name:
    239276        template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
    240277    t = template_loader.get_template(template_name)
    241278    c = RequestContext(request, {
    242279        '%s_list' % template_object_name: object_list,
    243280        '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,
     281        'previous_day': previous_day,
     282        'next_day': next_day,
    246283    }, context_processors)
    247284    for key, value in extra_context.items():
    248285        if callable(value):
Back to Top