Skip to content

Commit

Permalink
Merge pull request #3529 from open-formulieren/backport/3527-check-du…
Browse files Browse the repository at this point in the history
…plicated-steps

Backport: duplicated form steps check
  • Loading branch information
sergei-maertens authored Oct 10, 2023
2 parents efc1220 + 966090d commit 8bc301c
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
72 changes: 72 additions & 0 deletions bin/check_non_unique_steps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python
import sys
from pathlib import Path

import django

from tabulate import tabulate

SRC_DIR = Path(__file__).parent.parent / "src"
sys.path.insert(0, str(SRC_DIR.resolve()))


def check_non_unique_steps() -> bool:
from django.db.models import Count

from openforms.forms.models import Form, FormDefinition, FormStep

# query for form definitions used multiple times in a form
qs = (
FormStep.objects.values("form", "form_definition")
.annotate(occurrences=Count("form_definition"))
.filter(occurrences__gt=1)
.order_by() # reset ordering (implicitly added by django-ordered-model)
)
num = qs.count()
if not num:
print("No forms found with duplicated form steps.")
return True

# report which forms have issues
forms = Form.objects.filter(pk__in=qs.values_list("form")).in_bulk()
form_definitions = FormDefinition.objects.filter(
pk__in=qs.values_list("form_definition")
).in_bulk()

duplicates = []
for item in qs:
form = forms[item["form"]]
form_definition = form_definitions[item["form_definition"]]

duplicates.append(
[
form.admin_name,
form_definition.admin_name,
item["occurrences"],
]
)

print("Found forms with duplicated steps.")
print("")
print(
tabulate(
duplicates,
headers=("Form", "Form step", "Occurrences"),
)
)

return False


def main(skip_setup=False) -> bool:
from openforms.setup import setup_env

if not skip_setup:
setup_env()
django.setup()

return check_non_unique_steps()


if __name__ == "__main__":
main()
6 changes: 5 additions & 1 deletion src/openforms/upgrades/upgrade_paths.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,11 @@ def run_checks(self) -> bool:
"2.3": UpgradeConstraint(
valid_ranges={
VersionRange(minimum="2.1.3"),
}
},
scripts=(
# run detection to prevent crashes, see #3527
"check_non_unique_steps",
),
),
"2.2": UpgradeConstraint(
valid_ranges={
Expand Down

0 comments on commit 8bc301c

Please sign in to comment.