Ticket #2433: date_based.py.diff

File date_based.py.diff, 7.4 KB (added by Pete Crosier, 18 years ago)
  • django/views/generic/date_based.py

     
    77def archive_index(request, queryset, date_field, num_latest=15,
    88        template_name=None, template_loader=loader,
    99        extra_context=None, allow_empty=False, context_processors=None,
    10         mimetype=None):
     10        mimetype=None, allow_future=False):
    1111    """
    1212    Generic top-level archive of date-based objects.
    1313
     
    2020    """
    2121    if extra_context is None: extra_context = {}
    2222    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()})
    2425    date_list = queryset.dates(date_field, 'year')[::-1]
    2526    if not date_list and not allow_empty:
    2627        raise Http404, "No %s available" % model._meta.verbose_name
     
    4748def archive_year(request, year, queryset, date_field, template_name=None,
    4849        template_loader=loader, extra_context=None, allow_empty=False,
    4950        context_processors=None, template_object_name='object', mimetype=None,
    50         make_object_list=False):
     51        make_object_list=False, allow_future=False):
    5152    """
    5253    Generic yearly archive view.
    5354
     
    6768
    6869    lookup_kwargs = {'%s__year' % date_field: year}
    6970
    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:
    7273        lookup_kwargs['%s__lte' % date_field] = now
    7374    date_list = queryset.filter(**lookup_kwargs).dates(date_field, 'month')
    7475    if not date_list and not allow_empty:
     
    9596def archive_month(request, year, month, queryset, date_field,
    9697        month_format='%b', template_name=None, template_loader=loader,
    9798        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):
    99100    """
    100101    Generic monthly archive view.
    101102
     
    127128        last_day = first_day.replace(month=first_day.month + 1)
    128129    lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
    129130
    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:
    132133        lookup_kwargs['%s__lte' % date_field] = now
    133134    object_list = queryset.filter(**lookup_kwargs)
    134135    if not object_list and not allow_empty:
    135136        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
    136146    if not template_name:
    137147        template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
    138148    t = template_loader.get_template(template_name)
    139149    c = RequestContext(request, {
    140150        '%s_list' % template_object_name: object_list,
    141151        'month': date,
    142         'next_month': (last_day < datetime.date.today()) and (last_day + datetime.timedelta(days=1)) or None,
     152        'next_month': next_month,
    143153        'previous_month': first_day - datetime.timedelta(days=1),
    144154    }, context_processors)
    145155    for key, value in extra_context.items():
     
    152162def archive_week(request, year, week, queryset, date_field,
    153163        template_name=None, template_loader=loader,
    154164        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):
    156166    """
    157167    Generic weekly archive view.
    158168
     
    177187    last_day = date + datetime.timedelta(days=7)
    178188    lookup_kwargs = {'%s__range' % date_field: (first_day, last_day)}
    179189
    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:
    182192        lookup_kwargs['%s__lte' % date_field] = now
    183193    object_list = queryset.filter(**lookup_kwargs)
    184194    if not object_list and not allow_empty:
     
    201211        month_format='%b', day_format='%d', template_name=None,
    202212        template_loader=loader, extra_context=None, allow_empty=False,
    203213        context_processors=None, template_object_name='object',
    204         mimetype=None):
     214        mimetype=None, allow_future=False):
    205215    """
    206216    Generic daily archive view.
    207217
     
    229239        '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
    230240    }
    231241
    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:
    234244        lookup_kwargs['%s__lte' % date_field] = now
    235245    object_list = queryset.filter(**lookup_kwargs)
    236246    if not allow_empty and not object_list:
    237247        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
    238257    if not template_name:
    239258        template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
    240259    t = template_loader.get_template(template_name)
     
    242261        '%s_list' % template_object_name: object_list,
    243262        'day': date,
    244263        '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,
    246265    }, context_processors)
    247266    for key, value in extra_context.items():
    248267        if callable(value):
     
    267286        month_format='%b', day_format='%d', object_id=None, slug=None,
    268287        slug_field=None, template_name=None, template_name_field=None,
    269288        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):
    271290    """
    272291    Generic detail view from year/month/day/slug or year/month/day/id structure.
    273292
     
    289308        '%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max)),
    290309    }
    291310
    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:
    294313        lookup_kwargs['%s__lte' % date_field] = now
    295314    if object_id:
    296315        lookup_kwargs['%s__exact' % model._meta.pk.name] = object_id
Back to Top