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 .