Code

Ticket #11695: change_list_sort.2.diff

File change_list_sort.2.diff, 8.4 KB (added by Rupe, 5 years ago)

Trying upload without replace

Line 
1Index: django/contrib/admin/templates/admin/change_list.html
2===================================================================
3--- django/contrib/admin/templates/admin/change_list.html       (revision 11585)
4+++ django/contrib/admin/templates/admin/change_list.html       (working copy)
5@@ -41,6 +41,13 @@
6     {% block object-tools %}
7       {% if has_add_permission %}
8         <ul class="object-tools">
9+          <li><a href=".">
10+               {% if cl.has_filters or cl.date_hierarchy %}
11+                       {% trans 'Reset sorting and filters' %}
12+               {% else %}
13+                       {% trans 'Reset sorting' %}
14+               {% endif %}
15+          </a></li>
16           <li>
17             <a href="add/{% if is_popup %}?_popup=1{% endif %}" class="addlink">
18               {% blocktrans with cl.opts.verbose_name as name %}Add {{ name }}{% endblocktrans %}
19Index: django/contrib/admin/templatetags/admin_list.py
20===================================================================
21--- django/contrib/admin/templatetags/admin_list.py     (revision 11585)
22+++ django/contrib/admin/templatetags/admin_list.py     (working copy)
23@@ -120,7 +120,7 @@
24 
25         th_classes = []
26         new_order_type = 'asc'
27-        if field_name == cl.order_field or admin_order_field == cl.order_field:
28+        if cl.order_field is not None and (field_name == cl.order_field or admin_order_field == cl.order_field):
29             th_classes.append('sorted %sending' % cl.order_type.lower())
30             new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type.lower()]
31 
32Index: django/contrib/admin/views/main.py
33===================================================================
34--- django/contrib/admin/views/main.py  (revision 11585)
35+++ django/contrib/admin/views/main.py  (working copy)
36@@ -63,7 +63,7 @@
37         if ERROR_FLAG in self.params:
38             del self.params[ERROR_FLAG]
39 
40-        self.order_field, self.order_type = self.get_ordering()
41+        self.order_field, self.order_type, self.ordering = self.get_ordering()
42         self.query = request.GET.get(SEARCH_VAR, '')
43         self.query_set = self.get_query_set()
44         self.get_results(request)
45@@ -138,10 +138,14 @@
46         # manually-specified ordering from the query string.
47         ordering = self.model_admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]
48 
49-        if ordering[0].startswith('-'):
50-            order_field, order_type = ordering[0][1:], 'desc'
51+        if len(ordering) == 1:
52+            if ordering[0].startswith('-'):
53+                order_field, order_type = ordering[0][1:], 'desc'
54+            else:
55+                order_field, order_type = ordering[0], 'asc'
56         else:
57-            order_field, order_type = ordering[0], 'asc'
58+            order_field = None
59+            order_type = None
60         if ORDER_VAR in params:
61             try:
62                 field_name = self.list_display[int(params[ORDER_VAR])]
63@@ -166,7 +170,7 @@
64                 pass # Invalid ordering specified. Just use the default.
65         if ORDER_TYPE_VAR in params and params[ORDER_TYPE_VAR] in ('asc', 'desc'):
66             order_type = params[ORDER_TYPE_VAR]
67-        return order_field, order_type
68+        return order_field, order_type, ordering
69 
70     def get_query_set(self):
71         qs = self.root_query_set
72@@ -216,6 +220,8 @@
73         # Set ordering.
74         if self.order_field:
75             qs = qs.order_by('%s%s' % ((self.order_type == 'desc' and '-' or ''), self.order_field))
76+        elif self.ordering:
77+            qs = qs.order_by(*self.ordering)
78 
79         # Apply keyword searches.
80         def construct_search(field_name):
81Index: docs/ref/contrib/admin/index.txt
82===================================================================
83--- docs/ref/contrib/admin/index.txt    (revision 11585)
84+++ docs/ref/contrib/admin/index.txt    (working copy)
85@@ -492,7 +492,15 @@
86 
87     Django will only honor the first element in the list/tuple; any others
88     will be ignored.
89+   
90+.. versionadded:: 1.2
91 
92+    Django will now honor the entire list/tuple specified in the ``ordering``
93+    attribute, but only for the initial display of the list. After the change
94+    list page is displayed, if a column is selected by the user for sorting
95+    only that column will be used. The ``Reset Sorting`` link can be used to
96+    bring the list back to the initial sorting.
97+   
98 .. attribute:: ModelAdmin.prepopulated_fields
99 
100 Set ``prepopulated_fields`` to a dictionary mapping field names to the fields
101Index: tests/regressiontests/admin_changelist/models.py
102===================================================================
103--- tests/regressiontests/admin_changelist/models.py    (revision 11585)
104+++ tests/regressiontests/admin_changelist/models.py    (working copy)
105@@ -6,4 +6,8 @@
106 
107 class Child(models.Model):
108     parent = models.ForeignKey(Parent, editable=False)
109-    name = models.CharField(max_length=30, blank=True)
110\ No newline at end of file
111+    name = models.CharField(max_length=30, blank=True)
112+   
113+    class Meta: 
114+        # This is to test the default ordering in the ChangeList     
115+        ordering = ["parent", "name"]
116\ No newline at end of file
117Index: tests/regressiontests/admin_changelist/tests.py
118===================================================================
119--- tests/regressiontests/admin_changelist/tests.py     (revision 11585)
120+++ tests/regressiontests/admin_changelist/tests.py     (working copy)
121@@ -14,6 +14,16 @@
122                 m.list_filter,m.date_hierarchy, m.search_fields,
123                 m.list_select_related, m.list_per_page, m.list_editable, m)
124         self.assertEqual(cl.query_set.query.select_related, {'parent': {'name': {}}})
125+       
126+    def test_model_order_used(self):
127+        """
128+        The ChangeList should initially use the sort order specified in the model
129+        """
130+        m = ChildAdmin(Child, admin.site)
131+        cl = ChangeList(MockRequest(), Child, m.list_display, m.list_display_links,
132+                m.list_filter,m.date_hierarchy, m.search_fields,
133+                m.list_select_related, m.list_per_page, m.list_editable, m)
134+        self.assert_(cl.query_set.query.order_by == Child._meta.ordering, "The model sort order was not properly applied.")
135 
136 class ChildAdmin(admin.ModelAdmin):
137     list_display = ['name', 'parent']
138Index: tests/regressiontests/admin_views/models.py
139===================================================================
140--- tests/regressiontests/admin_views/models.py (revision 11585)
141+++ tests/regressiontests/admin_views/models.py (working copy)
142@@ -22,6 +22,10 @@
143     date = models.DateTimeField()
144     section = models.ForeignKey(Section)
145 
146+    class Meta:
147+        # This is to test the default ordering in the ChangeList
148+        ordering = ['section', 'content']
149+
150     def __unicode__(self):
151         return self.title
152 
153Index: tests/regressiontests/admin_views/tests.py
154===================================================================
155--- tests/regressiontests/admin_views/tests.py  (revision 11585)
156+++ tests/regressiontests/admin_views/tests.py  (working copy)
157@@ -164,7 +164,7 @@
158         Ensure we can sort on a list_display field that is a Model method
159         (colunn 3 is 'model_year' in ArticleAdmin)
160         """
161-        response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'ot': 'dsc', 'o': 3})
162+        response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit, {'ot': 'desc', 'o': 3})
163         self.failUnlessEqual(response.status_code, 200)
164         self.failUnless(
165             response.content.index('Newest content') < response.content.index('Middle content') and
166@@ -185,6 +185,16 @@
167             "Results of sorting on ModelAdmin method are out of order."
168         )
169 
170+    def testChangeListSortingModelDefault(self):
171+        """Ensure the default ordering is used from the model"""
172+        response = self.client.get('/test_admin/%s/admin_views/article/' % self.urlbit)
173+        self.failUnlessEqual(response.status_code, 200)
174+        self.failUnless(
175+            response.content.index('Middle content') < response.content.index('Newest content') and
176+            response.content.index('Newest content') < response.content.index('Oldest content'),
177+            "Results of sorting on Model default sort are out of order."
178+        )
179+
180     def testLimitedFilter(self):
181         """Ensure admin changelist filters do not contain objects excluded via limit_choices_to."""
182         response = self.client.get('/test_admin/%s/admin_views/thing/' % self.urlbit)