This repository has been archived by the owner on Mar 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
dbate
committed
Jul 4, 2014
1 parent
10ba5bb
commit 812cb6c
Showing
4 changed files
with
9 additions
and
98 deletions.
There are no files selected for viewing
This file contains 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 characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,30 @@ | ||
import json | ||
|
||
import pickle | ||
from django.core.exceptions import ValidationError | ||
from django.db.models import TextField, SubfieldBase | ||
from six import with_metaclass | ||
|
||
|
||
class IterField(with_metaclass(SubfieldBase, TextField)): | ||
""" | ||
Stores the an iterable object in the the database. The data is stored as JSON and so all the values given to the | ||
field must be serializable by the "json" module. This includes dict, list, tuple, str, int, float, True, False and | ||
None | ||
Stores an iterable object in the the database. All the values given to the field must be serializable by the "pickle" package. | ||
""" | ||
|
||
def to_python(self, value): | ||
if isinstance(value, list) or isinstance(value, dict) or value is None: | ||
return value | ||
|
||
if not isinstance(value, str): | ||
if not isinstance(value, bytes): | ||
raise ValidationError("Invalid input for a IterField instance") | ||
|
||
if not value: | ||
return [] | ||
|
||
# We could store the data as a string representation of the iterable which we then "eval" but this would allow | ||
# for malicious data to be stores in the field so we need to do some sanity checking on the string. We let the | ||
# json library handle this. | ||
return json.loads(value) | ||
# pickle library handle this. | ||
return pickle.loads(value) | ||
|
||
def get_prep_value(self, value): | ||
return json.dumps(value, separators=(',', ':')) | ||
return pickle.dumps(value) | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains 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 characters
This file was deleted.
Oops, something went wrong.