fields.py~ 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. print value[0]
  45. return value[0].decode(connection.charset)
  46. def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
  47. "Returns field's value prepared for database lookup."
  48. if lookup_type == 'endswith':
  49. return ["*%s" % escape_ldap_filter(value)]
  50. elif lookup_type == 'startswith':
  51. return ["%s*" % escape_ldap_filter(value)]
  52. elif lookup_type in ['contains', 'icontains']:
  53. return ["*%s*" % escape_ldap_filter(value)]
  54. elif lookup_type == 'exact':
  55. return [escape_ldap_filter(value)]
  56. elif lookup_type == 'in':
  57. return [escape_ldap_filter(v) for v in value]
  58. raise TypeError("CharField has invalid lookup: %s" % lookup_type)
  59. def get_db_prep_save(self, value, connection):
  60. return [value.encode(connection.charset)]
  61. def get_prep_lookup(self, lookup_type, value):
  62. "Perform preliminary non-db specific lookup checks and conversions"
  63. if lookup_type == 'endswith':
  64. return "*%s" % escape_ldap_filter(value)
  65. elif lookup_type == 'startswith':
  66. return "%s*" % escape_ldap_filter(value)
  67. elif lookup_type in ['contains', 'icontains']:
  68. return "*%s*" % escape_ldap_filter(value)
  69. elif lookup_type == 'exact':
  70. return escape_ldap_filter(value)
  71. elif lookup_type == 'in':
  72. return [escape_ldap_filter(v) for v in value]
  73. raise TypeError("CharField has invalid lookup: %s" % lookup_type)
  74. class ImageField(fields.Field):
  75. def from_ldap(self, value, connection):
  76. if len(value) == 0:
  77. return ''
  78. else:
  79. return value[0]
  80. def get_db_prep_lookup(self, lookup_type, value, connection, 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, prepared=False):
  95. "Returns field's value prepared for database lookup."
  96. return [self.get_prep_lookup(lookup_type, value)]
  97. def get_db_prep_save(self, value, connection):
  98. return [str(value)]
  99. def get_prep_lookup(self, lookup_type, value):
  100. "Perform preliminary non-db specific lookup checks and conversions"
  101. if lookup_type in ('exact', 'gte', 'lte'):
  102. return value
  103. raise TypeError("IntegerField has invalid lookup: %s" % lookup_type)
  104. class ListField(fields.Field):
  105. __metaclass__ = SubfieldBase
  106. def from_ldap(self, value, connection):
  107. return value
  108. def get_db_prep_lookup(self, lookup_type, value, connection, prepared=False):
  109. "Returns field's value prepared for database lookup."
  110. return [self.get_prep_lookup(lookup_type, value)]
  111. def get_db_prep_save(self, value, connection):
  112. return [x.encode(connection.charset) for x in value]
  113. def get_prep_lookup(self, lookup_type, value):
  114. "Perform preliminary non-db specific lookup checks and conversions"
  115. if lookup_type == 'contains':
  116. return escape_ldap_filter(value)
  117. raise TypeError("ListField has invalid lookup: %s" % lookup_type)
  118. def to_python(self, value):
  119. if not value:
  120. return []
  121. return value