Ticket #2433: date_based.py.diff
File date_based.py.diff, 7.4 KB (added by , 18 years ago) |
---|
-
django/views/generic/date_based.py
7 7 def archive_index(request, queryset, date_field, num_latest=15, 8 8 template_name=None, template_loader=loader, 9 9 extra_context=None, allow_empty=False, context_processors=None, 10 mimetype=None ):10 mimetype=None, allow_future=False): 11 11 """ 12 12 Generic top-level archive of date-based objects. 13 13 … … 20 20 """ 21 21 if extra_context is None: extra_context = {} 22 22 model = queryset.model 23 queryset = queryset.filter(**{'%s__lte' % date_field: datetime.datetime.now()}) 23 if not allow_future: 24 queryset = queryset.filter(**{'%s__lte' % date_field: datetime.datetime.now()}) 24 25 date_list = queryset.dates(date_field, 'year')[::-1] 25 26 if not date_list and not allow_empty: 26 27 raise Http404, "No %s available" % model._meta.verbose_name … … 47 48 def archive_year(request, year, queryset, date_field, template_name=None, 48 49 template_loader=loader, extra_context=None, allow_empty=False, 49 50 context_processors=None, template_object_name='object', mimetype=None, 50 make_object_list=False ):51 make_object_list=False, allow_future=False): 51 52 """ 52 53 Generic yearly archive view. 53 54 … … 67 68 68 69 lookup_kwargs = {'%s__year' % date_field: year} 69 70 70 # Only bother to check current date if the year isn't in the past .71 if int(year) >= now.year :71 # Only bother to check current date if the year isn't in the past and future objects aren't requested. 72 if int(year) >= now.year and not allow_future: 72 73 lookup_kwargs['%s__lte' % date_field] = now 73 74 date_list = queryset.filter(**lookup_kwargs).dates(date_field, 'month') 74 75 if not date_list and not allow_empty: … … 95 96 def archive_month(request, year, month, queryset, date_field, 96 97 month_format='%b', template_name=None, template_loader=loader, 97 98 extra_context=None, allow_empty=False, context_processors=None, 98 template_object_name='object', mimetype=None ):99 template_object_name='object', mimetype=None, allow_future=False): 99 100 """ 100 101 Generic monthly archive view. 101 102 … … 127 128 last_day = first_day.replace(month=first_day.month + 1) 128 129 lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)} 129 130 130 # Only bother to check current date if the month isn't in the past .131 if last_day >= now.date() :131 # Only bother to check current date if the month isn't in the past and future objects are requested. 132 if last_day >= now.date() and not allow_future: 132 133 lookup_kwargs['%s__lte' % date_field] = now 133 134 object_list = queryset.filter(**lookup_kwargs) 134 135 if not object_list and not allow_empty: 135 136 raise Http404 137 138 # Calculate the next month, if applicable. 139 if allow_future: 140 next_month = last_day + datetime.timedelta(days=1) 141 elif last_day < datetime.date.today(): 142 next_month = last_day + datetime.timedelta(days=1) 143 else: 144 next_month = None 145 136 146 if not template_name: 137 147 template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower()) 138 148 t = template_loader.get_template(template_name) 139 149 c = RequestContext(request, { 140 150 '%s_list' % template_object_name: object_list, 141 151 'month': date, 142 'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None,152 'next_month': next_month, 143 153 'previous_month': first_day - datetime.timedelta(days=1), 144 154 }, context_processors) 145 155 for key, value in extra_context.items(): … … 152 162 def archive_week(request, year, week, queryset, date_field, 153 163 template_name=None, template_loader=loader, 154 164 extra_context=None, allow_empty=True, context_processors=None, 155 template_object_name='object', mimetype=None ):165 template_object_name='object', mimetype=None, allow_future=False): 156 166 """ 157 167 Generic weekly archive view. 158 168 … … 177 187 last_day = date + datetime.timedelta(days=7) 178 188 lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)} 179 189 180 # Only bother to check current date if the week isn't in the past .181 if last_day >= now.date() :190 # Only bother to check current date if the week isn't in the past and future objects aren't requested. 191 if last_day >= now.date() and not allow_future: 182 192 lookup_kwargs['%s__lte' % date_field] = now 183 193 object_list = queryset.filter(**lookup_kwargs) 184 194 if not object_list and not allow_empty: … … 201 211 month_format='%b', day_format='%d', template_name=None, 202 212 template_loader=loader, extra_context=None, allow_empty=False, 203 213 context_processors=None, template_object_name='object', 204 mimetype=None ):214 mimetype=None, allow_future=False): 205 215 """ 206 216 Generic daily archive view. 207 217 … … 229 239 '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)), 230 240 } 231 241 232 # Only bother to check current date if the date isn't in the past .233 if date >= now.date() :242 # Only bother to check current date if the date isn't in the past and future objects aren't requested. 243 if date >= now.date() and not allow_future: 234 244 lookup_kwargs['%s__lte' % date_field] = now 235 245 object_list = queryset.filter(**lookup_kwargs) 236 246 if not allow_empty and not object_list: 237 247 raise Http404 248 249 # Calculate the next day, if applicable. 250 if allow_future: 251 next_day = date + datetime.timedelta(days=1) 252 elif date < datetime.date.today(): 253 next_day = date + datetime.timedelta(days=1) 254 else: 255 next_day = None 256 238 257 if not template_name: 239 258 template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower()) 240 259 t = template_loader.get_template(template_name) … … 242 261 '%s_list' % template_object_name: object_list, 243 262 'day': date, 244 263 'previous_day': date - datetime.timedelta(days=1), 245 'next_day': (date < datetime.date.today()) and (date + datetime.timedelta(days=1)) or None,264 'next_day': next_day, 246 265 }, context_processors) 247 266 for key, value in extra_context.items(): 248 267 if callable(value): … … 267 286 month_format='%b', day_format='%d', object_id=None, slug=None, 268 287 slug_field=None, template_name=None, template_name_field=None, 269 288 template_loader=loader, extra_context=None, context_processors=None, 270 template_object_name='object', mimetype=None ):289 template_object_name='object', mimetype=None, allow_future=False): 271 290 """ 272 291 Generic detail view from year/month/day/slug or year/month/day/id structure. 273 292 … … 289 308 '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)), 290 309 } 291 310 292 # Only bother to check current date if the date isn't in the past .293 if date >= now.date() :311 # Only bother to check current date if the date isn't in the past and future objects aren't requested. 312 if date >= now.date() and not allow_future: 294 313 lookup_kwargs['%s__lte' % date_field] = now 295 314 if object_id: 296 315 lookup_kwargs['%s__exact' % model._meta.pk.name] = object_id