Note that this change will require deleting all CourseStructure data. This data should be regenerated using the generate_course_structure management command. XCOM-513
74 lines
2.2 KiB
Python
74 lines
2.2 KiB
Python
"""Models for the util app. """
|
|
import cStringIO
|
|
import gzip
|
|
import logging
|
|
|
|
from django.db import models
|
|
from django.utils.text import compress_string
|
|
|
|
from config_models.models import ConfigurationModel
|
|
|
|
logger = logging.getLogger(__name__) # pylint: disable=invalid-name
|
|
|
|
|
|
class RateLimitConfiguration(ConfigurationModel):
|
|
"""Configuration flag to enable/disable rate limiting.
|
|
|
|
Applies to Django Rest Framework views.
|
|
|
|
This is useful for disabling rate limiting for performance tests.
|
|
When enabled, it will disable rate limiting on any view decorated
|
|
with the `can_disable_rate_limit` class decorator.
|
|
"""
|
|
pass
|
|
|
|
|
|
def decompress_string(value):
|
|
"""
|
|
Helper function to reverse CompressedTextField.get_prep_value.
|
|
"""
|
|
|
|
try:
|
|
val = value.encode('utf').decode('base64')
|
|
zbuf = cStringIO.StringIO(val)
|
|
zfile = gzip.GzipFile(fileobj=zbuf)
|
|
ret = zfile.read()
|
|
zfile.close()
|
|
except Exception as e:
|
|
logger.error('String decompression failed. There may be corrupted data in the database: %s', e)
|
|
ret = value
|
|
return ret
|
|
|
|
|
|
class CompressedTextField(models.TextField):
|
|
""" TextField that transparently compresses data when saving to the database, and decompresses the data
|
|
when retrieving it from the database. """
|
|
|
|
__metaclass__ = models.SubfieldBase
|
|
|
|
def get_prep_value(self, value):
|
|
""" Compress the text data. """
|
|
if value is not None:
|
|
if isinstance(value, unicode):
|
|
value = value.encode('utf8')
|
|
value = compress_string(value)
|
|
value = value.encode('base64').decode('utf8')
|
|
return value
|
|
|
|
def to_python(self, value):
|
|
""" Decompresses the value from the database. """
|
|
if isinstance(value, unicode):
|
|
value = decompress_string(value)
|
|
|
|
return value
|
|
|
|
def south_field_triple(self):
|
|
"""Returns a suitable description of this field for South."""
|
|
# We'll just introspect the _actual_ field.
|
|
from south.modelsinspector import introspector
|
|
|
|
field_class = "django.db.models.fields.TextField"
|
|
args, kwargs = introspector(self)
|
|
# That's our definition!
|
|
return field_class, args, kwargs
|