Ticket #2450: allow_ascending.diff

File allow_ascending.diff, 4.3 KB (added by cogat, 8 years ago)
  • date_based.py

     
    2121        latest
    2222            Latest N (defaults to 15) objects by date
    2323    """
     24   
     25    if date_field[0]==("-"):
     26        date_field = date_field[1:]
     27        ascending=''
     28    else:
     29        ascending='-'
     30   
    2431    if extra_context is None: extra_context = {}
    2532    model = queryset.model
    2633    if not allow_future:
     
    3037        raise Http404, "No %s available" % model._meta.verbose_name
    3138
    3239    if date_list and num_latest:
    33         latest = queryset.order_by('-'+date_field)[:num_latest]
     40        latest = queryset.order_by(ascending+date_field)[:num_latest]
    3441    else:
    3542        latest = None
    3643
     
    6976    model = queryset.model
    7077    now = datetime.datetime.now()
    7178
     79    if date_field[0]==("-"):
     80        date_field = date_field[1:]
     81        ascending=''
     82    else:
     83        ascending='-'
     84
    7285    lookup_kwargs = {'%s__year' % date_field: year}
    7386
    7487    # Only bother to check current date if the year isn't in the past and future objects aren't requested.
     
    7891    if not date_list and not allow_empty:
    7992        raise Http404
    8093    if make_object_list:
    81         object_list = queryset.filter(**lookup_kwargs)
     94        object_list = queryset.filter(**lookup_kwargs).order_by(ascending+date_field)
    8295    else:
    8396        object_list = []
    8497    if not template_name:
     
    123136    model = queryset.model
    124137    now = datetime.datetime.now()
    125138
     139    if date_field[0]==("-"):
     140        date_field = date_field[1:]
     141        ascending=''
     142    else:
     143        ascending='-'
     144
    126145    # Calculate first and last day of month, for use in a date-range lookup.
    127146    first_day = date.replace(day=1)
    128147    if first_day.month == 12:
     
    134153    # Only bother to check current date if the month isn't in the past and future objects are requested.
    135154    if last_day >= now.date() and not allow_future:
    136155        lookup_kwargs['%s__lte' % date_field] = now
    137     object_list = queryset.filter(**lookup_kwargs)
     156    object_list = queryset.filter(**lookup_kwargs).order_by(ascending+date_field)
    138157    if not object_list and not allow_empty:
    139158        raise Http404
    140159
     
    185204    model = queryset.model
    186205    now = datetime.datetime.now()
    187206
     207    if date_field[0]==("-"):
     208        date_field = date_field[1:]
     209        ascending=''
     210    else:
     211        ascending='-'
     212
    188213    # Calculate first and last day of week, for use in a date-range lookup.
    189214    first_day = date
    190215    last_day = date + datetime.timedelta(days=7)
     
    193218    # Only bother to check current date if the week isn't in the past and future objects aren't requested.
    194219    if last_day >= now.date() and not allow_future:
    195220        lookup_kwargs['%s__lte' % date_field] = now
    196     object_list = queryset.filter(**lookup_kwargs)
     221    object_list = queryset.filter(**lookup_kwargs).order_by(ascending+date_field)
    197222    if not object_list and not allow_empty:
    198223        raise Http404
    199224    if not template_name:
     
    238263    model = queryset.model
    239264    now = datetime.datetime.now()
    240265
     266    if date_field[0]==("-"):
     267        date_field = date_field[1:]
     268        ascending=''
     269    else:
     270        ascending='-'
     271
    241272    if isinstance(model._meta.get_field(date_field), DateTimeField):
    242273        lookup_kwargs = {'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max))}
    243274    else:
     
    246277    # Only bother to check current date if the date isn't in the past and future objects aren't requested.
    247278    if date >= now.date() and not allow_future:
    248279        lookup_kwargs['%s__lte' % date_field] = now
    249     object_list = queryset.filter(**lookup_kwargs)
     280    object_list = queryset.filter(**lookup_kwargs).order_by(ascending+date_field)
    250281    if not allow_empty and not object_list:
    251282        raise Http404
    252283
     
    308339    model = queryset.model
    309340    now = datetime.datetime.now()
    310341
     342    if date_field[0]==("-"):
     343        date_field = date_field[1:]
     344
    311345    if isinstance(model._meta.get_field(date_field), DateTimeField):
    312346        lookup_kwargs = {'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max))}
    313347    else:
Back to Top