Skip to content

Commit 09c0073

Browse files
authored
Merge pull request #290 from peopledoc/intersect-conditional-rules
Filter conditional rules
2 parents 6a95fcb + e08a985 commit 09c0073

File tree

4 files changed

+63
-12
lines changed

4 files changed

+63
-12
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+
- Keep only existing fields ids for current role in the conditional part
99

1010
Release 1.2.0 (2018-01-09)
1111
==========================

demo/tests/fixtures/wrong-conditions.json

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"action": "display_iff",
55
"fields_ids": [
66
"third-field",
7-
"fourth-field"
7+
"fourth-field",
8+
"first-field",
9+
"second-field"
810
],
911
"name": "Rule",
1012
"tests": [
@@ -28,7 +30,17 @@
2830
"required": false,
2931
"slug": "first-field",
3032
"type_id": "help_text",
31-
"validations": []
33+
"validations": [],
34+
"accesses": [
35+
{
36+
"access_id": "TEST_ROLE",
37+
"level": "EDITABLE"
38+
},
39+
{
40+
"access_id": "TEST_ROLE2",
41+
"level": "EDITABLE"
42+
}
43+
]
3244
},
3345
{
3446
"defaults": [],
@@ -37,9 +49,18 @@
3749
"label": "second field",
3850
"placeholder": null,
3951
"required": false,
40-
"slug": "file",
52+
"slug": "second-field",
4153
"type_id": "file",
42-
"validations": []
54+
"validations": [],
55+
"accesses": [
56+
{
57+
"access_id": "TEST_ROLE2",
58+
"level": "EDITABLE"
59+
}, {
60+
"access_id": "TEST_ROLE",
61+
"level": "HIDDEN"
62+
}
63+
]
4364
}
4465
],
4566
"id": 1,

demo/tests/test_conditions.py

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,13 @@
99

1010

1111
from formidable import constants
12+
1213
from formidable.forms import (
1314
FormidableForm, fields, get_dynamic_form_class,
1415
get_dynamic_form_class_from_schema
1516
)
1617
from formidable.serializers.forms import (
17-
ContextFormSerializer, FormidableSerializer
18+
ContextFormSerializer, FormidableSerializer, contextualize
1819
)
1920

2021
TESTS_DIR = os.path.dirname(os.path.abspath(__file__))
@@ -25,6 +26,14 @@ class ConditionTestCase(TestCase):
2526
def get_form_class(self, formidable, role):
2627
return get_dynamic_form_class(formidable, role)
2728

29+
@classmethod
30+
def setUpTestData(cls):
31+
cls.conditions_schema = json.load(open(
32+
os.path.join(
33+
TESTS_DIR, 'fixtures', 'wrong-conditions.json'
34+
)
35+
))
36+
2837
def setUp(self):
2938
conditions_schema = [
3039
{
@@ -207,18 +216,31 @@ def test_condition_field_doesnt_exist(self):
207216
doesn't have rights to read or write to it. So can't see this field
208217
in the 'fields' section.
209218
"""
210-
schema = json.load(open(
211-
os.path.join(
212-
TESTS_DIR, 'fixtures', 'wrong-conditions.json'
213-
)
214-
))
219+
215220
try:
216-
get_dynamic_form_class_from_schema(schema)
221+
get_dynamic_form_class_from_schema(self.conditions_schema)
217222
except KeyError:
218223
self.fail("Doesn't have to raise an exception here ")
219224
else:
220225
self.assertTrue(True)
221226

227+
def test_filter_conditions_by_fields_ids(self):
228+
first_schema = contextualize(self.conditions_schema, 'TEST_ROLE')
229+
conditions = first_schema['conditions']
230+
self.assertEqual(len(conditions), 1)
231+
condition = conditions[0]
232+
233+
self.assertEqual(condition['fields_ids'], ['first-field'])
234+
235+
second_schema = contextualize(self.conditions_schema, 'TEST_ROLE2')
236+
conditions = second_schema['conditions']
237+
self.assertEqual(len(conditions), 1)
238+
condition = conditions[0]
239+
self.assertEqual(
240+
sorted(condition['fields_ids']),
241+
['first-field', 'second-field']
242+
)
243+
222244

223245
class ConditionFromSchemaTestCase(ConditionTestCase):
224246

formidable/serializers/forms.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,4 +181,12 @@ def contextualize(form, role):
181181
"""
182182
form = copy.deepcopy(form)
183183
form['fields'] = list(contextualize_fields(form['fields'], role))
184+
185+
# filter conditions by fields ids for current role
186+
field_ids = {field['slug'] for field in form['fields']}
187+
for condition in form.get('conditions', []):
188+
condition['fields_ids'] = list(
189+
set(condition.get('fields_ids', [])) & field_ids
190+
)
191+
184192
return form

0 commit comments

Comments
 (0)