From 6baded55b00e883c2612c013b884e8135baeaebe Mon Sep 17 00:00:00 2001 From: jlaine Date: Mon, 24 May 2010 09:55:21 +0000 Subject: [PATCH] allow order_by using a '-' prefix git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@869 e071eeec-0327-468d-9b6a-08194a12b294 --- ldapdb/models/query.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py index 020d4ee..b35c123 100644 --- a/ldapdb/models/query.py +++ b/ldapdb/models/query.py @@ -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: -- 2.30.2