Ticket #10890: week_pagination.diff

File week_pagination.diff, 2.2 KB (added by palewire, 15 years ago)

An addition to the archive_week generic view that adds next_week and previous_week objects to the context for easier pagination in your templates

  • django/views/generic/date_based.py

     
    183183    Context:
    184184        week:
    185185            (date) this week
     186        next_week:
     187            (dict) with three keys:
     188                'date' (date): the first day of the next week
     189                'year' (int): the year of first day of the next week
     190                'week' (int): the week number of the first day of the next week
     191            or None if the next week is in the future
     192        previous_week:
     193            (dict) with three keys:
     194                'date' (date): the first day of the previous week
     195                'year' (int): the year of first day of the previous week
     196                'week' (int): the week number of the first day of the previous week
    186197        object_list:
    187198            list of objects published in the given week
    188199    """
     
    208219    if last_day >= now.date() and not allow_future:
    209220        lookup_kwargs['%s__lte' % date_field] = now
    210221    object_list = queryset.filter(**lookup_kwargs)
     222
     223    # Calculate the next week, if applicable.
     224    if allow_future:
     225        next_week = {
     226            'year': last_day.year,
     227            'week': last_day.isocalendar()[1],
     228            'date': last_day
     229        }
     230    elif last_day < datetime.date.today():
     231        next_week = {
     232            'year': last_day.year,
     233            'week': last_day.isocalendar()[1],
     234            'date': last_day
     235        }
     236    else:
     237        next_week = None
     238
     239    # Calculate the previous week
     240    previous_week_dt = first_day - datetime.timedelta(days=7)
     241    previous_week = {
     242        'year': previous_week_dt.year,
     243        'week': previous_week_dt.isocalendar()[1],
     244        'date': previous_week_dt
     245    }
     246
    211247    if not object_list and not allow_empty:
    212248        raise Http404
    213249    if not template_name:
     
    216252    c = RequestContext(request, {
    217253        '%s_list' % template_object_name: object_list,
    218254        'week': date,
     255        'previous_week': previous_week,
     256        'next_week': next_week,
    219257    })
    220258    for key, value in extra_context.items():
    221259        if callable(value):
Back to Top