django-picklefield-0.3.1/0000775000175000017500000000000012241510545016023 5ustar gintasgintas00000000000000django-picklefield-0.3.1/setup.cfg0000664000175000017500000000007312241510545017644 0ustar gintasgintas00000000000000[egg_info] tag_build = tag_date = 0 tag_svn_revision = 0 django-picklefield-0.3.1/PKG-INFO0000664000175000017500000002160412241510545017123 0ustar gintasgintas00000000000000Metadata-Version: 1.1 Name: django-picklefield Version: 0.3.1 Summary: Pickled object field for Django Home-page: http://github.com/gintas/django-picklefield Author: Gintautas Miliauskas Author-email: gintautas@miliauskas.lt License: UNKNOWN Description: ----- About ----- **django-picklefield** provides an implementation of a pickled object field. Such fields can contain any picklable objects. The implementation is taken and adopted from Django snippet #1694 by Taavi Taijala, which is in turn based on Django snippet #513 by Oliver Beattie. django-picklefield is available under the MIT license. ----- Usage ----- First of all, you need to have **django-picklefield** installed; for your convenience, recent versions should be available from PyPI. To use, just define a field in your model:: >>> from picklefield.fields import PickledObjectField ... class SomeObject(models.Model): ... args = PickledObjectField() and assign whatever you like (as long as it's picklable) to the field:: >>> obj = SomeObject() >>> obj.args = ['fancy', {'objects': 'inside'}] >>> obj.save() ----- Notes ----- If you need to serialize an object with a PickledObjectField for transmission to the browser, you may need to subclass the field and override the ``value_to_string()`` method. Currently pickle fields are serialized as base64-encoded pickles, which allows reliable deserialization, but such a format is not convenient for parsing in the browser. By overriding ``value_to_string()`` you can choose a more convenient serialization format. -------------- Original notes -------------- Here are the notes by taavi223, the original author: Incredibly useful for storing just about anything in the database (provided it is Pickle-able, of course) when there isn't a 'proper' field for the job. PickledObjectField is database-agnostic, and should work with any database backend you can throw at it. You can pass in any Python object and it will automagically be converted behind the scenes. You never have to manually pickle or unpickle anything. Also works fine when querying; supports exact, in, and isnull lookups. It should be noted, however, that calling QuerySet.values() will only return the encoded data, not the original Python object. This PickledObjectField has a few improvements over the one in snippet #513. This one solves the DjangoUnicodeDecodeError problem when saving an object containing non-ASCII data by base64 encoding the pickled output stream. This ensures that all stored data is ASCII, eliminating the problem. PickledObjectField will now optionally use zlib to compress (and uncompress) pickled objects on the fly. This can be set per-field using the keyword argument "compress=True". For most items this is probably not worth the small performance penalty, but for Models with larger objects, it can be a real space saver. You can also now specify the pickle protocol per-field, using the protocol keyword argument. The default of 2 should always work, unless you are trying to access the data from outside of the Django ORM. Worked around a rare issue when using the cPickle and performing lookups of complex data types. In short, cPickle would sometimes output different streams for the same object depending on how it was referenced. This of course could cause lookups for complex objects to fail, even when a matching object exists. See the docstrings and tests for more information. You can now use the isnull lookup and have it function as expected. A consequence of this is that by default, PickledObjectField has null=True set (you can of course pass null=False if you want to change that). If null=False is set (the default for fields), then you wouldn't be able to store a Python None value, since None values aren't pickled or encoded (this in turn is what makes the isnull lookup possible). You can now pass in an object as the default argument for the field without it being converted to a unicode string first. If you pass in a callable though, the field will still call it. It will not try to pickle and encode it. You can manually import dbsafe_encode and dbsafe_decode from fields.py if you want to encode and decode objects yourself. This is mostly useful for decoding values returned from calling QuerySet.values(), which are still encoded strings. Note: If you are trying to store other django models in the PickledObjectField, please see the comments for a discussion on the problems associated with doing that. The easy solution is to put django models into a list or tuple before assigning them to the PickledObjectField. Update 9/2/09: Fixed the value_to_string method so that serialization should now work as expected. Also added deepcopy back into dbsafe_encode, fixing #4 above, since deepcopy had somehow managed to remove itself. This means that lookups should once again work as expected in all situations. Also made the field editable=False by default (which I swear I already did once before!) since it is never a good idea to have a PickledObjectField be user editable. ------- Changes ------- Changes in version 0.3.1 ======================== * Favor the built in json module (thanks to Simon Charette). Changes in version 0.3.0 ======================== * Python 3 support (thanks to Rafal Stozek). Changes in version 0.2.0 ======================== * Allow pickling of subclasses of django.db.models.Model (thanks to Simon Charette). Changes in version 0.1.9 ======================== * Added `connection` and `prepared` parameters to `get_db_prep_value()` too (thanks to Matthew Schinckel). Changes in version 0.1.8 ======================== * Updated link to code repository. Changes in version 0.1.7 ======================== * Added `connection` and `prepared` parameters to `get_db_prep_lookup()` to get rid of deprecation warnings in Django 1.2. Changes in version 0.1.6 ======================== * Fixed South support (thanks aehlke@github). Changes in version 0.1.5 ======================== * Added support for South. * Changed default to null=False, as is common throughout Django. Changes in version 0.1.4 ======================== * Updated copyright statements. Changes in version 0.1.3 ======================== * Updated serialization tests (thanks to Michael Fladischer). Changes in version 0.1.2 ======================== * Added Simplified BSD licence. Changes in version 0.1.1 ======================== * Added test for serialization. * Added note about JSON serialization for browser. * Added support for different pickle protocol versions (thanks to Michael Fladischer). Changes in version 0.1 ====================== * First public release. -------- Feedback -------- There is a home page with instructions on how to access the code repository. Send feedback and suggestions to gintautas@miliauskas.lt . Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Framework :: Django Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2.5 Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.2 Classifier: Programming Language :: Python :: 3.3 django-picklefield-0.3.1/README0000664000175000017500000001515612241507660016717 0ustar gintasgintas00000000000000----- About ----- **django-picklefield** provides an implementation of a pickled object field. Such fields can contain any picklable objects. The implementation is taken and adopted from Django snippet #1694 by Taavi Taijala, which is in turn based on Django snippet #513 by Oliver Beattie. django-picklefield is available under the MIT license. ----- Usage ----- First of all, you need to have **django-picklefield** installed; for your convenience, recent versions should be available from PyPI. To use, just define a field in your model:: >>> from picklefield.fields import PickledObjectField ... class SomeObject(models.Model): ... args = PickledObjectField() and assign whatever you like (as long as it's picklable) to the field:: >>> obj = SomeObject() >>> obj.args = ['fancy', {'objects': 'inside'}] >>> obj.save() ----- Notes ----- If you need to serialize an object with a PickledObjectField for transmission to the browser, you may need to subclass the field and override the ``value_to_string()`` method. Currently pickle fields are serialized as base64-encoded pickles, which allows reliable deserialization, but such a format is not convenient for parsing in the browser. By overriding ``value_to_string()`` you can choose a more convenient serialization format. -------------- Original notes -------------- Here are the notes by taavi223, the original author: Incredibly useful for storing just about anything in the database (provided it is Pickle-able, of course) when there isn't a 'proper' field for the job. PickledObjectField is database-agnostic, and should work with any database backend you can throw at it. You can pass in any Python object and it will automagically be converted behind the scenes. You never have to manually pickle or unpickle anything. Also works fine when querying; supports exact, in, and isnull lookups. It should be noted, however, that calling QuerySet.values() will only return the encoded data, not the original Python object. This PickledObjectField has a few improvements over the one in snippet #513. This one solves the DjangoUnicodeDecodeError problem when saving an object containing non-ASCII data by base64 encoding the pickled output stream. This ensures that all stored data is ASCII, eliminating the problem. PickledObjectField will now optionally use zlib to compress (and uncompress) pickled objects on the fly. This can be set per-field using the keyword argument "compress=True". For most items this is probably not worth the small performance penalty, but for Models with larger objects, it can be a real space saver. You can also now specify the pickle protocol per-field, using the protocol keyword argument. The default of 2 should always work, unless you are trying to access the data from outside of the Django ORM. Worked around a rare issue when using the cPickle and performing lookups of complex data types. In short, cPickle would sometimes output different streams for the same object depending on how it was referenced. This of course could cause lookups for complex objects to fail, even when a matching object exists. See the docstrings and tests for more information. You can now use the isnull lookup and have it function as expected. A consequence of this is that by default, PickledObjectField has null=True set (you can of course pass null=False if you want to change that). If null=False is set (the default for fields), then you wouldn't be able to store a Python None value, since None values aren't pickled or encoded (this in turn is what makes the isnull lookup possible). You can now pass in an object as the default argument for the field without it being converted to a unicode string first. If you pass in a callable though, the field will still call it. It will not try to pickle and encode it. You can manually import dbsafe_encode and dbsafe_decode from fields.py if you want to encode and decode objects yourself. This is mostly useful for decoding values returned from calling QuerySet.values(), which are still encoded strings. Note: If you are trying to store other django models in the PickledObjectField, please see the comments for a discussion on the problems associated with doing that. The easy solution is to put django models into a list or tuple before assigning them to the PickledObjectField. Update 9/2/09: Fixed the value_to_string method so that serialization should now work as expected. Also added deepcopy back into dbsafe_encode, fixing #4 above, since deepcopy had somehow managed to remove itself. This means that lookups should once again work as expected in all situations. Also made the field editable=False by default (which I swear I already did once before!) since it is never a good idea to have a PickledObjectField be user editable. ------- Changes ------- Changes in version 0.3.1 ======================== * Favor the built in json module (thanks to Simon Charette). Changes in version 0.3.0 ======================== * Python 3 support (thanks to Rafal Stozek). Changes in version 0.2.0 ======================== * Allow pickling of subclasses of django.db.models.Model (thanks to Simon Charette). Changes in version 0.1.9 ======================== * Added `connection` and `prepared` parameters to `get_db_prep_value()` too (thanks to Matthew Schinckel). Changes in version 0.1.8 ======================== * Updated link to code repository. Changes in version 0.1.7 ======================== * Added `connection` and `prepared` parameters to `get_db_prep_lookup()` to get rid of deprecation warnings in Django 1.2. Changes in version 0.1.6 ======================== * Fixed South support (thanks aehlke@github). Changes in version 0.1.5 ======================== * Added support for South. * Changed default to null=False, as is common throughout Django. Changes in version 0.1.4 ======================== * Updated copyright statements. Changes in version 0.1.3 ======================== * Updated serialization tests (thanks to Michael Fladischer). Changes in version 0.1.2 ======================== * Added Simplified BSD licence. Changes in version 0.1.1 ======================== * Added test for serialization. * Added note about JSON serialization for browser. * Added support for different pickle protocol versions (thanks to Michael Fladischer). Changes in version 0.1 ====================== * First public release. -------- Feedback -------- There is a home page with instructions on how to access the code repository. Send feedback and suggestions to gintautas@miliauskas.lt . django-picklefield-0.3.1/src/0000775000175000017500000000000012241510545016612 5ustar gintasgintas00000000000000django-picklefield-0.3.1/src/picklefield/0000775000175000017500000000000012241510545021065 5ustar gintasgintas00000000000000django-picklefield-0.3.1/src/picklefield/tests.py0000664000175000017500000002050612061052746022610 0ustar gintasgintas00000000000000"""Unit tests for django-picklefield.""" from django.test import TestCase from django.db import models from django.core import serializers from picklefield.compat import json from picklefield.fields import (PickledObjectField, wrap_conflictual_object, dbsafe_encode) S1 = 'Hello World' T1 = (1, 2, 3, 4, 5) L1 = [1, 2, 3, 4, 5] D1 = {1: 1, 2: 4, 3: 6, 4: 8, 5: 10} D2 = {1: 2, 2: 4, 3: 6, 4: 8, 5: 10} class TestingModel(models.Model): pickle_field = PickledObjectField() compressed_pickle_field = PickledObjectField(compress=True) default_pickle_field = PickledObjectField(default=(D1, S1, T1, L1)) class MinimalTestingModel(models.Model): pickle_field = PickledObjectField() class TestCustomDataType(str): pass class PickledObjectFieldTests(TestCase): def setUp(self): self.testing_data = (D2, S1, T1, L1, TestCustomDataType(S1), MinimalTestingModel) return super(PickledObjectFieldTests, self).setUp() def testDataIntegrity(self): """ Tests that data remains the same when saved to and fetched from the database, whether compression is enabled or not. """ for value in self.testing_data: model_test = TestingModel(pickle_field=value, compressed_pickle_field=value) model_test.save() model_test = TestingModel.objects.get(id__exact=model_test.id) # Make sure that both the compressed and uncompressed fields return # the same data, even thought it's stored differently in the DB. self.assertEquals(value, model_test.pickle_field) self.assertEquals(value, model_test.compressed_pickle_field) # Make sure we can also retrieve the model model_test.save() model_test.delete() # Make sure the default value for default_pickled_field gets stored # correctly and that it isn't converted to a string. model_test = TestingModel() model_test.save() model_test = TestingModel.objects.get(id__exact=model_test.id) self.assertEquals((D1, S1, T1, L1), model_test.default_pickle_field) def testLookups(self): """ Tests that lookups can be performed on data once stored in the database, whether compression is enabled or not. One problem with cPickle is that it will sometimes output different streams for the same object, depending on how they are referenced. It should be noted though, that this does not happen for every object, but usually only with more complex ones. >>> from pickle import dumps >>> t = ({1: 1, 2: 4, 3: 6, 4: 8, 5: 10}, \ ... 'Hello World', (1, 2, 3, 4, 5), [1, 2, 3, 4, 5]) >>> dumps(({1: 1, 2: 4, 3: 6, 4: 8, 5: 10}, \ ... 'Hello World', (1, 2, 3, 4, 5), [1, 2, 3, 4, 5])) "((dp0\nI1\nI1\nsI2\nI4\nsI3\nI6\nsI4\nI8\nsI5\nI10\nsS'Hello World'\np1\n(I1\nI2\nI3\nI4\nI5\ntp2\n(lp3\nI1\naI2\naI3\naI4\naI5\natp4\n." >>> dumps(t) "((dp0\nI1\nI1\nsI2\nI4\nsI3\nI6\nsI4\nI8\nsI5\nI10\nsS'Hello World'\np1\n(I1\nI2\nI3\nI4\nI5\ntp2\n(lp3\nI1\naI2\naI3\naI4\naI5\natp4\n." >>> # Both dumps() are the same using pickle. >>> from cPickle import dumps >>> t = ({1: 1, 2: 4, 3: 6, 4: 8, 5: 10}, 'Hello World', (1, 2, 3, 4, 5), [1, 2, 3, 4, 5]) >>> dumps(({1: 1, 2: 4, 3: 6, 4: 8, 5: 10}, 'Hello World', (1, 2, 3, 4, 5), [1, 2, 3, 4, 5])) "((dp1\nI1\nI1\nsI2\nI4\nsI3\nI6\nsI4\nI8\nsI5\nI10\nsS'Hello World'\np2\n(I1\nI2\nI3\nI4\nI5\ntp3\n(lp4\nI1\naI2\naI3\naI4\naI5\nat." >>> dumps(t) "((dp1\nI1\nI1\nsI2\nI4\nsI3\nI6\nsI4\nI8\nsI5\nI10\nsS'Hello World'\n(I1\nI2\nI3\nI4\nI5\nt(lp2\nI1\naI2\naI3\naI4\naI5\natp3\n." >>> # But with cPickle the two dumps() are not the same! >>> # Both will generate the same object when loads() is called though. We can solve this by calling deepcopy() on the value before pickling it, as this copies everything to a brand new data structure. >>> from cPickle import dumps >>> from copy import deepcopy >>> t = ({1: 1, 2: 4, 3: 6, 4: 8, 5: 10}, 'Hello World', (1, 2, 3, 4, 5), [1, 2, 3, 4, 5]) >>> dumps(deepcopy(({1: 1, 2: 4, 3: 6, 4: 8, 5: 10}, 'Hello World', (1, 2, 3, 4, 5), [1, 2, 3, 4, 5]))) "((dp1\nI1\nI1\nsI2\nI4\nsI3\nI6\nsI4\nI8\nsI5\nI10\nsS'Hello World'\np2\n(I1\nI2\nI3\nI4\nI5\ntp3\n(lp4\nI1\naI2\naI3\naI4\naI5\nat." >>> dumps(deepcopy(t)) "((dp1\nI1\nI1\nsI2\nI4\nsI3\nI6\nsI4\nI8\nsI5\nI10\nsS'Hello World'\np2\n(I1\nI2\nI3\nI4\nI5\ntp3\n(lp4\nI1\naI2\naI3\naI4\naI5\nat." >>> # Using deepcopy() beforehand means that now both dumps() are idential. >>> # It may not be necessary, but deepcopy() ensures that lookups will always work. Unfortunately calling copy() alone doesn't seem to fix the problem as it lies primarily with complex data types. >>> from cPickle import dumps >>> from copy import copy >>> t = ({1: 1, 2: 4, 3: 6, 4: 8, 5: 10}, 'Hello World', (1, 2, 3, 4, 5), [1, 2, 3, 4, 5]) >>> dumps(copy(({1: 1, 2: 4, 3: 6, 4: 8, 5: 10}, 'Hello World', (1, 2, 3, 4, 5), [1, 2, 3, 4, 5]))) "((dp1\nI1\nI1\nsI2\nI4\nsI3\nI6\nsI4\nI8\nsI5\nI10\nsS'Hello World'\np2\n(I1\nI2\nI3\nI4\nI5\ntp3\n(lp4\nI1\naI2\naI3\naI4\naI5\nat." >>> dumps(copy(t)) "((dp1\nI1\nI1\nsI2\nI4\nsI3\nI6\nsI4\nI8\nsI5\nI10\nsS'Hello World'\n(I1\nI2\nI3\nI4\nI5\nt(lp2\nI1\naI2\naI3\naI4\naI5\natp3\n." """ for value in self.testing_data: model_test = TestingModel(pickle_field=value, compressed_pickle_field=value) model_test.save() # Make sure that we can do an ``exact`` lookup by both the # pickle_field and the compressed_pickle_field. wrapped_value = wrap_conflictual_object(value) model_test = TestingModel.objects.get(pickle_field__exact=wrapped_value, compressed_pickle_field__exact=wrapped_value) self.assertEquals(value, model_test.pickle_field) self.assertEquals(value, model_test.compressed_pickle_field) # Make sure that ``in`` lookups also work correctly. model_test = TestingModel.objects.get(pickle_field__in=[wrapped_value], compressed_pickle_field__in=[wrapped_value]) self.assertEquals(value, model_test.pickle_field) self.assertEquals(value, model_test.compressed_pickle_field) # Make sure that ``is_null`` lookups are working. self.assertEquals(1, TestingModel.objects.filter(pickle_field__isnull=False).count()) self.assertEquals(0, TestingModel.objects.filter(pickle_field__isnull=True).count()) model_test.delete() # Make sure that lookups of the same value work, even when referenced # differently. See the above docstring for more info on the issue. value = (D1, S1, T1, L1) model_test = TestingModel(pickle_field=value, compressed_pickle_field=value) model_test.save() # Test lookup using an assigned variable. model_test = TestingModel.objects.get(pickle_field__exact=value) self.assertEquals(value, model_test.pickle_field) # Test lookup using direct input of a matching value. model_test = TestingModel.objects.get( pickle_field__exact = (D1, S1, T1, L1), compressed_pickle_field__exact = (D1, S1, T1, L1), ) self.assertEquals(value, model_test.pickle_field) model_test.delete() def testSerialization(self): model = MinimalTestingModel(pickle_field={'foo': 'bar'}) serialized = serializers.serialize('json', [model]) data = json.loads(serialized) # determine output at runtime, because pickle output in python 3 # is different (but compatible with python 2) p = dbsafe_encode({'foo': 'bar'}) self.assertEquals(data, [{'pk': None, 'model': 'picklefield.minimaltestingmodel', 'fields': {"pickle_field": p}}]) for deserialized_test in serializers.deserialize('json', serialized): self.assertEquals(deserialized_test.object, model) django-picklefield-0.3.1/src/picklefield/__init__.py0000664000175000017500000000020212061052351023164 0ustar gintasgintas00000000000000"""Pickle field implementation for Django.""" DEFAULT_PROTOCOL = 2 from picklefield.fields import PickledObjectField # reexport django-picklefield-0.3.1/src/picklefield/compat.py0000664000175000017500000000103112241507344022720 0ustar gintasgintas00000000000000# django 1.5 introduces force_text instead of force_unicode try: from django.utils.encoding import force_text except ImportError: from django.utils.encoding import force_unicode as force_text # python 3.x does not have cPickle module try: from cPickle import loads, dumps # cpython 2.x except ImportError: from pickle import loads, dumps # cpython 3.x, other interpreters # django 1.6 will deprecate django.utils.simple_json try: import json except ImportError: from django.utils import simplejson as json django-picklefield-0.3.1/src/picklefield/models.py0000664000175000017500000000000012061052351022704 0ustar gintasgintas00000000000000django-picklefield-0.3.1/src/picklefield/fields.py0000664000175000017500000001703212061052746022714 0ustar gintasgintas00000000000000"""Pickle field implementation for Django.""" from copy import deepcopy from base64 import b64encode, b64decode from zlib import compress, decompress import six import django from django.db import models from picklefield import DEFAULT_PROTOCOL from picklefield.compat import force_text, loads, dumps class PickledObject(str): """ A subclass of string so it can be told whether a string is a pickled object or not (if the object is an instance of this class then it must [well, should] be a pickled one). Only really useful for passing pre-encoded values to ``default`` with ``dbsafe_encode``, not that doing so is necessary. If you remove PickledObject and its references, you won't be able to pass in pre-encoded values anymore, but you can always just pass in the python objects themselves. """ class _ObjectWrapper(object): """ A class used to wrap object that have properties that may clash with the ORM internals. For example, objects with the `prepare_database_save` property such as `django.db.Model` subclasses won't work under certain conditions and the same apply for trying to retrieve any `callable` object. """ __slots__ = ('_obj',) def __init__(self, obj): self._obj = obj def wrap_conflictual_object(obj): if hasattr(obj, 'prepare_database_save') or callable(obj): obj = _ObjectWrapper(obj) return obj def dbsafe_encode(value, compress_object=False, pickle_protocol=DEFAULT_PROTOCOL): # We use deepcopy() here to avoid a problem with cPickle, where dumps # can generate different character streams for same lookup value if # they are referenced differently. # The reason this is important is because we do all of our lookups as # simple string matches, thus the character streams must be the same # for the lookups to work properly. See tests.py for more information. value = dumps(deepcopy(value), protocol=pickle_protocol) if compress_object: value = compress(value) value = b64encode(value).decode() # decode bytes to str return PickledObject(value) def dbsafe_decode(value, compress_object=False): value = value.encode() # encode str to bytes value = b64decode(value) if compress_object: value = decompress(value) return loads(value) def _get_subfield_superclass(): # hardcore trick to support django < 1.3 - there was something wrong with # inheritance and SubfieldBase before django 1.3 # see https://github.com/django/django/commit/222c73261650201f5ce99e8dd4b1ce0d30a69eb4 if django.VERSION < (1,3): return models.Field return six.with_metaclass(models.SubfieldBase, models.Field) class PickledObjectField(_get_subfield_superclass()): """ A field that will accept *any* python object and store it in the database. PickledObjectField will optionally compress its values if declared with the keyword argument ``compress=True``. Does not actually encode and compress ``None`` objects (although you can still do lookups using None). This way, it is still possible to use the ``isnull`` lookup type correctly. """ __metaclass__ = models.SubfieldBase # for django < 1.3 def __init__(self, *args, **kwargs): self.compress = kwargs.pop('compress', False) self.protocol = kwargs.pop('protocol', DEFAULT_PROTOCOL) kwargs.setdefault('editable', False) super(PickledObjectField, self).__init__(*args, **kwargs) def get_default(self): """ Returns the default value for this field. The default implementation on models.Field calls force_unicode on the default, which means you can't set arbitrary Python objects as the default. To fix this, we just return the value without calling force_unicode on it. Note that if you set a callable as a default, the field will still call it. It will *not* try to pickle and encode it. """ if self.has_default(): if callable(self.default): return self.default() return self.default # If the field doesn't have a default, then we punt to models.Field. return super(PickledObjectField, self).get_default() def to_python(self, value): """ B64decode and unpickle the object, optionally decompressing it. If an error is raised in de-pickling and we're sure the value is a definite pickle, the error is allowed to propagate. If we aren't sure if the value is a pickle or not, then we catch the error and return the original value instead. """ if value is not None: try: value = dbsafe_decode(value, self.compress) except: # If the value is a definite pickle; and an error is raised in # de-pickling it should be allowed to propogate. if isinstance(value, PickledObject): raise else: if isinstance(value, _ObjectWrapper): return value._obj return value def pre_save(self, model_instance, add): value = super(PickledObjectField, self).pre_save(model_instance, add) return wrap_conflictual_object(value) def get_db_prep_value(self, value, connection=None, prepared=False): """ Pickle and b64encode the object, optionally compressing it. The pickling protocol is specified explicitly (by default 2), rather than as -1 or HIGHEST_PROTOCOL, because we don't want the protocol to change over time. If it did, ``exact`` and ``in`` lookups would likely fail, since pickle would now be generating a different string. """ if value is not None and not isinstance(value, PickledObject): # We call force_text here explicitly, so that the encoded string # isn't rejected by the postgresql_psycopg2 backend. Alternatively, # we could have just registered PickledObject with the psycopg # marshaller (telling it to store it like it would a string), but # since both of these methods result in the same value being stored, # doing things this way is much easier. value = force_text(dbsafe_encode(value, self.compress, self.protocol)) return value def value_to_string(self, obj): value = self._get_val_from_obj(obj) return self.get_db_prep_value(value) def get_internal_type(self): return 'TextField' def get_db_prep_lookup(self, lookup_type, value, connection=None, prepared=False): if lookup_type not in ['exact', 'in', 'isnull']: raise TypeError('Lookup type %s is not supported.' % lookup_type) # The Field model already calls get_db_prep_value before doing the # actual lookup, so all we need to do is limit the lookup types. try: return super(PickledObjectField, self).get_db_prep_lookup( lookup_type, value, connection=connection, prepared=prepared) except TypeError: # Try not to break on older versions of Django, where the # `connection` and `prepared` parameters are not available. return super(PickledObjectField, self).get_db_prep_lookup( lookup_type, value) # South support; see http://south.aeracode.org/docs/tutorial/part4.html#simple-inheritance try: from south.modelsinspector import add_introspection_rules except ImportError: pass else: add_introspection_rules([], [r"^picklefield\.fields\.PickledObjectField"]) django-picklefield-0.3.1/src/django_picklefield.egg-info/0000775000175000017500000000000012241510545024101 5ustar gintasgintas00000000000000django-picklefield-0.3.1/src/django_picklefield.egg-info/PKG-INFO0000664000175000017500000002160412241510545025201 0ustar gintasgintas00000000000000Metadata-Version: 1.1 Name: django-picklefield Version: 0.3.1 Summary: Pickled object field for Django Home-page: http://github.com/gintas/django-picklefield Author: Gintautas Miliauskas Author-email: gintautas@miliauskas.lt License: UNKNOWN Description: ----- About ----- **django-picklefield** provides an implementation of a pickled object field. Such fields can contain any picklable objects. The implementation is taken and adopted from Django snippet #1694 by Taavi Taijala, which is in turn based on Django snippet #513 by Oliver Beattie. django-picklefield is available under the MIT license. ----- Usage ----- First of all, you need to have **django-picklefield** installed; for your convenience, recent versions should be available from PyPI. To use, just define a field in your model:: >>> from picklefield.fields import PickledObjectField ... class SomeObject(models.Model): ... args = PickledObjectField() and assign whatever you like (as long as it's picklable) to the field:: >>> obj = SomeObject() >>> obj.args = ['fancy', {'objects': 'inside'}] >>> obj.save() ----- Notes ----- If you need to serialize an object with a PickledObjectField for transmission to the browser, you may need to subclass the field and override the ``value_to_string()`` method. Currently pickle fields are serialized as base64-encoded pickles, which allows reliable deserialization, but such a format is not convenient for parsing in the browser. By overriding ``value_to_string()`` you can choose a more convenient serialization format. -------------- Original notes -------------- Here are the notes by taavi223, the original author: Incredibly useful for storing just about anything in the database (provided it is Pickle-able, of course) when there isn't a 'proper' field for the job. PickledObjectField is database-agnostic, and should work with any database backend you can throw at it. You can pass in any Python object and it will automagically be converted behind the scenes. You never have to manually pickle or unpickle anything. Also works fine when querying; supports exact, in, and isnull lookups. It should be noted, however, that calling QuerySet.values() will only return the encoded data, not the original Python object. This PickledObjectField has a few improvements over the one in snippet #513. This one solves the DjangoUnicodeDecodeError problem when saving an object containing non-ASCII data by base64 encoding the pickled output stream. This ensures that all stored data is ASCII, eliminating the problem. PickledObjectField will now optionally use zlib to compress (and uncompress) pickled objects on the fly. This can be set per-field using the keyword argument "compress=True". For most items this is probably not worth the small performance penalty, but for Models with larger objects, it can be a real space saver. You can also now specify the pickle protocol per-field, using the protocol keyword argument. The default of 2 should always work, unless you are trying to access the data from outside of the Django ORM. Worked around a rare issue when using the cPickle and performing lookups of complex data types. In short, cPickle would sometimes output different streams for the same object depending on how it was referenced. This of course could cause lookups for complex objects to fail, even when a matching object exists. See the docstrings and tests for more information. You can now use the isnull lookup and have it function as expected. A consequence of this is that by default, PickledObjectField has null=True set (you can of course pass null=False if you want to change that). If null=False is set (the default for fields), then you wouldn't be able to store a Python None value, since None values aren't pickled or encoded (this in turn is what makes the isnull lookup possible). You can now pass in an object as the default argument for the field without it being converted to a unicode string first. If you pass in a callable though, the field will still call it. It will not try to pickle and encode it. You can manually import dbsafe_encode and dbsafe_decode from fields.py if you want to encode and decode objects yourself. This is mostly useful for decoding values returned from calling QuerySet.values(), which are still encoded strings. Note: If you are trying to store other django models in the PickledObjectField, please see the comments for a discussion on the problems associated with doing that. The easy solution is to put django models into a list or tuple before assigning them to the PickledObjectField. Update 9/2/09: Fixed the value_to_string method so that serialization should now work as expected. Also added deepcopy back into dbsafe_encode, fixing #4 above, since deepcopy had somehow managed to remove itself. This means that lookups should once again work as expected in all situations. Also made the field editable=False by default (which I swear I already did once before!) since it is never a good idea to have a PickledObjectField be user editable. ------- Changes ------- Changes in version 0.3.1 ======================== * Favor the built in json module (thanks to Simon Charette). Changes in version 0.3.0 ======================== * Python 3 support (thanks to Rafal Stozek). Changes in version 0.2.0 ======================== * Allow pickling of subclasses of django.db.models.Model (thanks to Simon Charette). Changes in version 0.1.9 ======================== * Added `connection` and `prepared` parameters to `get_db_prep_value()` too (thanks to Matthew Schinckel). Changes in version 0.1.8 ======================== * Updated link to code repository. Changes in version 0.1.7 ======================== * Added `connection` and `prepared` parameters to `get_db_prep_lookup()` to get rid of deprecation warnings in Django 1.2. Changes in version 0.1.6 ======================== * Fixed South support (thanks aehlke@github). Changes in version 0.1.5 ======================== * Added support for South. * Changed default to null=False, as is common throughout Django. Changes in version 0.1.4 ======================== * Updated copyright statements. Changes in version 0.1.3 ======================== * Updated serialization tests (thanks to Michael Fladischer). Changes in version 0.1.2 ======================== * Added Simplified BSD licence. Changes in version 0.1.1 ======================== * Added test for serialization. * Added note about JSON serialization for browser. * Added support for different pickle protocol versions (thanks to Michael Fladischer). Changes in version 0.1 ====================== * First public release. -------- Feedback -------- There is a home page with instructions on how to access the code repository. Send feedback and suggestions to gintautas@miliauskas.lt . Platform: UNKNOWN Classifier: Development Status :: 5 - Production/Stable Classifier: Framework :: Django Classifier: Intended Audience :: Developers Classifier: License :: OSI Approved :: MIT License Classifier: Programming Language :: Python Classifier: Programming Language :: Python :: 2.5 Classifier: Programming Language :: Python :: 2.6 Classifier: Programming Language :: Python :: 2.7 Classifier: Programming Language :: Python :: 3.2 Classifier: Programming Language :: Python :: 3.3 django-picklefield-0.3.1/src/django_picklefield.egg-info/top_level.txt0000664000175000017500000000001412241510545026626 0ustar gintasgintas00000000000000picklefield django-picklefield-0.3.1/src/django_picklefield.egg-info/dependency_links.txt0000664000175000017500000000000112241510545030147 0ustar gintasgintas00000000000000 django-picklefield-0.3.1/src/django_picklefield.egg-info/SOURCES.txt0000664000175000017500000000056712241510545025775 0ustar gintasgintas00000000000000README setup.py src/django_picklefield.egg-info/PKG-INFO src/django_picklefield.egg-info/SOURCES.txt src/django_picklefield.egg-info/dependency_links.txt src/django_picklefield.egg-info/requires.txt src/django_picklefield.egg-info/top_level.txt src/picklefield/__init__.py src/picklefield/compat.py src/picklefield/fields.py src/picklefield/models.py src/picklefield/tests.pydjango-picklefield-0.3.1/src/django_picklefield.egg-info/requires.txt0000664000175000017500000000000312241510545026472 0ustar gintasgintas00000000000000sixdjango-picklefield-0.3.1/setup.py0000664000175000017500000000415712241507530017544 0ustar gintasgintas00000000000000# Copyright (c) 2009-2010 Gintautas Miliauskas # Copyright (c) 2009, Taavi Taijala # Copyright (c) 2007, Oliver Beattie # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. import codecs from setuptools import setup, find_packages DESC=codecs.open('README', encoding='utf-8').read() setup(name='django-picklefield', version='0.3.1', description='Pickled object field for Django', long_description=DESC, author='Gintautas Miliauskas', author_email='gintautas@miliauskas.lt', url='http://github.com/gintas/django-picklefield', packages=find_packages('src'), package_dir={'' : 'src'}, install_requires=[ 'six', ], classifiers=[ 'Development Status :: 5 - Production/Stable', 'Framework :: Django', 'Intended Audience :: Developers', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python', 'Programming Language :: Python :: 2.5', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3.2', 'Programming Language :: Python :: 3.3', ] )