From ab216ac000ca0b6aeea64d9ce112395bbb7962d5 Mon Sep 17 00:00:00 2001
From: jlaine <jlaine@e071eeec-0327-468d-9b6a-08194a12b294>
Date: Mon, 24 May 2010 08:23:14 +0000
Subject: [PATCH] add support for "greater or equal" and "less or equal"
 lookups on IntegerFields

git-svn-id: https://svn.bolloretelecom.eu/opensource/django-ldapdb/trunk@862 e071eeec-0327-468d-9b6a-08194a12b294
---
 ldapdb/models/fields.py |  2 +-
 ldapdb/models/query.py  | 16 +++++++++++++---
 ldapdb/tests.py         | 12 ++++++++++--
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/ldapdb/models/fields.py b/ldapdb/models/fields.py
index ff0ad72..d21bbd0 100644
--- a/ldapdb/models/fields.py
+++ b/ldapdb/models/fields.py
@@ -50,7 +50,7 @@ class ImageField(fields.Field):
 
 class IntegerField(fields.IntegerField):
     def get_db_prep_lookup(self, lookup_type, value):
-        if lookup_type == 'exact':
+        if lookup_type in ('exact', 'gte', 'lte'):
             return [value]
 
         raise TypeError("IntegerField has invalid lookup: %s" % lookup_type)
diff --git a/ldapdb/models/query.py b/ldapdb/models/query.py
index 67d1936..a8c614f 100644
--- a/ldapdb/models/query.py
+++ b/ldapdb/models/query.py
@@ -30,6 +30,14 @@ import ldapdb
 
 from ldapdb.models.fields import CharField
 
+def get_lookup_operator(lookup_type):
+    if lookup_type == 'gte':
+        return '>='
+    elif lookup_type == 'lte':
+        return '<='
+    else:
+        return '='
+
 class Constraint(BaseConstraint):
     """
     An object that can be passed to WhereNode.add() and knows how to
@@ -76,11 +84,13 @@ class WhereNode(BaseWhereNode):
             constraint, x, y, values = item
             if hasattr(constraint, "col"):
                 # django 1.2
-                clause = "(%s=%s)" % (constraint.col, values)
+                comp = get_lookup_operator(constraint.lookup_type)
+                clause = "(%s%s%s)" % (constraint.col, comp, values)
             else:
                 # django 1.1
-                (table, column, type) = constraint
-                equal_bits = [ "(%s=%s)" % (column, value) for value in values ]
+                (table, column, db_type) = constraint
+                comp = get_lookup_operator(x)
+                equal_bits = [ "(%s%s%s)" % (column, comp, value) for value in values ]
                 if len(equal_bits) == 1:
                     clause = equal_bits[0]
                 else:
diff --git a/ldapdb/tests.py b/ldapdb/tests.py
index 4b6f7f1..de13b9d 100644
--- a/ldapdb/tests.py
+++ b/ldapdb/tests.py
@@ -21,7 +21,7 @@
 from django.test import TestCase
 from django.db.models.sql.where import Constraint, AND, OR
 
-from ldapdb.models.query import escape_ldap_filter
+from ldapdb import escape_ldap_filter
 from ldapdb.models.fields import CharField, IntegerField, ListField
 from ldapdb.models.query import WhereNode
 
@@ -60,9 +60,17 @@ class WhereTestCase(TestCase):
 
     def test_integer_field(self):
         where = WhereNode()
-        where.add((Constraint("uid", "uid", CharField()), 'exact', 1), AND)
+        where.add((Constraint("uid", "uid", IntegerField()), 'exact', 1), AND)
         self.assertEquals(where.as_sql(), "(uid=1)")
 
+        where = WhereNode()
+        where.add((Constraint("uid", "uid", IntegerField()), 'gte', 1), AND)
+        self.assertEquals(where.as_sql(), "(uid>=1)")
+
+        where = WhereNode()
+        where.add((Constraint("uid", "uid", IntegerField()), 'lte', 1), AND)
+        self.assertEquals(where.as_sql(), "(uid<=1)")
+
     def test_and(self):
         where = WhereNode()
         where.add((Constraint("cn", "cn", CharField()), 'exact', "foo"), AND)
-- 
2.30.2