Skip to content

Commit

Permalink
fix(form): only recalculate dependent questions on document creation
Browse files Browse the repository at this point in the history
  • Loading branch information
luytena committed Dec 10, 2024
1 parent bc3a07d commit 89c34b1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
45 changes: 34 additions & 11 deletions caluma/caluma_form/domain_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from caluma.caluma_form.utils import recalculate_answers_from_document, update_or_create_calc_answer
from caluma.caluma_user.models import BaseUser
from caluma.utils import update_model

import itertools



Expand Down Expand Up @@ -170,17 +170,21 @@ def create(
if answer.question.type == models.Question.TYPE_TABLE:
answer.create_answer_documents(documents)

print("creating answer", flush=True)
if answer.question.calc_dependents:
print("creating answer for question", answer.question, 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 _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)
update_or_create_calc_answer(question, root_doc, struc)

return answer

Expand Down Expand Up @@ -306,10 +310,29 @@ def create(

# TODO do we need really this? If yes, can we make it more efficient?
print("domain logic: update calc answers after document has been created")
for question in models.Form.get_all_questions(
[(document.family or document).form_id]
).filter(type=models.Question.TYPE_CALCULATED_FLOAT):
update_or_create_calc_answer(question, document, None)
#for question in models.Form.get_all_questions(
# [(document.family or document).form_id]
#).filter(type=models.Question.TYPE_CALCULATED_FLOAT):
# update_or_create_calc_answer(question, document, None)

root_doc = document.family
root_doc = models.Document.objects.filter(pk=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)

dependents = document.form.questions.exclude(
calc_dependents=[]
).values_list("calc_dependents", flat=True)

dependent_questions = list(itertools.chain(*dependents))
print(f"document {document.form.pk} created, update {dependent_questions}")

for question in models.Question.objects.filter(pk__in=dependent_questions):
print("question", question)
update_or_create_calc_answer(question, document, struc)

return document

@staticmethod
Expand Down
7 changes: 5 additions & 2 deletions caluma/caluma_form/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.db.models import Prefetch
from caluma.caluma_form.jexl import QuestionJexl
from time import time

import inspect

def build_document_prefetch_statements(prefix="", prefetch_options=False):
"""Build needed prefetch statements to performantly fetch a document.
Expand Down Expand Up @@ -102,6 +102,8 @@ def update_calc_dependents(slug, old_expr, new_expr):


def update_or_create_calc_answer(question, document, struc):
print("callsite", inspect.stack()[1][3], flush=True)

root_doc = document.family

if not struc:
Expand All @@ -115,9 +117,10 @@ def update_or_create_calc_answer(question, document, struc):

# skip if question doesn't exist in this document structure
if field is None:
print("-- didn't find question, stopping")
print("-- didn't find question, stopping", question)
return

print("-- found field", question)
jexl = QuestionJexl(
{"form": field.form, "document": field.document, "structure": field.parent()}
)
Expand Down

0 comments on commit 89c34b1

Please sign in to comment.