Code

Ticket #389: 389.diffs

File 389.diffs, 4.5 KB (added by dcf, 8 years ago)

Diffs for magic-removal branch

Line 
1Index: django/contrib/admin/templatetags/admin_list.py
2===================================================================
3--- django/contrib/admin/templatetags/admin_list.py     (revision 2427)
4+++ django/contrib/admin/templatetags/admin_list.py     (working copy)
5@@ -93,9 +93,9 @@
6             else:
7                 th_classes = []
8                 new_order_type = 'asc'
9-                if field_name == cl.order_field:
10-                    th_classes.append('sorted %sending' % cl.order_type.lower())
11-                    new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type.lower()]
12+                if field_name == cl.order_field[0]:
13+                    th_classes.append('sorted %sending' % cl.order_type[0].lower())
14+                    new_order_type = {'asc': 'desc', 'desc': 'asc'}[cl.order_type[0].lower()]
15 
16                 yield {"text": f.verbose_name,
17                        "sortable": True,
18Index: django/contrib/admin/views/main.py
19===================================================================
20--- django/contrib/admin/views/main.py  (revision 2427)
21+++ django/contrib/admin/views/main.py  (working copy)
22@@ -624,15 +624,20 @@
23         # then check the object's default ordering. If neither of those exist,
24         # order descending by ID by default. Finally, look for manually-specified
25         # ordering from the query string.
26-        ordering = lookup_opts.admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]
27+        orderings = lookup_opts.admin.ordering or lookup_opts.ordering or ['-' + lookup_opts.pk.name]
28 
29         # Normalize it to new-style ordering.
30-        ordering = handle_legacy_orderlist(ordering)
31+        orderings = handle_legacy_orderlist(orderings)
32 
33-        if ordering[0].startswith('-'):
34-            order_field, order_type = ordering[0][1:], 'desc'
35-        else:
36-            order_field, order_type = ordering[0], 'asc'
37+        order_fields = []
38+        order_types = []
39+        for ordering in orderings:
40+            if ordering[0].startswith('-'):
41+                order_fields.append(ordering[1:])
42+                order_types.append('desc')
43+            else:
44+                order_fields.append(ordering)
45+                order_types.append('asc')
46         if params.has_key(ORDER_VAR):
47             try:
48                 try:
49@@ -641,12 +646,12 @@
50                     pass
51                 else:
52                     if not isinstance(f.rel, models.ManyToOne) or not f.null:
53-                        order_field = f.name
54+                        order_fields = [f.name]
55             except (IndexError, ValueError):
56                 pass # Invalid ordering specified. Just use the default.
57         if params.has_key(ORDER_TYPE_VAR) and params[ORDER_TYPE_VAR] in ('asc', 'desc'):
58-            order_type = params[ORDER_TYPE_VAR]
59-        return order_field, order_type
60+            order_types = [params[ORDER_TYPE_VAR]]
61+        return order_fields, order_types
62 
63     def get_query_set(self):
64         qs = self.manager.get_query_set()
65@@ -676,18 +681,20 @@
66         # Calculate lookup_order_field.
67         # If the order-by field is a field with a relationship, order by the
68         # value in the related table.
69-        lookup_order_field = self.order_field
70-        try:
71-            f = self.lookup_opts.get_field(self.order_field, many_to_many=False)
72-        except models.FieldDoesNotExist:
73-            pass
74-        else:
75-            if isinstance(f.rel, models.ManyToOne):
76-                rel_ordering = f.rel.to._meta.ordering and f.rel.to._meta.ordering[0] or f.rel.to._meta.pk.column
77-                lookup_order_field = '%s.%s' % (f.rel.to._meta.db_table, rel_ordering)
78+        ordering = []
79+        for order_field, order_type in zip(self.order_field, self.order_type):
80+            try:
81+                f = self.lookup_opts.get_field(order_field, many_to_many=False)
82+            except models.FieldDoesNotExist:
83+                pass
84+            else:
85+                if isinstance(f.rel, models.ManyToOne):
86+                    rel_ordering = f.rel.to._meta.ordering and f.rel.to._meta.ordering[0] or f.rel.to._meta.pk.column
87+                    order_field = '%s.%s' % (f.rel.to._meta.db_table, rel_ordering)
88 
89-        # Set ordering.
90-        qs = qs.order_by((self.order_type == 'desc' and '-' or '') + lookup_order_field)
91+            # Set ordering.
92+            ordering.append((order_type == 'desc' and '-' or '') + order_field)
93+        qs = qs.order_by(ordering)
94 
95         # Apply keyword searches.
96         if self.lookup_opts.admin.search_fields and self.query: