Skip to content

Commit 2371a4e

Browse files
authored
Merge pull request #287 from peopledoc/allow-wrong-fields-ids
Do not raise an exception when conditional field is not visible
2 parents 56a3acd + a0ca80c commit 2371a4e

File tree

5 files changed

+87
-3
lines changed

5 files changed

+87
-3
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+
- Allow wrong field ids in conditions
99

1010
Release 1.1.0 (2017-12-04)
1111
==========================
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"conditions": [
3+
{
4+
"action": "display_iff",
5+
"fields_ids": [
6+
"third-field",
7+
"fourth-field"
8+
],
9+
"name": "Rule",
10+
"tests": [
11+
{
12+
"field_id": "test-field",
13+
"operator": "eq",
14+
"values": [
15+
true
16+
]
17+
}
18+
]
19+
}
20+
],
21+
"description": "Description",
22+
"fields": [
23+
{
24+
"defaults": [],
25+
"description": "first field",
26+
"disabled": false,
27+
"placeholder": null,
28+
"required": false,
29+
"slug": "first-field",
30+
"type_id": "help_text",
31+
"validations": []
32+
},
33+
{
34+
"defaults": [],
35+
"description": null,
36+
"disabled": false,
37+
"label": "second field",
38+
"placeholder": null,
39+
"required": false,
40+
"slug": "file",
41+
"type_id": "file",
42+
"validations": []
43+
}
44+
],
45+
"id": 1,
46+
"label": "Some label here"
47+
}

demo/tests/test_conditions.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@
22

33
from __future__ import unicode_literals
44

5+
import os
6+
import json
7+
58
from django.test import TestCase
69

10+
711
from formidable import constants
812
from formidable.forms import (
913
FormidableForm, fields, get_dynamic_form_class,
@@ -13,6 +17,8 @@
1317
ContextFormSerializer, FormidableSerializer
1418
)
1519

20+
TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
21+
1622

1723
class ConditionTestCase(TestCase):
1824

@@ -187,6 +193,32 @@ def test_readonly_field_not_displayed(self):
187193
self.assertTrue(form.is_valid(), form.errors)
188194
self.assertEqual(form.cleaned_data, {'checkbox': True})
189195

196+
def test_condition_field_doesnt_exist(self):
197+
"""
198+
This test checks situation when the conditional fields don't exists
199+
in the list of fields.
200+
it shows that we don't raise an Exception anymore.
201+
202+
You could get this situation when you specified the fields access
203+
that current user doesn't have an access to the conditional field.
204+
205+
In the fixture 'wrong-conditions.json' you see this situation.
206+
Conditions are configured for the 'test-field' but current user
207+
doesn't have rights to read or write to it. So can't see this field
208+
in the 'fields' section.
209+
"""
210+
schema = json.load(open(
211+
os.path.join(
212+
TESTS_DIR, 'fixtures', 'wrong-conditions.json'
213+
)
214+
))
215+
try:
216+
get_dynamic_form_class_from_schema(schema)
217+
except KeyError:
218+
self.fail("Doesn't have to raise an exception here ")
219+
else:
220+
self.assertTrue(True)
221+
190222

191223
class ConditionFromSchemaTestCase(ConditionTestCase):
192224

formidable/forms/conditions.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@ def gen_from_schema(self, fields, conditions_schema):
3939

4040
# cast values to field's type
4141
def convert_values(field_id, values):
42-
field = fields[field_id]
43-
return [field.to_python(value) for value in values]
42+
try:
43+
field = fields[field_id]
44+
except KeyError:
45+
return []
46+
else:
47+
return [field.to_python(value) for value in values]
4448

4549
tests = [ConditionTest(test['field_id'],
4650
test['operator'],

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ deps =
1616
django18: Django>=1.8,<1.9
1717
django18: djangorestframework<3.7
1818
django19: Django>=1.9,<1.10
19+
django19: djangorestframework<3.7
1920
django110: Django>=1.10,<1.11
2021
; Requirements from demo project
2122
-rdemo/requirements-demo.pip

0 commit comments

Comments
 (0)