1 # -*- coding: utf-8 -*-
4 # Copyright (C) 2009-2010 Bolloré telecom
5 # See AUTHORS file for a full list of contributors.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 from django.db.models import fields, SubfieldBase
23 from ldapdb import escape_ldap_filter
25 class CharField(fields.CharField):
26 def __init__(self, *args, **kwargs):
27 kwargs['max_length'] = 200
28 super(CharField, self).__init__(*args, **kwargs)
30 def from_ldap(self, value, connection):
34 return value[0].decode(connection.charset)
36 def get_db_prep_lookup(self, lookup_type, value):
37 "Returns field's value prepared for database lookup."
38 if lookup_type == 'endswith':
39 return ["*%s" % escape_ldap_filter(value)]
40 elif lookup_type == 'startswith':
41 return ["%s*" % escape_ldap_filter(value)]
42 elif lookup_type in ['contains', 'icontains']:
43 return ["*%s*" % escape_ldap_filter(value)]
44 elif lookup_type == 'exact':
45 return [escape_ldap_filter(value)]
46 elif lookup_type == 'in':
47 return [escape_ldap_filter(v) for v in value]
49 raise TypeError("CharField has invalid lookup: %s" % lookup_type)
51 def get_db_prep_save(self, value, connection):
52 return [value.encode(connection.charset)]
54 def get_prep_lookup(self, lookup_type, value):
55 "Perform preliminary non-db specific lookup checks and conversions"
56 if lookup_type == 'endswith':
57 return "*%s" % escape_ldap_filter(value)
58 elif lookup_type == 'startswith':
59 return "%s*" % escape_ldap_filter(value)
60 elif lookup_type in ['contains', 'icontains']:
61 return "*%s*" % escape_ldap_filter(value)
62 elif lookup_type == 'exact':
63 return escape_ldap_filter(value)
64 elif lookup_type == 'in':
65 return [escape_ldap_filter(v) for v in value]
67 raise TypeError("CharField has invalid lookup: %s" % lookup_type)
69 class ImageField(fields.Field):
70 def from_ldap(self, value, connection):
76 def get_db_prep_lookup(self, lookup_type, value):
77 "Returns field's value prepared for database lookup."
78 return [self.get_prep_lookup(lookup_type, value)]
80 def get_db_prep_save(self, value, connection):
83 def get_prep_lookup(self, lookup_type, value):
84 "Perform preliminary non-db specific lookup checks and conversions"
85 raise TypeError("ImageField has invalid lookup: %s" % lookup_type)
87 class IntegerField(fields.IntegerField):
88 def from_ldap(self, value, connection):
94 def get_db_prep_lookup(self, lookup_type, value):
95 "Returns field's value prepared for database lookup."
96 return [self.get_prep_lookup(lookup_type, value)]
98 def get_db_prep_save(self, value, connection):
101 def get_prep_lookup(self, lookup_type, value):
102 "Perform preliminary non-db specific lookup checks and conversions"
103 if lookup_type in ('exact', 'gte', 'lte'):
105 raise TypeError("IntegerField has invalid lookup: %s" % lookup_type)
107 class ListField(fields.Field):
108 __metaclass__ = SubfieldBase
110 def from_ldap(self, value, connection):
113 def get_db_prep_lookup(self, lookup_type, value):
114 "Returns field's value prepared for database lookup."
115 return [self.get_prep_lookup(lookup_type, value)]
117 def get_db_prep_save(self, value, connection):
118 return [x.encode(connection.charset) for x in value]
120 def get_prep_lookup(self, lookup_type, value):
121 "Perform preliminary non-db specific lookup checks and conversions"
122 if lookup_type == 'contains':
123 return escape_ldap_filter(value)
124 raise TypeError("ListField has invalid lookup: %s" % lookup_type)
126 def to_python(self, value):