Ticket #7161: date_based.next_prev.diff
File date_based.next_prev.diff, 6.6 KB (added by , 17 years ago) |
---|
-
django/views/generic/date_based.py
107 107 Context: 108 108 month: 109 109 (date) this month 110 previous_month: 111 (date) the first day of the previous month containing published objects, or None 110 112 next_month: 111 (date) the first day of the next month, or None if the next month is in the future 112 previous_month: 113 (date) the first day of the previous month 113 (date) the first day of the next month containing published objects, or None 114 114 object_list: 115 115 list of objects published in the given month 116 116 """ … … 138 138 if not object_list and not allow_empty: 139 139 raise Http404 140 140 141 # Calculate the next month, if applicable. 142 if allow_future: 143 next_month = last_day + datetime.timedelta(days=1) 144 elif last_day < datetime.date.today(): 145 next_month = last_day + datetime.timedelta(days=1) 146 else: 147 next_month = None 148 141 # Prepare the DateQuerySet for lazy querying of previous_month / next_month. 142 months_lookup_kwargs = {} 143 if not allow_future: 144 months_lookup_kwargs['%s__lte' % date_field] = now 145 all_months = queryset.filter(**months_lookup_kwargs).dates(date_field, 'month') 146 147 def previous_month(): 148 months = list(all_months) 149 date_index = months.index(datetime.datetime(*date.timetuple()[:3])) 150 # Is this the first month? 151 if date_index == 0: 152 return None 153 else: 154 return months[date_index-1].date() 155 156 def next_month(): 157 months = list(all_months) 158 date_index = months.index(datetime.datetime(*date.timetuple()[:3])) 159 # Is this the last month? 160 if date_index == len(months) - 1: 161 return None 162 else: 163 return months[date_index+1].date() 164 149 165 if not template_name: 150 166 template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower()) 151 167 t = template_loader.get_template(template_name) 152 168 c = RequestContext(request, { 153 169 '%s_list' % template_object_name: object_list, 154 170 'month': date, 171 'previous_month': previous_month, 155 172 'next_month': next_month, 156 'previous_month': first_day - datetime.timedelta(days=1),157 173 }, context_processors) 158 174 for key, value in extra_context.items(): 159 175 if callable(value): … … 225 241 day: 226 242 (datetime) the day 227 243 previous_day 228 (datetime) the previous day 244 (datetime) the previous day containing published objects, or None 229 245 next_day 230 (datetime) the next day , or None if the current day is today246 (datetime) the next day containing published objects, or None 231 247 """ 232 248 if extra_context is None: extra_context = {} 233 249 try: … … 249 265 object_list = queryset.filter(**lookup_kwargs) 250 266 if not allow_empty and not object_list: 251 267 raise Http404 252 253 # Calculate the next day, if applicable. 254 if allow_future: 255 next_day = date + datetime.timedelta(days=1) 256 elif date < datetime.date.today(): 257 next_day = date + datetime.timedelta(days=1) 258 else: 259 next_day = None 260 268 269 # Prepare the DateQuerySet for lazy querying of previous_day / next_day. 270 days_lookup_kwargs = {} 271 if not allow_future: 272 days_lookup_kwargs['%s__lte' % date_field] = now 273 all_days = queryset.filter(**days_lookup_kwargs).dates(date_field, 'day') 274 275 def previous_day(): 276 days = list(all_days) 277 date_index = days.index(datetime.datetime(*date.timetuple()[:3])) 278 # Is this the first day? 279 if date_index == 0: 280 return None 281 else: 282 return days[date_index-1].date() 283 284 def next_day(): 285 days = list(all_days) 286 date_index = days.index(datetime.datetime(*date.timetuple()[:3])) 287 # Is this the last day? 288 if date_index == len(days) - 1: 289 return None 290 else: 291 return days[date_index+1].date() 292 261 293 if not template_name: 262 294 template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower()) 263 295 t = template_loader.get_template(template_name) 264 296 c = RequestContext(request, { 265 297 '%s_list' % template_object_name: object_list, 266 298 'day': date, 267 'previous_day': date - datetime.timedelta(days=1),299 'previous_day': previous_day, 268 300 'next_day': next_day, 269 301 }, context_processors) 270 302 for key, value in extra_context.items(): -
docs/generic_views.txt
396 396 * ``month``: A ``datetime.date`` object representing the given month. 397 397 398 398 * ``next_month``: A ``datetime.date`` object representing the first day of 399 the next month . If the next month is in the future, this will be400 ``None``.399 the next month containing published items. If ``month`` is the last month 400 with published items, this will be ``None``. 401 401 402 402 * ``previous_month``: A ``datetime.date`` object representing the first day 403 of the previous month . Unlike ``next_month``, this will never be404 ``None``.403 of the previous month containing published items. If ``month`` is the 404 first month with published items, this will be ``None``. 405 405 406 406 * ``object_list``: A list of objects available for the given month. This 407 407 variable's name depends on the ``template_object_name`` parameter, which … … 561 561 562 562 * ``day``: A ``datetime.date`` object representing the given day. 563 563 564 * ``next_day``: A ``datetime.date`` object representing the next day. If 565 the next day is in the future, this will be ``None``. 564 * ``next_day``: A ``datetime.date`` object representing the next day 565 containing published items. If ``day`` is the last day with published 566 items, this will be ``None``. 566 567 567 * ``previous_day``: A ``datetime.date`` object representing the given day. 568 Unlike ``next_day``, this will never be ``None``. 568 * ``previous_day``: A ``datetime.date`` object representing the previous day 569 containing published items. If ``day`` is the first day with published 570 items, this will be ``None``. 569 571 570 572 * ``object_list``: A list of objects available for the given day. This 571 573 variable's name depends on the ``template_object_name`` parameter, which