Skip to content

Instantly share code, notes, and snippets.

@atabary
Last active September 6, 2022 00:32
Show Gist options
  • Select an option

  • Save atabary/9183736 to your computer and use it in GitHub Desktop.

Select an option

Save atabary/9183736 to your computer and use it in GitHub Desktop.

Revisions

  1. atabary revised this gist Feb 24, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions fields.py
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@ class CompressedBinaryField(with_metaclass(models.SubfieldBase, models.BinaryFie
    def to_python(self, value):
    # Check value
    value = super(CompressedBinaryField, self).to_python(value)
    if value in self.empty_values:
    if value is None:
    return None
    if isinstance(value, bytes):
    return value
    @@ -23,7 +23,7 @@ def to_python(self, value):

    def get_prep_value(self, value):
    # Check for empty values
    if value in self.empty_values:
    if value is None:
    return None

    # Compress
  2. atabary revised this gist Feb 24, 2014. 1 changed file with 10 additions and 4 deletions.
    14 changes: 10 additions & 4 deletions fields.py
    Original file line number Diff line number Diff line change
    @@ -10,15 +10,21 @@ class CompressedBinaryField(with_metaclass(models.SubfieldBase, models.BinaryFie
    description = "A binary field with bzip2 compression"

    def to_python(self, value):
    # Check if the value is already deserialized
    # Check value
    value = super(CompressedBinaryField, self).to_python(value)
    if value in self.empty_values:
    return None
    if isinstance(value, bytes):
    return value

    # Read everything from the buffer
    # Decompress value if necessary
    compressed_content = StringIO.StringIO(value).getvalue()

    # Decompress
    return bz2.decompress(compressed_content)

    def get_prep_value(self, value):
    # Check for empty values
    if value in self.empty_values:
    return None

    # Compress
    return bz2.compress(value)
  3. atabary renamed this gist Feb 24, 2014. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. atabary created this gist Feb 24, 2014.
    24 changes: 24 additions & 0 deletions gistfile1.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    import bz2
    import StringIO

    from django.db import models
    from django.utils.six import with_metaclass


    class CompressedBinaryField(with_metaclass(models.SubfieldBase, models.BinaryField)):

    description = "A binary field with bzip2 compression"

    def to_python(self, value):
    # Check if the value is already deserialized
    if isinstance(value, bytes):
    return value

    # Read everything from the buffer
    compressed_content = StringIO.StringIO(value).getvalue()

    # Decompress
    return bz2.decompress(compressed_content)

    def get_prep_value(self, value):
    return bz2.compress(value)