From 43783b00a16b8f7bdd5108ae662693b6e6f3b6dc Mon Sep 17 00:00:00 2001 From: j Date: Fri, 19 Feb 2016 19:00:53 +0530 Subject: [PATCH] rewrite ox.django.fields --- ox/django/fields.py | 72 ++++++++++++++++++++------------------------- 1 file changed, 32 insertions(+), 40 deletions(-) diff --git a/ox/django/fields.py b/ox/django/fields.py index 2ff69d8..83e11f0 100644 --- a/ox/django/fields.py +++ b/ox/django/fields.py @@ -49,57 +49,49 @@ class DictField(models.TextField): # Used so to_python() is called __metaclass__ = models.SubfieldBase + _type = dict + + def loads(self, value): + return json.loads(value, object_hook=from_json) + + def dumps(self, obj): + return json.dumps(obj, default=to_json) def to_python(self, value): - """Convert our string value to python after we load it from the DB""" - if value == None: + # django 1.7 + if value is None: return value - if isinstance(value, dict): + if isinstance(value, self._type): return value try: - value = json.loads(value, object_hook=from_json) - except: #this is required to load fixtures - value = eval(value) - assert isinstance(value, dict) + value = self.loads(value) + except: + raise Exception('failed to parse value: %s' % value) + if value is not None: + assert isinstance(value, self._type) return value - def get_db_prep_save(self, value, connection): - """Convert our JSON object to a string before we save""" - if value == None: - return value - if isinstance(value, string_types): - value = eval(value) - assert isinstance(value, dict) - value = json.dumps(value, default=to_json) - return super(DictField, self).get_db_prep_save(value, connection=connection) + def from_db_value(self, value, expression, connection, context): + # django 1.8 + return self.to_python(value) -class TupleField(models.TextField): - """TupleField is a textfield that contains JSON-serialized tuples.""" + def get_prep_value(self, value): + if isinstance(value, self._type): + value = self.dumps(value) + if value is not None: + assert isinstance(value, string_types) + value = models.TextField.get_prep_value(self, value) + return value - # Used so to_python() is called - __metaclass__ = models.SubfieldBase - def to_python(self, value): - """Convert our string value to JSON after we load it from the DB""" - if isinstance(value, tuple): - return value +class TupleField(DictField): + _type = tuple - try: - value = json.loads(value, object_hook=from_json) - except: #this is required to load fixtures - value = eval(value) - assert isinstance(value, list) - return tuple(value) - - def get_db_prep_save(self, value, connection): - """Convert our JSON object to a string before we save""" - if isinstance(value, string_types): - value = eval(value) - if isinstance(value, list): - value = tuple(value) - assert isinstance(value, tuple) - value = json.dumps(value, default=to_json) - return super(TupleField, self).get_db_prep_save(value, connection=connection) + def loads(self, value): + value = DictField.loads(self, value) + if isinstance(value, list): + value = tuple(value) + return value try: from south.modelsinspector import add_introspection_rules