Ticket #2367: date_based_paginate.diff

File date_based_paginate.diff, 8.8 KB (added by apgwoz@…, 9 years ago)
  • django/views/generic/date_based.py

     
    11from django.template import loader, RequestContext
    22from django.core.exceptions import ObjectDoesNotExist
    33from django.core.xheaders import populate_xheaders
     4from django.core.paginator import ObjectPaginator, InvalidPage
    45from django.http import Http404, HttpResponse
    56import datetime, time
    67
     
    9293            c[key] = value
    9394    return HttpResponse(t.render(c), mimetype=mimetype)
    9495
    95 def archive_month(request, year, month, queryset, date_field,
    96         month_format='%b', template_name=None, template_loader=loader,
    97         extra_context=None, allow_empty=False, context_processors=None,
    98         template_object_name='object', mimetype=None):
     96def archive_month(request, year, month, queryset, date_field, paginate_by=None,
     97        page=None, month_format='%b', template_name=None,
     98        template_loader=loader, extra_context=None, allow_empty=False,
     99        context_processors=None, template_object_name='object', mimetype=None):
    99100    """
    100101    Generic monthly archive view.
    101102
     
    109110            (date) the first day of the previous month
    110111        object_list:
    111112            list of objects published in the given month
     113        is_paginated
     114            are the results paginated?
     115        results_per_page
     116            number of objects per page (if paginated)
     117        has_next
     118            is there a next page?
     119        has_previous
     120            is there a prev page?
     121        page
     122            the current page
     123        next
     124            the next page
     125        previous
     126            the previous page
     127        pages
     128            number of pages, total
     129        hits
     130            number of objects, total
    112131    """
    113132    if extra_context is None: extra_context = {}
    114133    try:
     
    133152    object_list = queryset.filter(**lookup_kwargs)
    134153    if not object_list and not allow_empty:
    135154        raise Http404
     155
     156    if paginate_by:
     157        paginator = ObjectPaginator(object_list, paginate_by)
     158        if not page:
     159            page = request.GET.get('page', 1)
     160        try:
     161            page = int(page)
     162            object_list = paginator.get_page(page - 1)
     163        except (InvalidPage, ValueError):
     164            if page == 1 and allow_empty:
     165                object_list = []
     166            else:
     167                raise Http404
     168
    136169    if not template_name:
    137170        template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
    138171    t = template_loader.get_template(template_name)
     
    142175        'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None,
    143176        'previous_month': first_day - datetime.timedelta(days=1),
    144177    }, context_processors)
     178
    145179    for key, value in extra_context.items():
    146180        if callable(value):
    147181            c[key] = value()
    148182        else:
    149183            c[key] = value
     184
     185    if paginate_by:
     186        c['is_paginated'] = paginator.pages > 1
     187        c['results_per_page'] = paginate_by
     188        c['has_next'] = paginator.has_next_page(page - 1)
     189        c['has_previous'] = paginator.has_previous_page(page - 1)
     190        c['page'] = page
     191        c['next'] = page + 1
     192        c['previous'] = page - 1
     193        c['pages'] = paginator.pages
     194        c['hits'] = paginator.hits
     195    else:
     196        c['is_paginated'] = False
     197
    150198    return HttpResponse(t.render(c), mimetype=mimetype)
    151199
    152 def archive_week(request, year, week, queryset, date_field,
    153         template_name=None, template_loader=loader,
     200def archive_week(request, year, week, queryset, date_field, paginate_by=None,
     201        page=None, template_name=None, template_loader=loader,
    154202        extra_context=None, allow_empty=True, context_processors=None,
    155203        template_object_name='object', mimetype=None):
    156204    """
     
    162210            (date) this week
    163211        object_list:
    164212            list of objects published in the given week
     213        is_paginated
     214            are the results paginated?
     215        results_per_page
     216            number of objects per page (if paginated)
     217        has_next
     218            is there a next page?
     219        has_previous
     220            is there a prev page?
     221        page
     222            the current page
     223        next
     224            the next page
     225        previous
     226            the previous page
     227        pages
     228            number of pages, total
     229        hits
     230            number of objects, total
    165231    """
    166232    if extra_context is None: extra_context = {}
    167233    try:
     
    183249    object_list = queryset.filter(**lookup_kwargs)
    184250    if not object_list and not allow_empty:
    185251        raise Http404
     252    if paginate_by:
     253        paginator = ObjectPaginator(object_list, paginate_by)
     254        if not page:
     255            page = request.GET.get('page', 1)
     256        try:
     257            page = int(page)
     258            object_list = paginator.get_page(page - 1)
     259        except (InvalidPage, ValueError):
     260            if page == 1 and allow_empty:
     261                object_list = []
     262            else:
     263                raise Http404
    186264    if not template_name:
    187265        template_name = "%s/%s_archive_week.html" % (model._meta.app_label, model._meta.object_name.lower())
    188266    t = template_loader.get_template(template_name)
     
    195273            c[key] = value()
    196274        else:
    197275            c[key] = value
     276    if paginate_by:
     277        c['is_paginated'] = paginator.pages > 1
     278        c['results_per_page'] = paginate_by
     279        c['has_next'] = paginator.has_next_page(page - 1)
     280        c['has_previous'] = paginator.has_previous_page(page - 1)
     281        c['page'] = page
     282        c['next'] = page + 1
     283        c['previous'] = page - 1
     284        c['pages'] = paginator.pages
     285        c['hits'] = paginator.hits
     286    else:
     287        c['is_paginated'] = False
    198288    return HttpResponse(t.render(c), mimetype=mimetype)
    199289
    200290def archive_day(request, year, month, day, queryset, date_field,
    201         month_format='%b', day_format='%d', template_name=None,
    202         template_loader=loader, extra_context=None, allow_empty=False,
    203         context_processors=None, template_object_name='object',
    204         mimetype=None):
     291        paginate_by=None, page=None, month_format='%b', day_format='%d',
     292        template_name=None, template_loader=loader, extra_context=None,
     293        allow_empty=False, context_processors=None,
     294        template_object_name='object', mimetype=None):
    205295    """
    206296    Generic daily archive view.
    207297
     
    215305            (datetime) the previous day
    216306        next_day
    217307            (datetime) the next day, or None if the current day is today
     308        is_paginated
     309            are the results paginated?
     310        results_per_page
     311            number of objects per page (if paginated)
     312        has_next
     313            is there a next page?
     314        has_previous
     315            is there a prev page?
     316        page
     317            the current page
     318        next
     319            the next page
     320        previous
     321            the previous page
     322        pages
     323            number of pages, total
     324        hits
     325            number of objects, total
    218326    """
    219327    if extra_context is None: extra_context = {}
    220328    try:
     
    232340    # Only bother to check current date if the date isn't in the past.
    233341    if date >= now.date():
    234342        lookup_kwargs['%s__lte' % date_field] = now
    235     object_list = queryset.filter(**lookup_kwargs)
     343    object_list = queryset.filter(**lookup_kwargs)   
    236344    if not allow_empty and not object_list:
    237345        raise Http404
     346    if paginate_by:
     347        paginator = ObjectPaginator(object_list, paginate_by)
     348        if not page:
     349            page = request.GET.get('page', 1)
     350        try:
     351            page = int(page)
     352            object_list = paginator.get_page(page - 1)
     353        except (InvalidPage, ValueError):
     354            if page == 1 and allow_empty:
     355                object_list = []
     356            else:
     357                raise Http404
    238358    if not template_name:
    239359        template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
     360
    240361    t = template_loader.get_template(template_name)
    241362    c = RequestContext(request, {
    242363        '%s_list' % template_object_name: object_list,
     
    249370            c[key] = value()
    250371        else:
    251372            c[key] = value
     373    if paginate_by:
     374        c['is_paginated'] = paginator.pages > 1
     375        c['results_per_page'] = paginate_by
     376        c['has_next'] = paginator.has_next_page(page - 1)
     377        c['has_previous'] = paginator.has_previous_page(page - 1)
     378        c['page'] = page
     379        c['next'] = page + 1
     380        c['previous'] = page - 1
     381        c['pages'] = paginator.pages
     382        c['hits'] = paginator.hits
     383    else:
     384        c['is_paginated'] = False
    252385    return HttpResponse(t.render(c), mimetype=mimetype)
    253386
    254387def archive_today(request, **kwargs):
Back to Top