Code

Ticket #2450: allow_ascending.diff

File allow_ascending.diff, 4.3 KB (added by cogat, 7 years ago)
Line 
1Index: date_based.py
2===================================================================
3--- date_based.py       (revision 6710)
4+++ date_based.py       (working copy)
5@@ -21,6 +21,13 @@
6         latest
7             Latest N (defaults to 15) objects by date
8     """
9+   
10+    if date_field[0]==("-"):
11+        date_field = date_field[1:]
12+        ascending=''
13+    else:
14+        ascending='-'
15+   
16     if extra_context is None: extra_context = {}
17     model = queryset.model
18     if not allow_future:
19@@ -30,7 +37,7 @@
20         raise Http404, "No %s available" % model._meta.verbose_name
21 
22     if date_list and num_latest:
23-        latest = queryset.order_by('-'+date_field)[:num_latest]
24+        latest = queryset.order_by(ascending+date_field)[:num_latest]
25     else:
26         latest = None
27 
28@@ -69,6 +76,12 @@
29     model = queryset.model
30     now = datetime.datetime.now()
31 
32+    if date_field[0]==("-"):
33+        date_field = date_field[1:]
34+        ascending=''
35+    else:
36+        ascending='-'
37+
38     lookup_kwargs = {'%s__year' % date_field: year}
39 
40     # Only bother to check current date if the year isn't in the past and future objects aren't requested.
41@@ -78,7 +91,7 @@
42     if not date_list and not allow_empty:
43         raise Http404
44     if make_object_list:
45-        object_list = queryset.filter(**lookup_kwargs)
46+        object_list = queryset.filter(**lookup_kwargs).order_by(ascending+date_field)
47     else:
48         object_list = []
49     if not template_name:
50@@ -123,6 +136,12 @@
51     model = queryset.model
52     now = datetime.datetime.now()
53 
54+    if date_field[0]==("-"):
55+        date_field = date_field[1:]
56+        ascending=''
57+    else:
58+        ascending='-'
59+
60     # Calculate first and last day of month, for use in a date-range lookup.
61     first_day = date.replace(day=1)
62     if first_day.month == 12:
63@@ -134,7 +153,7 @@
64     # Only bother to check current date if the month isn't in the past and future objects are requested.
65     if last_day >= now.date() and not allow_future:
66         lookup_kwargs['%s__lte' % date_field] = now
67-    object_list = queryset.filter(**lookup_kwargs)
68+    object_list = queryset.filter(**lookup_kwargs).order_by(ascending+date_field)
69     if not object_list and not allow_empty:
70         raise Http404
71 
72@@ -185,6 +204,12 @@
73     model = queryset.model
74     now = datetime.datetime.now()
75 
76+    if date_field[0]==("-"):
77+        date_field = date_field[1:]
78+        ascending=''
79+    else:
80+        ascending='-'
81+
82     # Calculate first and last day of week, for use in a date-range lookup.
83     first_day = date
84     last_day = date + datetime.timedelta(days=7)
85@@ -193,7 +218,7 @@
86     # Only bother to check current date if the week isn't in the past and future objects aren't requested.
87     if last_day >= now.date() and not allow_future:
88         lookup_kwargs['%s__lte' % date_field] = now
89-    object_list = queryset.filter(**lookup_kwargs)
90+    object_list = queryset.filter(**lookup_kwargs).order_by(ascending+date_field)
91     if not object_list and not allow_empty:
92         raise Http404
93     if not template_name:
94@@ -238,6 +263,12 @@
95     model = queryset.model
96     now = datetime.datetime.now()
97 
98+    if date_field[0]==("-"):
99+        date_field = date_field[1:]
100+        ascending=''
101+    else:
102+        ascending='-'
103+
104     if isinstance(model._meta.get_field(date_field), DateTimeField):
105         lookup_kwargs = {'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max))}
106     else:
107@@ -246,7 +277,7 @@
108     # Only bother to check current date if the date isn't in the past and future objects aren't requested.
109     if date >= now.date() and not allow_future:
110         lookup_kwargs['%s__lte' % date_field] = now
111-    object_list = queryset.filter(**lookup_kwargs)
112+    object_list = queryset.filter(**lookup_kwargs).order_by(ascending+date_field)
113     if not allow_empty and not object_list:
114         raise Http404
115 
116@@ -308,6 +339,9 @@
117     model = queryset.model
118     now = datetime.datetime.now()
119 
120+    if date_field[0]==("-"):
121+        date_field = date_field[1:]
122+
123     if isinstance(model._meta.get_field(date_field), DateTimeField):
124         lookup_kwargs = {'%s__range' % date_field: (datetime.datetime.combine(date, datetime.time.min), datetime.datetime.combine(date, datetime.time.max))}
125     else: