rework field preparation
[matthijs/upstream/django-ldapdb.git] / ldapdb / models / query.py
index 277fec922dbea9704a36b32c84372620169974af..67d193607b7bc77c09ac74c4a802388a185df6f1 100644 (file)
@@ -1,7 +1,7 @@
 # -*- coding: utf-8 -*-
 # 
 # django-ldapdb
-# Copyright (C) 2009 BollorĂ© telecom
+# Copyright (C) 2009-2010 BollorĂ© telecom
 # See AUTHORS file for a full list of contributors.
 # 
 # This program is free software: you can redistribute it and/or modify
@@ -28,7 +28,7 @@ from django.db.models.sql.where import WhereNode as BaseWhereNode, Constraint as
 
 import ldapdb
 
-from ldapdb import escape_ldap_filter
+from ldapdb.models.fields import CharField
 
 class Constraint(BaseConstraint):
     """
@@ -43,23 +43,12 @@ class Constraint(BaseConstraint):
         # Because of circular imports, we need to import this here.
         from django.db.models.base import ObjectDoesNotExist
 
-        if lookup_type == 'endswith':
-            params = ["*%s" % escape_ldap_filter(value)]
-        elif lookup_type == 'startswith':
-            params = ["%s*" % escape_ldap_filter(value)]
-        elif lookup_type == 'contains':
-            params = ["*%s*" % escape_ldap_filter(value)]
-        elif lookup_type == 'exact':
-            params = [escape_ldap_filter(value)]
-        elif lookup_type == 'in':
-            params = [escape_ldap_filter(v) for v in value]
-        else:
-            raise TypeError("Field has invalid lookup: %s" % lookup_type)
-
         try:
             if self.field:
+                params = self.field.get_db_prep_lookup(lookup_type, value)
                 db_type = self.field.db_type()
             else:
+                params = CharField().get_db_prep_lookup(lookup_type, value)
                 db_type = None
         except ObjectDoesNotExist:
             raise EmptyShortCircuit