fields.py 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. # -*- coding: utf-8 -*-
  2. #
  3. # django-ldapdb
  4. # Copyright (c) 2009-2011, Bolloré telecom
  5. # All rights reserved.
  6. #
  7. # See AUTHORS file for a full list of contributors.
  8. #
  9. # Redistribution and use in source and binary forms, with or without modification,
  10. # are permitted provided that the following conditions are met:
  11. #
  12. # 1. Redistributions of source code must retain the above copyright notice,
  13. # this list of conditions and the following disclaimer.
  14. #
  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.
  18. #
  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.
  22. #
  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.
  33. #
  34. from django.db.models import fields, SubfieldBase
  35. from ldapdb import escape_ldap_filter
  36. class CharField(fields.CharField):
  37. def __init__(self, *args, **kwargs):
  38. kwargs['max_length'] = 200
  39. super(CharField, self).__init__(*args, **kwargs)
  40. def from_ldap(self, value, connection):
  41. if len(value) == 0:
  42. return ''
  43. else:
  44. return value[0].decode(connection.charset)
  45. def get_db_prep_lookup(self, lookup_type, value, connection, 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, prepared=False):
  80. "Returns field's value prepared for database lookup."
  81. return [self.get_prep_lookup(lookup_type, value)]
  82. def get_db_prep_save(self, value, connection):
  83. return [value]
  84. def get_prep_lookup(self, lookup_type, value):
  85. "Perform preliminary non-db specific lookup checks and conversions"
  86. raise TypeError("ImageField has invalid lookup: %s" % lookup_type)
  87. class IntegerField(fields.IntegerField):
  88. def from_ldap(self, value, connection):
  89. if len(value) == 0:
  90. return 0
  91. else:
  92. return int(value[0])
  93. def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
  94. "Returns field's value prepared for database lookup."
  95. return [self.get_prep_lookup(lookup_type, value)]
  96. def get_db_prep_save(self, value, connection):
  97. return [str(value)]
  98. def get_prep_lookup(self, lookup_type, value):
  99. "Perform preliminary non-db specific lookup checks and conversions"
  100. if lookup_type in ('exact', 'gte', 'lte'):
  101. return value
  102. raise TypeError("IntegerField has invalid lookup: %s" % lookup_type)
  103. class ListField(fields.Field):
  104. __metaclass__ = SubfieldBase
  105. def from_ldap(self, value, connection):
  106. return value
  107. def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
  108. "Returns field's value prepared for database lookup."
  109. return [self.get_prep_lookup(lookup_type, value)]
  110. def get_db_prep_save(self, value, connection):
  111. return [x.encode(connection.charset) for x in value]
  112. def get_prep_lookup(self, lookup_type, value):
  113. "Perform preliminary non-db specific lookup checks and conversions"
  114. if lookup_type == 'contains':
  115. return escape_ldap_filter(value)
  116. raise TypeError("ListField has invalid lookup: %s" % lookup_type)
  117. def to_python(self, value):
  118. if not value:
  119. return []
  120. return value