From 75503f7597e52f55e14b484de6392a7f777c8f9c Mon Sep 17 00:00:00 2001 From: vasileios Date: Fri, 17 May 2024 16:29:22 +0200 Subject: [PATCH] [#4262] Added script for reporting invalid default values in radio component --- bin/report_radio_invalid_default_values.py | 62 ++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 bin/report_radio_invalid_default_values.py diff --git a/bin/report_radio_invalid_default_values.py b/bin/report_radio_invalid_default_values.py new file mode 100644 index 0000000000..b3817682c5 --- /dev/null +++ b/bin/report_radio_invalid_default_values.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python +import django + +from tabulate import tabulate + + +def report_null_values() -> bool: + from openforms.forms.models import FormDefinition + + problems = [] + + form_definitions = FormDefinition.objects.iterator() + for fd in form_definitions: + for component in fd.iter_components(): + default_value = component["defaultValue"] + + if ( + component["type"] == "radio" + and isinstance(default_value, str) + and bool(default_value) + ): + expected_values = [ + radio_value["value"] for radio_value in component["values"] + ] + + if default_value not in expected_values: + problems.append( + [ + fd, + component["label"], + default_value, + ] + ) + + if not problems: + print("No invalid default values found.") + return True + + print("Found invalid default values in form definition radio components.") + print("") + print( + tabulate( + problems, + headers=("Form definition", "Component label", "Invalid value"), + ) + ) + + return False + + +def main(skip_setup=False) -> bool: + from openforms.setup import setup_env + + if not skip_setup: + setup_env() + django.setup() + + return report_null_values() + + +if __name__ == "__main__": + main()