Skip to content

Commit fdef2bb

Browse files
committed
Added the JSON version when going through the Formidable.to_json() class method
This would ensure that stored schemas would carry their version and wouldn't need extra JSON schema migrations.
1 parent 064626d commit fdef2bb

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ ChangeLog
55
master (unreleased)
66
===================
77

8-
Nothing here yet.
8+
- Added the JSON ``version`` when going through the ``Formidable.to_json()`` class method. This would ensure that stored schemas would carry their version and wouldn't need extra JSON schema migrations (#337).
99

1010
Release 2.0.0 (2018-05-30)
1111
==========================

demo/tests/test_form_from_schema.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from django import forms
44
from freezegun import freeze_time
55

6+
from formidable import json_version
67
from formidable.constants import REQUIRED
78
from formidable.forms import (
89
FormidableForm, fields, get_dynamic_form_class_from_schema
@@ -25,6 +26,15 @@ def get_schema(self, formidable, role):
2526
'role': role
2627
}).data
2728

29+
def test_version(self):
30+
class TestCharField(FormidableForm):
31+
""" Test charfield """
32+
charfield = fields.CharField()
33+
formidable = TestCharField.to_formidable(label='label')
34+
schema = self.get_schema(formidable, 'jedi')
35+
self.assertIn('version', schema)
36+
self.assertEqual(schema['version'], json_version)
37+
2838
def test_charfield(self):
2939
class TestCharField(FormidableForm):
3040
""" Test charfield """

formidable/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from django.db import models
77
from django.utils.encoding import python_2_unicode_compatible
88

9-
from formidable import constants
9+
from formidable import constants, json_version
1010
from formidable.register import FieldSerializerRegister
1111
from jsonfield.fields import JSONField
1212

@@ -63,7 +63,9 @@ def from_json(definition_schema, **kwargs):
6363

6464
def to_json(self):
6565
from formidable.serializers import FormidableSerializer
66-
return FormidableSerializer(self).data
66+
json_data = FormidableSerializer(self).data
67+
json_data['version'] = json_version
68+
return json_data
6769

6870
def __str__(self):
6971
return '{formidable.label}'.format(formidable=self)

formidable/serializers/forms.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from django.conf import settings
99
from django.db import transaction
1010

11-
from formidable import constants
11+
from formidable import constants, json_version
1212
from formidable.forms import conditions
1313
from formidable.models import Formidable
1414
from formidable.serializers import fields
@@ -162,6 +162,11 @@ def __init__(self, *args, **kwargs):
162162
super(ContextFormSerializer, self).__init__(*args, **kwargs)
163163
self.fields['fields'].set_context('role', self._context['role'])
164164

165+
def to_representation(self, obj):
166+
data = super(ContextFormSerializer, self).to_representation(obj)
167+
data['version'] = json_version
168+
return data
169+
165170

166171
def get_access(accesses, role):
167172
"""

0 commit comments

Comments
 (0)