Ticket #7161: date_based.next_prev.diff

File date_based.next_prev.diff, 6.6 KB (added by Ionut Ciocirlan <ionut.ciocirlan@…>, 7 years ago)
  • django/views/generic/date_based.py

     
    107107    Context:
    108108        month:
    109109            (date) this month
     110        previous_month:
     111            (date) the first day of the previous month containing published objects, or None
    110112        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
    114114        object_list:
    115115            list of objects published in the given month
    116116    """
     
    138138    if not object_list and not allow_empty:
    139139        raise Http404
    140140
    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   
    149165    if not template_name:
    150166        template_name = "%s/%s_archive_month.html" % (model._meta.app_label, model._meta.object_name.lower())
    151167    t = template_loader.get_template(template_name)
    152168    c = RequestContext(request, {
    153169        '%s_list' % template_object_name: object_list,
    154170        'month': date,
     171        'previous_month': previous_month,
    155172        'next_month': next_month,
    156         'previous_month': first_day - datetime.timedelta(days=1),
    157173    }, context_processors)
    158174    for key, value in extra_context.items():
    159175        if callable(value):
     
    225241        day:
    226242            (datetime) the day
    227243        previous_day
    228             (datetime) the previous day
     244            (datetime) the previous day containing published objects, or None
    229245        next_day
    230             (datetime) the next day, or None if the current day is today
     246            (datetime) the next day containing published objects, or None
    231247    """
    232248    if extra_context is None: extra_context = {}
    233249    try:
     
    249265    object_list = queryset.filter(**lookup_kwargs)
    250266    if not allow_empty and not object_list:
    251267        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 
    261293    if not template_name:
    262294        template_name = "%s/%s_archive_day.html" % (model._meta.app_label, model._meta.object_name.lower())
    263295    t = template_loader.get_template(template_name)
    264296    c = RequestContext(request, {
    265297        '%s_list' % template_object_name: object_list,
    266298        'day': date,
    267         'previous_day': date - datetime.timedelta(days=1),
     299        'previous_day': previous_day,
    268300        'next_day': next_day,
    269301    }, context_processors)
    270302    for key, value in extra_context.items():
  • docs/generic_views.txt

     
    396396    * ``month``: A ``datetime.date`` object representing the given month.
    397397
    398398    * ``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 be
    400       ``None``.
     399      the next month containing published items. If ``month`` is the last month
     400      with published items, this will be ``None``.
    401401
    402402    * ``previous_month``: A ``datetime.date`` object representing the first day
    403       of the previous month. Unlike ``next_month``, this will never be
    404       ``None``.
     403      of the previous month containing published items. If ``month`` is the
     404      first month with published items, this will be ``None``.
    405405
    406406    * ``object_list``: A list of objects available for the given month. This
    407407      variable's name depends on the ``template_object_name`` parameter, which
     
    561561
    562562    * ``day``: A ``datetime.date`` object representing the given day.
    563563
    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``.
    566567
    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``.
    569571
    570572    * ``object_list``: A list of objects available for the given day. This
    571573      variable's name depends on the ``template_object_name`` parameter, which
Back to Top