def test_JsonField(self): field = forms.JsonField() value = {'test': '????'} t = type(value) json_val = json.dumps(value) result = field.clean(json_val) self.assertEqual(type(result), t)
class ApplicationForm(forms.Form): id = forms.CharField(readonly=True, required=False) name = forms.SlugField(minlength=2, maxlength=32, validator=UniqueField) domain = forms.CharField(minlength=2, maxlength=120, required=False, validator=UniqueField) config = forms.JsonField(required=False, ace=json.dumps({'mode': 'json'}))
class PermissionForm(forms.Form): model = 'permission' id = forms.HiddenField(required=False) name = forms.CharField() description = forms.TextField() policy = forms.JsonField(text_edit=json.dumps({'mode': 'json'})) def clean(self): policy = self.cleaned_data['policy'] self.cleaned_data['policy'] = validate_policy(policy)
class PermissionForm(forms.Form): model = 'permissions' name = forms.CharField() description = forms.TextField(required=False, rows=2) policy = forms.JsonField(lux_ace=json.dumps({'mode': 'json'})) def clean(self): if 'policy' in self.cleaned_data: policy = self.cleaned_data['policy'] self.cleaned_data['policy'] = validate_policy(policy)
class PageForm(forms.Form): path = forms.CharField(required=False) title = forms.CharField() description = forms.TextField(required=False) template = odm.RelationshipField(template_model, label='template') published = forms.BooleanField(required=False) layout = forms.JsonField(text_edit=json.dumps({'mode': 'json'})) def clean_layout(self, value): if not isinstance(value, dict): raise forms.ValidationError('Layout must be a dictionary') layout = {} if 'components' in value: components = value['components'] if not isinstance(components, list): raise forms.ValidationError('componets must be a list') layout['components'] = components if 'rows' in value: rows = value['rows'] if not isinstance(rows, list): raise forms.ValidationError('rows must be a list') layout['rows'] = rows return layout
def test_JsonField_raises(self): field = forms.JsonField() value = 'string' with self.assertRaises(forms.ValidationError): field._clean(value, None)
def test_JsonField_fail(self): field = forms.JsonField() self.assertRaises(forms.ValidationError, field.clean, self)
def test_JsonField_object(self): field = forms.JsonField() value = {'bla': 'foo', 'zzz': [1, 2, 3]} self.assertEqual(field.clean(value), value)
def test_JsonField_string(self): field = forms.JsonField() self.assertRaises(forms.ValidationError, field.clean, 'string')