-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathfix_softrequired_component_required_validation.py
66 lines (48 loc) · 2.03 KB
/
fix_softrequired_component_required_validation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python
#
# Fix the validation set on soft-required components.
#
# Due to a bug in the global configuration, the validation for soft-required components
# could be set to `required: true`. This causes the form to be in-completable, as the
# user cannot provide a value for the soft-required component.
# The problem in the global configuration has been fixed, so new soft-required component
# won't have this issue.
#
# This script automatically fixes the validation rules of all soft-required components.
# Related to ticket: https://github.com/open-formulieren/open-forms/issues/5090
from __future__ import annotations
import sys
from pathlib import Path
import django
SRC_DIR = Path(__file__).parent.parent / "src"
sys.path.insert(0, str(SRC_DIR.resolve()))
def fix_softrequired_required_validation() -> None:
from django.db.models import Q
from openforms.forms.models import FormDefinition
form_definitions_to_update = []
fds = FormDefinition.objects.iterator()
for form_definition in fds:
# Flag to update Form Definition. Is set when child component is changed
should_persist_fd = False
for component in form_definition.iter_components():
# The fix is only needed for softRequiredErrors components
if component["type"] != "softRequiredErrors":
continue
# Only fix components with the required validation set to True
if not component["validate"].get("required", False):
continue
component["validate"]["required"] = False
# Set flag to update Form Definition
should_persist_fd = True
if should_persist_fd:
form_definitions_to_update.append(form_definition)
FormDefinition.objects.bulk_update(
form_definitions_to_update, fields=["configuration"]
)
def main(**kwargs):
from openforms.setup import setup_env
setup_env()
django.setup()
return fix_softrequired_required_validation(**kwargs)
if __name__ == "__main__":
main()