From bc3a07ddd750c740dbadaf6883e3c1d3e6a2f5aa Mon Sep 17 00:00:00 2001 From: Alana Luyten Date: Mon, 9 Dec 2024 16:38:56 +0100 Subject: [PATCH] fix(form): only recalculate on answer save if calc_dependents exist for question --- caluma/caluma_form/domain_logic.py | 46 ++++++++++++++++-------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/caluma/caluma_form/domain_logic.py b/caluma/caluma_form/domain_logic.py index ad7c9d7d4..99860e88b 100644 --- a/caluma/caluma_form/domain_logic.py +++ b/caluma/caluma_form/domain_logic.py @@ -170,16 +170,17 @@ def create( if answer.question.type == models.Question.TYPE_TABLE: answer.create_answer_documents(documents) - for question in models.Question.objects.filter( - pk__in=answer.question.calc_dependents - ): - print(f"recalculating {question} from domain logic _create_") - document = models.Document.objects.filter(pk=answer.document_id).prefetch_related( - *utils.build_document_prefetch_statements( - "family", prefetch_options=True - ), - ).first() - update_or_create_calc_answer(question, document, None) + if answer.question.calc_dependents: + for question in models.Question.objects.filter( + pk__in=answer.question.calc_dependents + ): + print(f"recalculating {question} from domain logic _create_") + document = models.Document.objects.filter(pk=answer.document_id).prefetch_related( + *utils.build_document_prefetch_statements( + "family", prefetch_options=True + ), + ).first() + update_or_create_calc_answer(question, document, None) return answer @@ -198,18 +199,19 @@ def update(cls, answer, validated_data, user: Optional[BaseUser] = None): if answer.question.type == models.Question.TYPE_TABLE: answer.create_answer_documents(documents) - root_doc = answer.document.family - root_doc = models.Document.objects.filter(pk=answer.document.family_id).prefetch_related( - *utils.build_document_prefetch_statements(prefetch_options=True) - ).first() - print("init structure top level") - struc = structure.FieldSet(root_doc, root_doc.form) - - for question in models.Question.objects.filter( - pk__in=answer.question.calc_dependents - ): - print(f"recalculating {question} from domain logic _update_") - update_or_create_calc_answer(question, root_doc, struc) + if answer.question.calc_dependents: + root_doc = answer.document.family + root_doc = models.Document.objects.filter(pk=answer.document.family_id).prefetch_related( + *utils.build_document_prefetch_statements(prefetch_options=True) + ).first() + print("init structure top level") + struc = structure.FieldSet(root_doc, root_doc.form) + + for question in models.Question.objects.filter( + pk__in=answer.question.calc_dependents + ): + print(f"recalculating {question} from domain logic _update_") + update_or_create_calc_answer(question, root_doc, struc) answer.refresh_from_db() return answer