fields.py 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. """
  2. Custom Django Model Fields.
  3. """
  4. from copy import deepcopy
  5. from base64 import b64encode, b64decode
  6. from zlib import compress, decompress
  7. try:
  8. import cPickle as pickle
  9. except ImportError:
  10. import pickle
  11. from django.db import models
  12. from django.utils.encoding import force_unicode
  13. class PickledObject(str):
  14. """A subclass of string so it can be told whether a string is a pickled
  15. object or not (if the object is an instance of this class then it must
  16. [well, should] be a pickled one).
  17. Only really useful for passing pre-encoded values to ``default``
  18. with ``dbsafe_encode``, not that doing so is necessary. If you
  19. remove PickledObject and its references, you won't be able to pass
  20. in pre-encoded values anymore, but you can always just pass in the
  21. python objects themselves.
  22. """
  23. pass
  24. def dbsafe_encode(value, compress_object=False):
  25. """We use deepcopy() here to avoid a problem with cPickle, where dumps
  26. can generate different character streams for same lookup value if
  27. they are referenced differently.
  28. The reason this is important is because we do all of our lookups as
  29. simple string matches, thus the character streams must be the same
  30. for the lookups to work properly. See tests.py for more
  31. information.
  32. """
  33. if not compress_object:
  34. value = b64encode(pickle.dumps(deepcopy(value)))
  35. else:
  36. value = b64encode(compress(pickle.dumps(deepcopy(value))))
  37. return PickledObject(value)
  38. def dbsafe_decode(value, compress_object=False):
  39. if not compress_object:
  40. value = pickle.loads(b64decode(value))
  41. else:
  42. value = pickle.loads(decompress(b64decode(value)))
  43. return value
  44. class PickledObjectField(models.Field):
  45. """A field that will accept *any* python object and store it in the
  46. database. PickledObjectField will optionally compress it's values if
  47. declared with the keyword argument ``compress=True``.
  48. Does not actually encode and compress ``None`` objects (although you
  49. can still do lookups using None). This way, it is still possible to
  50. use the ``isnull`` lookup type correctly. Because of this, the field
  51. defaults to ``null=True``, as otherwise it wouldn't be able to store
  52. None values since they aren't pickled and encoded.
  53. """
  54. __metaclass__ = models.SubfieldBase
  55. def __init__(self, *args, **kwargs):
  56. self.compress = kwargs.pop('compress', False)
  57. self.protocol = kwargs.pop('protocol', 2)
  58. kwargs.setdefault('null', True)
  59. kwargs.setdefault('editable', False)
  60. super(PickledObjectField, self).__init__(*args, **kwargs)
  61. def get_default(self):
  62. """Returns the default value for this field.
  63. The default implementation on models.Field calls force_unicode
  64. on the default, which means you can't set arbitrary Python
  65. objects as the default. To fix this, we just return the value
  66. without calling force_unicode on it. Note that if you set a
  67. callable as a default, the field will still call it. It will
  68. *not* try to pickle and encode it.
  69. """
  70. if self.has_default():
  71. if callable(self.default):
  72. return self.default()
  73. return self.default
  74. # If the field doesn't have a default, then we punt to models.Field.
  75. return super(PickledObjectField, self).get_default()
  76. def to_python(self, value):
  77. """B64decode and unpickle the object, optionally decompressing it.
  78. If an error is raised in de-pickling and we're sure the value is
  79. a definite pickle, the error is allowed to propogate. If we
  80. aren't sure if the value is a pickle or not, then we catch the
  81. error and return the original value instead.
  82. """
  83. if value is not None:
  84. try:
  85. value = dbsafe_decode(value, self.compress)
  86. except:
  87. # If the value is a definite pickle; and an error is raised in
  88. # de-pickling it should be allowed to propogate.
  89. if isinstance(value, PickledObject):
  90. raise
  91. return value
  92. def get_db_prep_value(self, value):
  93. """Pickle and b64encode the object, optionally compressing it.
  94. The pickling protocol is specified explicitly (by default 2),
  95. rather than as -1 or HIGHEST_PROTOCOL, because we don't want the
  96. protocol to change over time. If it did, ``exact`` and ``in``
  97. lookups would likely fail, since pickle would now be generating
  98. a different string.
  99. """
  100. if value is not None and not isinstance(value, PickledObject):
  101. # We call force_unicode here explicitly, so that the encoded
  102. # string isn't rejected by the postgresql_psycopg2 backend.
  103. # Alternatively, we could have just registered PickledObject with
  104. # the psycopg marshaller (telling it to store it like it would a
  105. # string), but since both of these methods result in the same
  106. # value being stored, doing things this way is much easier.
  107. value = force_unicode(dbsafe_encode(value, self.compress))
  108. return value
  109. def value_to_string(self, obj):
  110. value = self._get_val_from_obj(obj)
  111. return self.get_db_prep_value(value)
  112. def get_internal_type(self):
  113. return 'TextField'
  114. def get_db_prep_lookup(self, lookup_type, value):
  115. if lookup_type not in ['exact', 'in', 'isnull']:
  116. raise TypeError('Lookup type %s is not supported.' % lookup_type)
  117. # The Field model already calls get_db_prep_value before doing the
  118. # actual lookup, so all we need to do is limit the lookup types.
  119. return super(PickledObjectField, self).get_db_prep_lookup(lookup_type,
  120. value)