Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
8e35fc67
Commit
8e35fc67
authored
Aug 18, 2015
by
Clinton Blackburn
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9328 from edx/clintonb/compressed-text-field-fix
Resolved decompression errors during tests
parents
de15f8ec
207ce4ed
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
12 additions
and
26 deletions
+12
-26
common/djangoapps/util/models.py
+12
-26
No files found.
common/djangoapps/util/models.py
View file @
8e35fc67
...
...
@@ -4,12 +4,10 @@ import gzip
import
logging
from
django.db
import
models
from
django.db.models.signals
import
post_init
from
django.utils.text
import
compress_string
from
config_models.models
import
ConfigurationModel
logger
=
logging
.
getLogger
(
__name__
)
# pylint: disable=invalid-name
...
...
@@ -25,27 +23,31 @@ class RateLimitConfiguration(ConfigurationModel):
pass
def
uncompress_string
(
s
):
def
decompress_string
(
value
):
"""
Helper function to reverse CompressedTextField.get_prep_value.
"""
try
:
val
=
s
.
encode
(
'utf'
)
.
decode
(
'base64'
)
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
=
s
ret
=
value
return
ret
class
CompressedTextField
(
models
.
TextField
):
"""transparently compress data before hitting the db and uncompress after fetching"""
""" 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'
)
...
...
@@ -53,28 +55,12 @@ class CompressedTextField(models.TextField):
value
=
value
.
encode
(
'base64'
)
.
decode
(
'utf8'
)
return
value
def
post_init
(
self
,
instance
=
None
,
**
kwargs
):
# pylint: disable=unused-argument
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
)
post_init
.
connect
(
self
.
post_init
,
sender
=
cls
)
def
to_python
(
self
,
value
):
""" Decompresses the value from the database. """
if
isinstance
(
value
,
unicode
):
value
=
decompress_string
(
value
)
def
_get_val_from_obj
(
self
,
obj
):
if
obj
:
value
=
uncompress_string
(
getattr
(
obj
,
self
.
attname
))
if
value
is
not
None
:
try
:
value
=
value
.
decode
(
'utf8'
)
except
UnicodeDecodeError
:
pass
return
value
else
:
return
self
.
get_default
()
else
:
return
self
.
get_default
()
def
south_field_triple
(
self
):
"""Returns a suitable description of this field for South."""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment