Add Django 1.10 compatibility
This commit is contained in:
@@ -13,7 +13,7 @@ from .forms import (
|
||||
JSIGNATURE_EMPTY_VALUES)
|
||||
|
||||
|
||||
class JSignatureField(six.with_metaclass(models.SubfieldBase, models.Field)):
|
||||
class JSignatureField(models.Field):
|
||||
"""
|
||||
A model field handling a signature captured with jSignature
|
||||
"""
|
||||
@@ -23,10 +23,6 @@ class JSignatureField(six.with_metaclass(models.SubfieldBase, models.Field)):
|
||||
return 'TextField'
|
||||
|
||||
def to_python(self, value):
|
||||
"""
|
||||
Validates that the input can be red as a JSON object. Returns a Python
|
||||
datetime.date object.
|
||||
"""
|
||||
if value in JSIGNATURE_EMPTY_VALUES:
|
||||
return None
|
||||
elif isinstance(value, list):
|
||||
@@ -36,6 +32,14 @@ class JSignatureField(six.with_metaclass(models.SubfieldBase, models.Field)):
|
||||
except ValueError:
|
||||
raise ValidationError('Invalid JSON format.')
|
||||
|
||||
def from_db_value(self, value, expression, connection, context):
|
||||
if value in JSIGNATURE_EMPTY_VALUES:
|
||||
return None
|
||||
try:
|
||||
return json.loads(value)
|
||||
except ValueError:
|
||||
raise ValidationError('Invalid JSON format.')
|
||||
|
||||
def get_prep_value(self, value):
|
||||
if value in JSIGNATURE_EMPTY_VALUES:
|
||||
return None
|
||||
@@ -50,8 +54,3 @@ class JSignatureField(six.with_metaclass(models.SubfieldBase, models.Field)):
|
||||
defaults.update(kwargs)
|
||||
return super(JSignatureField, self).formfield(**defaults)
|
||||
|
||||
try:
|
||||
from south.modelsinspector import add_introspection_rules
|
||||
add_introspection_rules([], ["jsignature.fields.JSignatureField"])
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
@@ -31,6 +31,21 @@ class JSignatureFieldTest(SimpleTestCase):
|
||||
val = 'foo'
|
||||
self.assertRaises(ValidationError, f.to_python, val)
|
||||
|
||||
def test_from_db_value_empty(self):
|
||||
f = JSignatureField()
|
||||
self.assertIsNone(f.from_db_value(''))
|
||||
|
||||
def test_from_db_value_correct_value_json(self):
|
||||
f = JSignatureField()
|
||||
val = [{"x": [1, 2], "y": [3, 4]}]
|
||||
val_str = '[{"x":[1,2], "y":[3,4]}]'
|
||||
self.assertEquals(val, f.from_db_value(val_str))
|
||||
|
||||
def test_from_db_value_incorrect_value(self):
|
||||
f = JSignatureField()
|
||||
val = 'foo'
|
||||
self.assertRaises(ValidationError, f.to_python, val)
|
||||
|
||||
def test_get_prep_value_empty(self):
|
||||
f = JSignatureField()
|
||||
for val in ['', [], '[]']:
|
||||
|
||||
Reference in New Issue
Block a user