Last active
September 6, 2022 00:32
-
-
Save atabary/9183736 to your computer and use it in GitHub Desktop.
Revisions
-
atabary revised this gist
Feb 24, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 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 is None: return None # Compress -
atabary revised this gist
Feb 24, 2014 . 1 changed file with 10 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 value value = super(CompressedBinaryField, self).to_python(value) if value in self.empty_values: return None if isinstance(value, bytes): return value # Decompress value if necessary compressed_content = StringIO.StringIO(value).getvalue() 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) -
atabary renamed this gist
Feb 24, 2014 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
atabary created this gist
Feb 24, 2014 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)