1 # -*- coding: utf-8 -*-
4 # Copyright (c) 2009-2010, Bolloré telecom
7 # See AUTHORS file for a full list of contributors.
9 # Redistribution and use in source and binary forms, with or without modification,
10 # are permitted provided that the following conditions are met:
12 # 1. Redistributions of source code must retain the above copyright notice,
13 # this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # 3. Neither the name of Bolloré telecom nor the names of its contributors
20 # may be used to endorse or promote products derived from this software
21 # without specific prior written permission.
23 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
24 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
25 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26 # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
27 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
28 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
30 # ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 from django.db.models import fields, SubfieldBase
37 from ldapdb import escape_ldap_filter
39 class CharField(fields.CharField):
40 def __init__(self, *args, **kwargs):
41 kwargs['max_length'] = 200
42 super(CharField, self).__init__(*args, **kwargs)
44 def from_ldap(self, value, connection):
48 return value[0].decode(connection.charset)
50 def get_db_prep_lookup(self, lookup_type, value):
51 "Returns field's value prepared for database lookup."
52 if lookup_type == 'endswith':
53 return ["*%s" % escape_ldap_filter(value)]
54 elif lookup_type == 'startswith':
55 return ["%s*" % escape_ldap_filter(value)]
56 elif lookup_type in ['contains', 'icontains']:
57 return ["*%s*" % escape_ldap_filter(value)]
58 elif lookup_type == 'exact':
59 return [escape_ldap_filter(value)]
60 elif lookup_type == 'in':
61 return [escape_ldap_filter(v) for v in value]
63 raise TypeError("CharField has invalid lookup: %s" % lookup_type)
65 def get_db_prep_save(self, value, connection):
66 return [value.encode(connection.charset)]
68 def get_prep_lookup(self, lookup_type, value):
69 "Perform preliminary non-db specific lookup checks and conversions"
70 if lookup_type == 'endswith':
71 return "*%s" % escape_ldap_filter(value)
72 elif lookup_type == 'startswith':
73 return "%s*" % escape_ldap_filter(value)
74 elif lookup_type in ['contains', 'icontains']:
75 return "*%s*" % escape_ldap_filter(value)
76 elif lookup_type == 'exact':
77 return escape_ldap_filter(value)
78 elif lookup_type == 'in':
79 return [escape_ldap_filter(v) for v in value]
81 raise TypeError("CharField has invalid lookup: %s" % lookup_type)
83 class ImageField(fields.Field):
84 def from_ldap(self, value, connection):
90 def get_db_prep_lookup(self, lookup_type, value):
91 "Returns field's value prepared for database lookup."
92 return [self.get_prep_lookup(lookup_type, value)]
94 def get_db_prep_save(self, value, connection):
97 def get_prep_lookup(self, lookup_type, value):
98 "Perform preliminary non-db specific lookup checks and conversions"
99 raise TypeError("ImageField has invalid lookup: %s" % lookup_type)
101 class IntegerField(fields.IntegerField):
102 def from_ldap(self, value, connection):
108 def get_db_prep_lookup(self, lookup_type, value):
109 "Returns field's value prepared for database lookup."
110 return [self.get_prep_lookup(lookup_type, value)]
112 def get_db_prep_save(self, value, connection):
115 def get_prep_lookup(self, lookup_type, value):
116 "Perform preliminary non-db specific lookup checks and conversions"
117 if lookup_type in ('exact', 'gte', 'lte'):
119 raise TypeError("IntegerField has invalid lookup: %s" % lookup_type)
121 class ListField(fields.Field):
122 __metaclass__ = SubfieldBase
124 def from_ldap(self, value, connection):
127 def get_db_prep_lookup(self, lookup_type, value):
128 "Returns field's value prepared for database lookup."
129 return [self.get_prep_lookup(lookup_type, value)]
131 def get_db_prep_save(self, value, connection):
132 return [x.encode(connection.charset) for x in value]
134 def get_prep_lookup(self, lookup_type, value):
135 "Perform preliminary non-db specific lookup checks and conversions"
136 if lookup_type == 'contains':
137 return escape_ldap_filter(value)
138 raise TypeError("ListField has invalid lookup: %s" % lookup_type)
140 def to_python(self, value):