Skip to content

Commit ff1fddc

Browse files
authored
Merge pull request #320 from peopledoc/change-field-type-support
Change field type support
2 parents 264e7a6 + 7d650f4 commit ff1fddc

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

CHANGELOG.rst

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ Release 1.5.2 (2018-03-30)
1111
==========================
1212

1313
- Allow null and empty condition names.
14+
- Allow change type for the fields without changing name/slug
1415
- Added compatibility tests using Django 1.11.
1516

1617
Release 1.5.1 (2018-03-28)

demo/tests/test_post_save_callbacks_regression_tests.py

+31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
from __future__ import unicode_literals
33

4+
import json
5+
46
from copy import deepcopy
57

68
from django.core.urlresolvers import reverse
@@ -102,3 +104,32 @@ def test_update_no_settings_error(self):
102104
form_data_without_items, format='json'
103105
)
104106
self.assertEquals(res.status_code, 422)
107+
108+
109+
class UpdateFormWithTheSameFieldSlug(APITestCase):
110+
@override_settings(FORMIDABLE_POST_CREATE_CALLBACK_SUCCESS=None)
111+
def test_update_no_error(self):
112+
# Create a form with the usual data.
113+
res_create = self.client.post(
114+
reverse('formidable:form_create'), form_data, format='json'
115+
)
116+
# Do not use .json() for django 1.8
117+
data = json.loads(res_create.content.decode('utf-8'))
118+
form_id = data.get('id')
119+
data_to_update = deepcopy(form_data_items)
120+
# The field [0] was a text field
121+
# Now it becomes a dropdown
122+
data_to_update['fields'][0]['slug'] = form_data['fields'][0]['slug']
123+
res_update = self.client.put(
124+
reverse('formidable:form_detail', args=[form_id]),
125+
data_to_update, format='json'
126+
)
127+
self.assertEquals(res_update.status_code, 200)
128+
form = Formidable.objects.get(pk=form_id)
129+
json_data = form.to_json()
130+
# Still one field
131+
self.assertEqual(len(json_data['fields']), 1)
132+
self.assertEqual(
133+
json_data['fields'][0]['slug'], form_data['fields'][0]['slug']
134+
)
135+
self.assertEqual(json_data['fields'][0]['type_id'], 'dropdown')

formidable/serializers/child_proxy.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@ def call_right_serializer_by_instance(meth):
99

1010
def _wrapper(self, instance, *args, **kwargs):
1111

12-
type_id = getattr(instance, self.lookup_field)
12+
# Check if there is a fields data in the arguments
13+
# if exists take type of the new data instead of the
14+
# instance type.
15+
if args:
16+
field = args[0]
17+
type_id = field[self.lookup_field]
18+
else:
19+
type_id = getattr(instance, self.lookup_field)
20+
1321
serializer = self.get_right_serializer(type_id)
1422
meth_name = getattr(serializer, meth.__name__)
1523
return meth_name(instance, *args, **kwargs)

0 commit comments

Comments
 (0)