Django

Code

Changeset 3457

Show
Ignore:
Timestamp:
07/27/06 11:36:02 (2 years ago)
Author:
adrian
Message:

Fixed #2433 -- Added allow_future option to date-based generic views

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/views/generic/date_based.py

    r3070 r3457  
    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. 
     
    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: 
     
    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. 
     
    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') 
     
    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. 
     
    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()) 
     
    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) 
     
    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. 
     
    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) 
     
    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. 
     
    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()) 
     
    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(): 
     
    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. 
     
    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: 
  • django/trunk/docs/generic_views.txt

    r3071 r3457  
    149149 
    150150A top-level index page showing the "latest" objects, by date. Objects with 
    151 a date in the *future* are not included. 
     151a date in the *future* are not included unless you set ``allow_future`` to 
     152``True``. 
    152153 
    153154**Required arguments:** 
     
    185186    * ``mimetype``: The MIME type to use for the resulting document. Defaults 
    186187      to the value of the ``DEFAULT_MIME_TYPE`` setting. 
     188 
     189    * ``allow_future``: A boolean specifying whether to include "future" 
     190      objects on this page, where "future" means objects in which the field 
     191      specified in ``date_field`` is greater than the current date/time. By 
     192      default, this is ``False``. 
    187193 
    188194**Template name:** 
     
    218224 
    219225A yearly archive page showing all available months in a given year. Objects 
    220 with a date in the *future* are not displayed. 
     226with a date in the *future* are not displayed unless you set ``allow_future`` 
     227to ``True``. 
    221228 
    222229**Required arguments:** 
     
    266273      to the value of the ``DEFAULT_MIME_TYPE`` setting. 
    267274 
     275    * ``allow_future``: A boolean specifying whether to include "future" 
     276      objects on this page, where "future" means objects in which the field 
     277      specified in ``date_field`` is greater than the current date/time. By 
     278      default, this is ``False``. 
     279 
    268280**Template name:** 
    269281 
     
    297309 
    298310A monthly archive page showing all objects in a given month. Objects with a 
    299 date in the *future* are not displayed. 
     311date in the *future* are not displayed unless you set ``allow_future`` to 
     312``True``. 
    300313 
    301314**Required arguments:** 
     
    347360      to the value of the ``DEFAULT_MIME_TYPE`` setting. 
    348361 
     362    * ``allow_future``: A boolean specifying whether to include "future" 
     363      objects on this page, where "future" means objects in which the field 
     364      specified in ``date_field`` is greater than the current date/time. By 
     365      default, this is ``False``. 
     366 
    349367**Template name:** 
    350368 
     
    379397 
    380398A weekly archive page showing all objects in a given week. Objects with a date 
    381 in the *future* are not displayed
     399in the *future* are not displayed unless you set ``allow_future`` to ``True``
    382400 
    383401**Required arguments:** 
     
    423441      to the value of the ``DEFAULT_MIME_TYPE`` setting. 
    424442 
     443    * ``allow_future``: A boolean specifying whether to include "future" 
     444      objects on this page, where "future" means objects in which the field 
     445      specified in ``date_field`` is greater than the current date/time. By 
     446      default, this is ``False``. 
     447 
    425448**Template name:** 
    426449 
     
    446469 
    447470A day archive page showing all objects in a given day. Days in the future throw 
    448 a 404 error, regardless of whether any objects exist for future days. 
     471a 404 error, regardless of whether any objects exist for future days, unless 
     472you set ``allow_future`` to ``True``. 
    449473 
    450474**Required arguments:** 
     
    502526      to the value of the ``DEFAULT_MIME_TYPE`` setting. 
    503527 
     528    * ``allow_future``: A boolean specifying whether to include "future" 
     529      objects on this page, where "future" means objects in which the field 
     530      specified in ``date_field`` is greater than the current date/time. By 
     531      default, this is ``False``. 
     532 
    504533**Template name:** 
    505534 
     
    538567**Description:** 
    539568 
    540 A page representing an individual object. 
     569A page representing an individual object. If the object has a date value in the 
     570future, the view will throw a 404 error by default, unless you set 
     571``allow_future`` to ``True``. 
    541572 
    542573**Required arguments:** 
     
    605636      to the value of the ``DEFAULT_MIME_TYPE`` setting. 
    606637 
     638    * ``allow_future``: A boolean specifying whether to include "future" 
     639      objects on this page, where "future" means objects in which the field 
     640      specified in ``date_field`` is greater than the current date/time. By 
     641      default, this is ``False``. 
     642 
    607643**Template name:** 
    608644