Skip to content

Commit

Permalink
fix: changes from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Jan 9, 2025
1 parent af7e014 commit 5c07195
Show file tree
Hide file tree
Showing 5 changed files with 179 additions and 17 deletions.
24 changes: 13 additions & 11 deletions caluma/caluma_form/domain_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,7 @@ def update_calc_dependents(answer):
if not answer.question.calc_dependents:
return

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()
)
root_doc = utils.prefetch_document(answer.document.family_id)
struc = structure.FieldSet(root_doc, root_doc.form)

for question in models.Question.objects.filter(
Expand Down Expand Up @@ -301,6 +294,18 @@ def create(
document.meta.pop("_defer_calculation", None)
document.save()

SaveDocumentLogic._initialize_calculated_answers(document)

return document

@staticmethod
def _initialize_calculated_answers(document):
"""
Initialize all calculated questions in the document.
In order to do this efficiently, we get all calculated questions with their dependents,
sort them topoligically, and then update their answer.
"""
root_doc = document.family
root_doc = (
models.Document.objects.filter(pk=document.family_id)
Expand All @@ -311,9 +316,6 @@ def create(
)
struc = structure.FieldSet(root_doc, root_doc.form)

# Initialize all calculated questions in the form.
# In order to do this efficiently, we get all calculated questions with their dependents,
# sort them topoligically, and then update their answer.
calculated_questions = (
models.Form.get_all_questions([(document.family or document).form_id])
.filter(type=models.Question.TYPE_CALCULATED_FLOAT)
Expand Down
Loading

0 comments on commit 5c07195

Please sign in to comment.