Ticket #2367: date_based_pagination.diff

File date_based_pagination.diff, 11.8 KB (added by paolo@…, 18 years ago)

pagination for each date_based

  • 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.db.models.fields import DateTimeField
    56from django.http import Http404, HttpResponse
    67import datetime, time
    78
     9def compute_pagination(request, paginator, page, paginate_by):
     10    """
     11    Compute queryset considering pagination, and update the context.
     12    """
     13    if not page:
     14        page = request.GET.get('page', 1)
     15    try:
     16        page = int(page)
     17        queryset = paginator.get_page(page - 1)
     18    except (InvalidPage, ValueError):
     19        if page == 1 and allow_empty:
     20            queryset = []
     21        else:
     22            raise Http404
     23
     24    c = {}
     25    c['is_paginated'] = paginator.pages > 1
     26    c['results_per_page'] = paginate_by
     27    c['has_next'] = paginator.has_next_page(page - 1)
     28    c['has_previous'] = paginator.has_previous_page(page - 1)
     29    c['page'] = page
     30    c['next'] = page + 1
     31    c['previous'] = page - 1
     32    c['last_on_page'] = paginator.last_on_page(page - 1),
     33    c['first_on_page'] = paginator.first_on_page(page - 1),
     34    c['pages'] = paginator.pages
     35    c['hits'] = paginator.hits
     36
     37    return (queryset, c)
     38
    839def archive_index(request, queryset, date_field, num_latest=15,
    940        template_name=None, template_loader=loader,
    1041        extra_context=None, allow_empty=False, context_processors=None,
    11         mimetype=None, allow_future=False):
     42        mimetype=None, allow_future=False, paginate_by=None, page=None):
    1243    """
    1344    Generic top-level archive of date-based objects.
    1445
     
    1849            List of years
    1950        latest
    2051            Latest N (defaults to 15) objects by date
     52        is_paginated
     53            are the results paginated?
     54        results_per_page
     55            number of objects per page (if paginated)
     56        has_next
     57            is there a next page?
     58        has_previous
     59            is there a prev page?
     60        page
     61            the current page
     62        next
     63            the next page
     64        previous
     65            the previous page
     66        pages
     67            number of pages, total
     68        hits
     69            number of objects, total
     70        last_on_page
     71            the result number of the last of object in the
     72            object_list (1-indexed)
     73        first_on_page
     74            the result number of the first object in the
     75            object_list (1-indexed)
    2176    """
    2277    if extra_context is None: extra_context = {}
    2378    model = queryset.model
     
    3085    if date_list and num_latest:
    3186        latest = queryset.order_by('-'+date_field)[:num_latest]
    3287    else:
    33         latest = None
     88        latest = []
    3489
     90    # Pagination.
     91    pagination_context = {'is_paginated': False}
     92    if paginate_by:
     93        paginator = ObjectPaginator(latest, paginate_by)
     94        latest, pagination_context = compute_pagination(request, paginator, page, paginate_by)
     95
    3596    if not template_name:
    3697        template_name = "%s/%s_archive.html" % (model._meta.app_label, model._meta.object_name.lower())
    3798    t = template_loader.get_template(template_name)
     
    44105            c[key] = value()
    45106        else:
    46107            c[key] = value
     108
     109    c.update(pagination_context)
     110
    47111    return HttpResponse(t.render(c), mimetype=mimetype)
    48112
    49113def archive_year(request, year, queryset, date_field, template_name=None,
    50114        template_loader=loader, extra_context=None, allow_empty=False,
    51115        context_processors=None, template_object_name='object', mimetype=None,
    52         make_object_list=False, allow_future=False):
     116        make_object_list=False, allow_future=False, paginate_by=None, page=None):
    53117    """
    54118    Generic yearly archive view.
    55119
     
    61125            This year
    62126        object_list
    63127            List of objects published in the given month
     128
    64129            (Only available if make_object_list argument is True)
     130
     131        is_paginated
     132            are the results paginated?
     133        results_per_page
     134            number of objects per page (if paginated)
     135        has_next
     136            is there a next page?
     137        has_previous
     138            is there a prev page?
     139        page
     140            the current page
     141        next
     142            the next page
     143        previous
     144            the previous page
     145        pages
     146            number of pages, total
     147        hits
     148            number of objects, total
     149        last_on_page
     150            the result number of the last of object in the
     151            object_list (1-indexed)
     152        first_on_page
     153            the result number of the first object in the
     154            object_list (1-indexed)
    65155    """
    66156    if extra_context is None: extra_context = {}
    67157    model = queryset.model
     
    79169        object_list = queryset.filter(**lookup_kwargs).order_by(date_field)
    80170    else:
    81171        object_list = []
     172
     173    # Pagination.
     174    pagination_context = {'is_paginated': False}
     175    if paginate_by:
     176        paginator = ObjectPaginator(object_list, paginate_by)
     177        object_list, pagination_context = compute_pagination(request, paginator, page, paginate_by)
     178
    82179    if not template_name:
    83180        template_name = "%s/%s_archive_year.html" % (model._meta.app_label, model._meta.object_name.lower())
    84181    t = template_loader.get_template(template_name)
     
    92189            c[key] = value()
    93190        else:
    94191            c[key] = value
     192
     193    c.update(pagination_context)
     194
    95195    return HttpResponse(t.render(c), mimetype=mimetype)
    96196
    97197def archive_month(request, year, month, queryset, date_field,
    98198        month_format='%b', template_name=None, template_loader=loader,
    99199        extra_context=None, allow_empty=False, context_processors=None,
    100         template_object_name='object', mimetype=None, allow_future=False):
     200        template_object_name='object', mimetype=None, allow_future=False,
     201        paginate_by=None, page=None):
    101202    """
    102203    Generic monthly archive view.
    103204
     
    111212            (date) the first day of the previous month
    112213        object_list:
    113214            list of objects published in the given month
     215        is_paginated
     216            are the results paginated?
     217        results_per_page
     218            number of objects per page (if paginated)
     219        has_next
     220            is there a next page?
     221        has_previous
     222            is there a prev page?
     223        page
     224            the current page
     225        next
     226            the next page
     227        previous
     228            the previous page
     229        pages
     230            number of pages, total
     231        hits
     232            number of objects, total
     233        last_on_page
     234            the result number of the last of object in the
     235            object_list (1-indexed)
     236        first_on_page
     237            the result number of the first object in the
     238            object_list (1-indexed)
    114239    """
    115240    if extra_context is None: extra_context = {}
    116241    try:
     
    144269    else:
    145270        next_month = None
    146271
     272    # Pagination.
     273    pagination_context = {'is_paginated': False}
     274    if paginate_by:
     275        paginator = ObjectPaginator(object_list, paginate_by)
     276        object_list, pagination_context = compute_pagination(request, paginator, page, paginate_by)
     277
    147278    if not template_name:
    148279        template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
    149280    t = template_loader.get_template(template_name)
     
    158289            c[key] = value()
    159290        else:
    160291            c[key] = value
     292
     293    c.update(pagination_context)
     294
    161295    return HttpResponse(t.render(c), mimetype=mimetype)
    162296
    163297def archive_week(request, year, week, queryset, date_field,
    164298        template_name=None, template_loader=loader,
    165299        extra_context=None, allow_empty=True, context_processors=None,
    166         template_object_name='object', mimetype=None, allow_future=False):
     300        template_object_name='object', mimetype=None, allow_future=False,
     301        paginate_by=None, page=None):
    167302    """
    168303    Generic weekly archive view.
    169304
     
    173308            (date) this week
    174309        object_list:
    175310            list of objects published in the given week
     311        is_paginated
     312            are the results paginated?
     313        results_per_page
     314            number of objects per page (if paginated)
     315        has_next
     316            is there a next page?
     317        has_previous
     318            is there a prev page?
     319        page
     320            the current page
     321        next
     322            the next page
     323        previous
     324            the previous page
     325        pages
     326            number of pages, total
     327        hits
     328            number of objects, total
     329        last_on_page
     330            the result number of the last of object in the
     331            object_list (1-indexed)
     332        first_on_page
     333            the result number of the first object in the
     334            object_list (1-indexed)
    176335    """
    177336    if extra_context is None: extra_context = {}
    178337    try:
     
    194353    object_list = queryset.filter(**lookup_kwargs)
    195354    if not object_list and not allow_empty:
    196355        raise Http404
     356
     357    # Pagination.
     358    pagination_context = {'is_paginated': False}
     359    if paginate_by:
     360        paginator = ObjectPaginator(object_list, paginate_by)
     361        object_list, pagination_context = compute_pagination(request, paginator, page, paginate_by)
     362
    197363    if not template_name:
    198364        template_name = "%s/%s_archive_week.html" % (model._meta.app_label, model._meta.object_name.lower())
    199365    t = template_loader.get_template(template_name)
     
    206372            c[key] = value()
    207373        else:
    208374            c[key] = value
     375
     376    c.update(pagination_context)
     377
    209378    return HttpResponse(t.render(c), mimetype=mimetype)
    210379
    211380def archive_day(request, year, month, day, queryset, date_field,
    212381        month_format='%b', day_format='%d', template_name=None,
    213382        template_loader=loader, extra_context=None, allow_empty=False,
    214383        context_processors=None, template_object_name='object',
    215         mimetype=None, allow_future=False):
     384        mimetype=None, allow_future=False, paginate_by=None, page=None):
    216385    """
    217386    Generic daily archive view.
    218387
     
    226395            (datetime) the previous day
    227396        next_day
    228397            (datetime) the next day, or None if the current day is today
     398        is_paginated
     399            are the results paginated?
     400        results_per_page
     401            number of objects per page (if paginated)
     402        has_next
     403            is there a next page?
     404        has_previous
     405            is there a prev page?
     406        page
     407            the current page
     408        next
     409            the next page
     410        previous
     411            the previous page
     412        pages
     413            number of pages, total
     414        hits
     415            number of objects, total
     416        last_on_page
     417            the result number of the last of object in the
     418            object_list (1-indexed)
     419        first_on_page
     420            the result number of the first object in the
     421            object_list (1-indexed)
    229422    """
    230423    if extra_context is None: extra_context = {}
    231424    try:
     
    256449    else:
    257450        next_day = None
    258451
     452    # Pagination.
     453    pagination_context = {'is_paginated': False}
     454    if paginate_by:
     455        paginator = ObjectPaginator(object_list, paginate_by)
     456        object_list, pagination_context = compute_pagination(request, paginator, page, paginate_by)
     457
    259458    if not template_name:
    260459        template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
    261460    t = template_loader.get_template(template_name)
     
    270469            c[key] = value()
    271470        else:
    272471            c[key] = value
     472
     473    c.update(pagination_context)
     474
    273475    return HttpResponse(t.render(c), mimetype=mimetype)
    274476
    275477def archive_today(request, **kwargs):
Back to Top