CompressedTextField

 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
class CompressedTextField(models.TextField):
    """
    model Fields for storing text in a compressed format (bz2 by default)
    """
    __metaclass__ = models.SubfieldBase

    def to_python(self, value):
        if not value:
            return value

        try:
            return value.decode('base64').decode('bz2').decode('utf-8')
        except Exception:
            return value

    def get_prep_value(self, value):
        if not value:
            return value

        try:
            value.decode('base64')
            return value
        except Exception:
            try:
                tmp = value.encode('utf-8').encode('bz2').encode('base64')
            except Exception:
                return value
            else:
                if len(tmp) > len(value):
                    return value

                return tmp

More like this

  1. CompressedTextField by arne 4 years, 9 months ago
  2. Simple Plone Migration by msm-art 4 years, 4 months ago
  3. Database migration and dump/load script by akaihola 5 years, 1 month ago
  4. Dynamic Models Revisited by Ben 4 years, 7 months ago
  5. Log all interaction with user to the DB by inuwashi 1 year, 4 months ago

Comments

(Forgotten your password?)