Ticket #2367: date_based_paginate.diff
File date_based_paginate.diff, 8.8 KB (added by , 18 years ago) |
---|
-
django/views/generic/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.http import Http404, HttpResponse 5 6 import datetime, time 6 7 … … 92 93 c[key] = value 93 94 return HttpResponse(t.render(c), mimetype=mimetype) 94 95 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):96 def 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): 99 100 """ 100 101 Generic monthly archive view. 101 102 … … 109 110 (date) the first day of the previous month 110 111 object_list: 111 112 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 112 131 """ 113 132 if extra_context is None: extra_context = {} 114 133 try: … … 133 152 object_list = queryset.filter(**lookup_kwargs) 134 153 if not object_list and not allow_empty: 135 154 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 136 169 if not template_name: 137 170 template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower()) 138 171 t = template_loader.get_template(template_name) … … 142 175 'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None, 143 176 'previous_month': first_day - datetime.timedelta(days=1), 144 177 }, context_processors) 178 145 179 for key, value in extra_context.items(): 146 180 if callable(value): 147 181 c[key] = value() 148 182 else: 149 183 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 150 198 return HttpResponse(t.render(c), mimetype=mimetype) 151 199 152 def archive_week(request, year, week, queryset, date_field, 153 template_name=None, template_loader=loader,200 def archive_week(request, year, week, queryset, date_field, paginate_by=None, 201 page=None, template_name=None, template_loader=loader, 154 202 extra_context=None, allow_empty=True, context_processors=None, 155 203 template_object_name='object', mimetype=None): 156 204 """ … … 162 210 (date) this week 163 211 object_list: 164 212 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 165 231 """ 166 232 if extra_context is None: extra_context = {} 167 233 try: … … 183 249 object_list = queryset.filter(**lookup_kwargs) 184 250 if not object_list and not allow_empty: 185 251 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 186 264 if not template_name: 187 265 template_name = "%s/%s_archive_week.html" % (model._meta.app_label, model._meta.object_name.lower()) 188 266 t = template_loader.get_template(template_name) … … 195 273 c[key] = value() 196 274 else: 197 275 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 198 288 return HttpResponse(t.render(c), mimetype=mimetype) 199 289 200 290 def 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): 205 295 """ 206 296 Generic daily archive view. 207 297 … … 215 305 (datetime) the previous day 216 306 next_day 217 307 (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 218 326 """ 219 327 if extra_context is None: extra_context = {} 220 328 try: … … 232 340 # Only bother to check current date if the date isn't in the past. 233 341 if date >= now.date(): 234 342 lookup_kwargs['%s__lte' % date_field] = now 235 object_list = queryset.filter(**lookup_kwargs) 343 object_list = queryset.filter(**lookup_kwargs) 236 344 if not allow_empty and not object_list: 237 345 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 238 358 if not template_name: 239 359 template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower()) 360 240 361 t = template_loader.get_template(template_name) 241 362 c = RequestContext(request, { 242 363 '%s_list' % template_object_name: object_list, … … 249 370 c[key] = value() 250 371 else: 251 372 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 252 385 return HttpResponse(t.render(c), mimetype=mimetype) 253 386 254 387 def archive_today(request, **kwargs):