Login

CompressedTextField

Author:
arne
Posted:
August 24, 2007
Language:
Python
Version:
.96
Score:
2 (after 2 ratings)

A CompressedTextField to transparently save data gzipped in the database and uncompress at retrieval. Full description at my blog: arnebrodowski.de/...Field-for-Django.html

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from django.db import models
from django.utils.text import compress_string
from django.db.models import signals
from django.dispatch import dispatcher

def uncompress_string(s):
    '''helper function to reverse django.utils.text.compress_string'''
    import cStringIO, gzip
    try:
        zbuf = cStringIO.StringIO(s)
        zfile = gzip.GzipFile(fileobj=zbuf)
        ret = zfile.read()
        zfile.close()
    except:
        ret = s
    return ret

class CompressedTextField(models.TextField):
    '''transparently compress data before hitting the db and uncompress after fetching'''

    def get_db_prep_save(self, value):
        if value is not None:
            value = compress_string(value) 
        return models.TextField.get_db_prep_save(self, value)
 
    def _get_val_from_obj(self, obj):
        if obj:
            return uncompress_string(getattr(obj, self.attname))
        else:
            return self.get_default() 
    
    def post_init(self, instance=None):
        value = self._get_val_from_obj(instance)
        if value:
            setattr(instance, self.attname, value)

    def contribute_to_class(self, cls, name):
        super(CompressedTextField, self).contribute_to_class(cls, name)
        dispatcher.connect(self.post_init, signal=signals.post_init, sender=cls)
    
    def get_internal_type(self):
        return "TextField"
                
    def db_type(self):
        from django.conf import settings
        if settings.DATABASE_ENGINE == 'mysql':
            return 'longblob'
        else:
            raise Exception, '%s currently works only with MySQL'%self.__class__.__name__


class Item(models.Model):
    title = models.CharField(maxlength=200)
    summary = CompressedTextField(blank=True)

More like this

  1. Template tag - list punctuation for a list of items by shapiromatron 10 months, 1 week ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 10 months, 2 weeks ago
  3. Serializer factory with Django Rest Framework by julio 1 year, 5 months ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 1 year, 6 months ago
  5. Help text hyperlinks by sa2812 1 year, 6 months ago

Comments

Please login first before commenting.