allow order_by using a '-' prefix
authorjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 09:55:21 +0000 (09:55 +0000)
committerjlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Mon, 24 May 2010 09:55:21 +0000 (09:55 +0000)
git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@869 e071eeec-0327-468d-9b6a-08194a12b294

ldapdb/models/query.py

index 020d4eeb7d4a9bf3d03c6242ad296ade8789e0a2..b35c12311d29bead6b88b0ebba43f74b4ee681e1 100644 (file)
@@ -139,13 +139,21 @@ class Query(BaseQuery):
             ordering = self.order_by
         else:
             ordering = self.order_by or self.model._meta.ordering
-        def getkey(x):
-            keys = []
-            for k in ordering:
-                attr = self.model._meta.get_field(k).db_column
-                keys.append(x[1].get(attr, '').lower())
-            return keys
-        vals = sorted(vals, key=lambda x: getkey(x))
+        def cmpvals(x, y):
+            for field in ordering:
+                if field.startswith('-'):
+                    field = field[1:]
+                    negate = True
+                else:
+                    negate = False
+                attr = self.model._meta.get_field(field).db_column
+                attr_x = x[1].get(attr, '').lower()
+                attr_y = y[1].get(attr, '').lower()
+                val = negate and cmp(attr_y, attr_x) or cmp(attr_x, attr_y)
+                if val:
+                    return val
+            return 0
+        vals = sorted(vals, cmp=cmpvals)
 
         # process results
         for dn, attrs in vals: