-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3528 from open-formulieren/issue/3527-unique-cons…
…traint Add unique constraint on form + form definition
- Loading branch information
Showing
5 changed files
with
106 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
20 changes: 20 additions & 0 deletions
20
src/openforms/forms/migrations/0095_formstep_form_form_definition_unique_together.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Generated by Django 3.2.21 on 2023-10-09 10:23 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("forms", "0094_update_config_family"), | ||
] | ||
|
||
operations = [ | ||
migrations.AddConstraint( | ||
model_name="formstep", | ||
constraint=models.UniqueConstraint( | ||
fields=("form", "form_definition"), | ||
name="form_form_definition_unique_together", | ||
), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters