Ticket #2367: date_based_pagination.diff
File date_based_pagination.diff, 11.8 KB (added by , 18 years ago) |
---|
-
date_based.py
1 1 from django.template import loader, RequestContext 2 2 from django.core.exceptions import ObjectDoesNotExist 3 3 from django.core.xheaders import populate_xheaders 4 from django.core.paginator import ObjectPaginator, InvalidPage 4 5 from django.db.models.fields import DateTimeField 5 6 from django.http import Http404, HttpResponse 6 7 import datetime, time 7 8 9 def 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 8 39 def archive_index(request, queryset, date_field, num_latest=15, 9 40 template_name=None, template_loader=loader, 10 41 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): 12 43 """ 13 44 Generic top-level archive of date-based objects. 14 45 … … 18 49 List of years 19 50 latest 20 51 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) 21 76 """ 22 77 if extra_context is None: extra_context = {} 23 78 model = queryset.model … … 30 85 if date_list and num_latest: 31 86 latest = queryset.order_by('-'+date_field)[:num_latest] 32 87 else: 33 latest = None88 latest = [] 34 89 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 35 96 if not template_name: 36 97 template_name = "%s/%s_archive.html" % (model._meta.app_label, model._meta.object_name.lower()) 37 98 t = template_loader.get_template(template_name) … … 44 105 c[key] = value() 45 106 else: 46 107 c[key] = value 108 109 c.update(pagination_context) 110 47 111 return HttpResponse(t.render(c), mimetype=mimetype) 48 112 49 113 def archive_year(request, year, queryset, date_field, template_name=None, 50 114 template_loader=loader, extra_context=None, allow_empty=False, 51 115 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): 53 117 """ 54 118 Generic yearly archive view. 55 119 … … 61 125 This year 62 126 object_list 63 127 List of objects published in the given month 128 64 129 (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) 65 155 """ 66 156 if extra_context is None: extra_context = {} 67 157 model = queryset.model … … 79 169 object_list = queryset.filter(**lookup_kwargs).order_by(date_field) 80 170 else: 81 171 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 82 179 if not template_name: 83 180 template_name = "%s/%s_archive_year.html" % (model._meta.app_label, model._meta.object_name.lower()) 84 181 t = template_loader.get_template(template_name) … … 92 189 c[key] = value() 93 190 else: 94 191 c[key] = value 192 193 c.update(pagination_context) 194 95 195 return HttpResponse(t.render(c), mimetype=mimetype) 96 196 97 197 def archive_month(request, year, month, queryset, date_field, 98 198 month_format='%b', template_name=None, template_loader=loader, 99 199 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): 101 202 """ 102 203 Generic monthly archive view. 103 204 … … 111 212 (date) the first day of the previous month 112 213 object_list: 113 214 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) 114 239 """ 115 240 if extra_context is None: extra_context = {} 116 241 try: … … 144 269 else: 145 270 next_month = None 146 271 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 147 278 if not template_name: 148 279 template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower()) 149 280 t = template_loader.get_template(template_name) … … 158 289 c[key] = value() 159 290 else: 160 291 c[key] = value 292 293 c.update(pagination_context) 294 161 295 return HttpResponse(t.render(c), mimetype=mimetype) 162 296 163 297 def archive_week(request, year, week, queryset, date_field, 164 298 template_name=None, template_loader=loader, 165 299 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): 167 302 """ 168 303 Generic weekly archive view. 169 304 … … 173 308 (date) this week 174 309 object_list: 175 310 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) 176 335 """ 177 336 if extra_context is None: extra_context = {} 178 337 try: … … 194 353 object_list = queryset.filter(**lookup_kwargs) 195 354 if not object_list and not allow_empty: 196 355 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 197 363 if not template_name: 198 364 template_name = "%s/%s_archive_week.html" % (model._meta.app_label, model._meta.object_name.lower()) 199 365 t = template_loader.get_template(template_name) … … 206 372 c[key] = value() 207 373 else: 208 374 c[key] = value 375 376 c.update(pagination_context) 377 209 378 return HttpResponse(t.render(c), mimetype=mimetype) 210 379 211 380 def archive_day(request, year, month, day, queryset, date_field, 212 381 month_format='%b', day_format='%d', template_name=None, 213 382 template_loader=loader, extra_context=None, allow_empty=False, 214 383 context_processors=None, template_object_name='object', 215 mimetype=None, allow_future=False ):384 mimetype=None, allow_future=False, paginate_by=None, page=None): 216 385 """ 217 386 Generic daily archive view. 218 387 … … 226 395 (datetime) the previous day 227 396 next_day 228 397 (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) 229 422 """ 230 423 if extra_context is None: extra_context = {} 231 424 try: … … 256 449 else: 257 450 next_day = None 258 451 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 259 458 if not template_name: 260 459 template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower()) 261 460 t = template_loader.get_template(template_name) … … 270 469 c[key] = value() 271 470 else: 272 471 c[key] = value 472 473 c.update(pagination_context) 474 273 475 return HttpResponse(t.render(c), mimetype=mimetype) 274 476 275 477 def archive_today(request, **kwargs):