fields.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. # -*- coding: utf-8 -*-
  2. #
  3. # django-ldapdb
  4. # Copyright (c) 2009-2011, Bolloré telecom
  5. # Copyright (c) 2013, Jeremy Lainé
  6. # All rights reserved.
  7. #
  8. # See AUTHORS file for a full list of contributors.
  9. #
  10. # Redistribution and use in source and binary forms, with or without
  11. # modification, are permitted provided that the following conditions are met:
  12. #
  13. # 1. Redistributions of source code must retain the above copyright notice,
  14. # this list of conditions and the following disclaimer.
  15. #
  16. # 2. Redistributions in binary form must reproduce the above copyright
  17. # notice, this list of conditions and the following disclaimer in the
  18. # documentation and/or other materials provided with the distribution.
  19. #
  20. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21. # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  24. # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25. # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26. # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27. # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30. # POSSIBILITY OF SUCH DAMAGE.
  31. #
  32. from django.db.models import fields, SubfieldBase
  33. from ldapdb import escape_ldap_filter
  34. import datetime
  35. class CharField(fields.CharField):
  36. def __init__(self, *args, **kwargs):
  37. kwargs['max_length'] = 200
  38. super(CharField, self).__init__(*args, **kwargs)
  39. def from_ldap(self, value, connection):
  40. if len(value) == 0:
  41. return ''
  42. else:
  43. return value[0].decode(connection.charset)
  44. def get_db_prep_lookup(self, lookup_type, value, connection,
  45. prepared=False):
  46. "Returns field's value prepared for database lookup."
  47. if lookup_type == 'endswith':
  48. return ["*%s" % escape_ldap_filter(value)]
  49. elif lookup_type == 'startswith':
  50. return ["%s*" % escape_ldap_filter(value)]
  51. elif lookup_type in ['contains', 'icontains']:
  52. return ["*%s*" % escape_ldap_filter(value)]
  53. elif lookup_type == 'exact':
  54. return [escape_ldap_filter(value)]
  55. elif lookup_type == 'in':
  56. return [escape_ldap_filter(v) for v in value]
  57. raise TypeError("CharField has invalid lookup: %s" % lookup_type)
  58. def get_db_prep_save(self, value, connection):
  59. return [value.encode(connection.charset)]
  60. def get_prep_lookup(self, lookup_type, value):
  61. "Perform preliminary non-db specific lookup checks and conversions"
  62. if lookup_type == 'endswith':
  63. return "*%s" % escape_ldap_filter(value)
  64. elif lookup_type == 'startswith':
  65. return "%s*" % escape_ldap_filter(value)
  66. elif lookup_type in ['contains', 'icontains']:
  67. return "*%s*" % escape_ldap_filter(value)
  68. elif lookup_type == 'exact':
  69. return escape_ldap_filter(value)
  70. elif lookup_type == 'in':
  71. return [escape_ldap_filter(v) for v in value]
  72. raise TypeError("CharField has invalid lookup: %s" % lookup_type)
  73. class ImageField(fields.Field):
  74. def from_ldap(self, value, connection):
  75. if len(value) == 0:
  76. return ''
  77. else:
  78. return value[0]
  79. def get_db_prep_lookup(self, lookup_type, value, connection,
  80. prepared=False):
  81. "Returns field's value prepared for database lookup."
  82. return [self.get_prep_lookup(lookup_type, value)]
  83. def get_db_prep_save(self, value, connection):
  84. return [value]
  85. def get_prep_lookup(self, lookup_type, value):
  86. "Perform preliminary non-db specific lookup checks and conversions"
  87. raise TypeError("ImageField has invalid lookup: %s" % lookup_type)
  88. class IntegerField(fields.IntegerField):
  89. def from_ldap(self, value, connection):
  90. if len(value) == 0:
  91. return 0
  92. else:
  93. return int(value[0])
  94. def get_db_prep_lookup(self, lookup_type, value, connection,
  95. prepared=False):
  96. "Returns field's value prepared for database lookup."
  97. return [self.get_prep_lookup(lookup_type, value)]
  98. def get_db_prep_save(self, value, connection):
  99. return [str(value)]
  100. def get_prep_lookup(self, lookup_type, value):
  101. "Perform preliminary non-db specific lookup checks and conversions"
  102. if lookup_type in ('exact', 'gte', 'lte'):
  103. return value
  104. raise TypeError("IntegerField has invalid lookup: %s" % lookup_type)
  105. class FloatField(fields.FloatField):
  106. def from_ldap(self, value, connection):
  107. if len(value) == 0:
  108. return 0.0
  109. else:
  110. return float(value[0])
  111. def get_db_prep_lookup(self, lookup_type, value, connection,
  112. prepared=False):
  113. "Returns field's value prepared for database lookup."
  114. return [self.get_prep_lookup(lookup_type, value)]
  115. def get_db_prep_save(self, value, connection):
  116. return [str(value)]
  117. def get_prep_lookup(self, lookup_type, value):
  118. "Perform preliminary non-db specific lookup checks and conversions"
  119. if lookup_type in ('exact', 'gte', 'lte'):
  120. return value
  121. raise TypeError("FloatField has invalid lookup: %s" % lookup_type)
  122. class ListField(fields.Field):
  123. __metaclass__ = SubfieldBase
  124. def from_ldap(self, value, connection):
  125. return value
  126. def get_db_prep_lookup(self, lookup_type, value, connection,
  127. prepared=False):
  128. "Returns field's value prepared for database lookup."
  129. return [self.get_prep_lookup(lookup_type, value)]
  130. def get_db_prep_save(self, value, connection):
  131. return [x.encode(connection.charset) for x in value]
  132. def get_prep_lookup(self, lookup_type, value):
  133. "Perform preliminary non-db specific lookup checks and conversions"
  134. if lookup_type == 'contains':
  135. return escape_ldap_filter(value)
  136. raise TypeError("ListField has invalid lookup: %s" % lookup_type)
  137. def to_python(self, value):
  138. if not value:
  139. return []
  140. return value
  141. class DateField(fields.DateField):
  142. """
  143. A text field containing date, in specified format.
  144. The format can be specified as 'format' argument, as strptime()
  145. format string. It defaults to ISO8601 (%Y-%m-%d).
  146. Note: 'lte' and 'gte' lookups are done string-wise. Therefore,
  147. they will onlywork correctly on Y-m-d dates with constant
  148. component widths.
  149. """
  150. def __init__(self, *args, **kwargs):
  151. if 'format' in kwargs:
  152. self._date_format = kwargs.pop('format')
  153. else:
  154. self._date_format = '%Y-%m-%d'
  155. super(DateField, self).__init__(*args, **kwargs)
  156. def from_ldap(self, value, connection):
  157. if len(value) == 0:
  158. return None
  159. else:
  160. return datetime.datetime.strptime(value[0],
  161. self._date_format).date()
  162. def get_db_prep_lookup(self, lookup_type, value, connection,
  163. prepared=False):
  164. "Returns field's value prepared for database lookup."
  165. return [self.get_prep_lookup(lookup_type, value)]
  166. def get_db_prep_save(self, value, connection):
  167. if not isinstance(value, datetime.date) \
  168. and not isinstance(value, datetime.datetime):
  169. raise ValueError(
  170. 'DateField can be only set to a datetime.date instance')
  171. return [value.strftime(self._date_format)]
  172. def get_prep_lookup(self, lookup_type, value):
  173. "Perform preliminary non-db specific lookup checks and conversions"
  174. if lookup_type in ('exact',):
  175. return value
  176. raise TypeError("DateField has invalid lookup: %s" % lookup_type)