django/views/generic/dates.py
def _get_next_prev_month(generic_view, naive_result, is_previous, use_first_day):
    """
    Helper: Get the next or the previous valid date. The idea is to allow
    links on month/day views to never be 404s by never providing a date
    that'll be invalid for the given view.

    This is a bit complicated since it handles both next and previous months
    and days (for MonthArchiveView and DayArchiveView); hence the coupling to generic_view.

    However in essence the logic comes down to:

        * If allow_empty and allow_future are both true, this is easy: just
          return the naive result (just the next/previous day or month,
          reguardless of object existence.)

        * If allow_empty is true, allow_future is false, and the naive month
          isn't in the future, then return it; otherwise return None.

        * If allow_empty is false and allow_future is true, return the next
          date *that contains a valid object*, even if it's in the future. If
          there are no next objects, return None.

        * If allow_empty is false and allow_future is false, return the next
          date that contains a valid object. If that date is in the future, or
          if there are no next objects, return None.

    """
    date_field = generic_view.get_date_field()
    allow_empty = generic_view.get_allow_empty()
    allow_future = generic_view.get_allow_future()

    # If allow_empty is True the naive value will be valid
    if allow_empty:
        result = naive_result

    # Otherwise, we'll need to go to the database to look for an object
    # whose date_field is at least (greater than/less than) the given
    # naive result
    else:
        # Construct a lookup and an ordering depending on whether we're doing
        # a previous date or a next date lookup.
        if is_previous:
            lookup = {'%s__lte' % date_field: naive_result}
            ordering = '-%s' % date_field
        else:
            lookup = {'%s__gte' % date_field: naive_result}
            ordering = date_field

        qs = generic_view.get_queryset().filter(**lookup).order_by(ordering)

        # Snag the first object from the queryset; if it doesn't exist that
        # means there's no next/previous link available.
        try:
            result = getattr(qs[0], date_field)
        except IndexError:
            result = None

    # Convert datetimes to a dates
    if hasattr(result, 'date'):
        result = result.date()

    # For month views, we always want to have a date that's the first of the
    # month for consistency's sake.
    if result and use_first_day:
        result = result.replace(day=1)

    # Check against future dates.
    if result and (allow_future or result < datetime.date.today()):
        return result
    else:
        return None

    Helper: Get the next or the previous valid date. The idea is to allow
    links on month/day views to never be 404s by never providing a date
    that'll be invalid for the given view.

    This is a bit complicated since it handles both next and previous months
    and days (for MonthArchiveView and DayArchiveView); hence the coupling to generic_view.

    However in essence the logic comes down to:

        * If allow_empty and allow_future are both true, this is easy: just
          return the naive result (just the next/previous day or month,
          reguardless of object existence.)

        * If allow_empty is true, allow_future is false, and the naive month
          isn't in the future, then return it; otherwise return None.

        * If allow_empty is false and allow_future is true, return the next
          date *that contains a valid object*, even if it's in the future. If
          there are no next objects, return None.

        * If allow_empty is false and allow_future is false, return the next
          date that contains a valid object. If that date is in the future, or
          if there are no next objects, return None.

    
Get the next or the previous valid date. The idea is to allow
links on month/day views to never be 404s by never providing a date
that'll be invalid for the given view.

This is a bit complicated since it handles both next and previous months
and days (for MonthArchiveView and DayArchiveView); hence the coupling to generic_view.

However in essence the logic comes down to.

If allow_empty and allow_future are both true, this is easy: just
return the naive result (just the next/previous day or month,
reguardless of object existence.)

If allow_empty is true, allow_future is false, and the naive month
isn't in the future, then return it; otherwise return None.

If allow_empty is false and allow_future is true, return the next
date *that contains a valid object*, even if it's in the future. If
there are no next objects, return None.

If allow_empty is false and allow_future is false, return the next
date that contains a valid object. If that date is in the future, or
if there are no next objects, return None.
Get the next or the previous valid date . The idea is to allow links on month / day views to never be 404s by never providing a date that ' ll be invalid for the given view . This is a bit complicated since it handles both next and previous months and days ( for MonthArchiveView and DayArchiveView ) ; hence the coupling to generic_view . However in essence the logic comes down to . If allow_empty and allow_future are both true this is easy : just return the naive result ( just the next / previous day or month reguardless of object existence . ) If allow_empty is true allow_future is false and the naive month isn ' t in the future then return it ; otherwise return None . If allow_empty is false and allow_future is true return the next date * that contains a valid object * even if it ' s in the future . If there are no next objects return None . If allow_empty is false and allow_future is false return the next date that contains a valid object . If that date is in the future or if there are no next objects return None .
def _get_next_prev_month(generic_view, naive_result, is_previous, use_first_day):
    date_field = generic_view.get_date_field()
    allow_empty = generic_view.get_allow_empty()
    allow_future = generic_view.get_allow_future()
    if allow_empty:
        result = naive_result
    else:
        if is_previous:
            lookup = {'%s__lte' % date_field: naive_result}
            ordering = '-%s' % date_field
        else:
            lookup = {'%s__gte' % date_field: naive_result}
            ordering = date_field
        qs = generic_view.get_queryset().filter(**lookup).order_by(ordering)
        try:
            result = getattr(qs[0], date_field)
        except IndexError:
            result = None
    if hasattr(result, 'date'):
        result = result.date()
    if result and use_first_day:
        result = result.replace(day=1)
    if result and (allow_future or result < datetime.date.today()):
        return result
    else:
        return None
def _get_next_prev_month ( generic_view , naive_result , is_previous , use_first_day ) : date_field = generic_view . get_date_field ( ) allow_empty = generic_view . get_allow_empty ( ) allow_future = generic_view . get_allow_future ( ) if allow_empty : result = naive_result else : if is_previous : lookup = { '%s__lte' % date_field : naive_result } ordering = '-%s' % date_field else : lookup = { '%s__gte' % date_field : naive_result } ordering = date_field qs = generic_view . get_queryset ( ) . filter ( ** lookup ) . order_by ( ordering ) try : result = getattr ( qs [ 0 ] , date_field ) except IndexError : result = None if hasattr ( result , 'date' ) : result = result . date ( ) if result and use_first_day : result = result . replace ( day = 1 ) if result and ( allow_future or result < datetime . date . today ( ) ) : return result else : return None
Helper: Get the next or the previous valid date.
Helper : Get the next or the previous valid date .
"""
    Helper: Get the next or the previous valid date. The idea is to allow
    links on month/day views to never be 404s by never providing a date
    that'll be invalid for the given view.

    This is a bit complicated since it handles both next and previous months
    and days (for MonthArchiveView and DayArchiveView); hence the coupling to generic_view.

    However in essence the logic comes down to:

        * If allow_empty and allow_future are both true, this is easy: just
          return the naive result (just the next/previous day or month,
          reguardless of object existence.)

        * If allow_empty is true, allow_future is false, and the naive month
          isn't in the future, then return it; otherwise return None.

        * If allow_empty is false and allow_future is true, return the next
          date *that contains a valid object*, even if it's in the future. If
          there are no next objects, return None.

        * If allow_empty is false and allow_future is false, return the next
          date that contains a valid object. If that date is in the future, or
          if there are no next objects, return None.

    """ # If allow_empty is True the naive value will be valid # Otherwise, we'll need to go to the database to look for an object # whose date_field is at least (greater than/less than) the given # naive result # Construct a lookup and an ordering depending on whether we're doing # a previous date or a next date lookup. # Snag the first object from the queryset; if it doesn't exist that # means there's no next/previous link available. # Convert datetimes to a dates # For month views, we always want to have a date that's the first of the # month for consistency's sake. # Check against future dates.
generic_view
naive_result
is_previous
use_first_day