Skip to content

Commit

Permalink
fix: apply test feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
czosel committed Jan 9, 2025
1 parent 3d72775 commit dc0e4eb
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 20 deletions.
9 changes: 1 addition & 8 deletions caluma/caluma_form/domain_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,14 +306,7 @@ def _initialize_calculated_answers(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)
.prefetch_related(
*utils.build_document_prefetch_statements(prefetch_options=True)
)
.first()
)
root_doc = utils.prefetch_document(document.family_id)
struc = structure.FieldSet(root_doc, root_doc.form)

calculated_questions = (
Expand Down
4 changes: 1 addition & 3 deletions caluma/caluma_form/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,8 @@ def remove_calc_dependents(sender, instance, **kwargs):
@receiver(post_save, sender=models.Question)
@disable_raw
@filter_events(lambda instance: instance.type == models.Question.TYPE_CALCULATED_FLOAT)
@filter_events(lambda instance: getattr(instance, "calc_expression_changed"))
def update_calc_from_question(sender, instance, created, update_fields, **kwargs):
if not instance.calc_expression_changed:
return

for document in models.Document.objects.filter(form__questions=instance):
update_or_create_calc_answer(instance, document)

Expand Down
18 changes: 9 additions & 9 deletions caluma/caluma_form/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@


def prefetch_document(document_id):
return (
models.Document.objects.filter(pk=document_id)
.prefetch_related(*build_document_prefetch_statements(prefetch_options=True))
.first()
)


def build_document_prefetch_statements(prefix="", prefetch_options=False):
"""Build needed prefetch statements to performantly fetch a document.
"""Fetch a document while prefetching the entire structure.
This is needed to reduce the query count when almost all the form data
is needed for a given document, e.g. when recalculating calculated
answers: in order to evaluate calc expressions the complete document
structure is needed, which would otherwise result in a lot of queries.
"""
return (
models.Document.objects.filter(pk=document_id)
.prefetch_related(*_build_document_prefetch_statements(prefetch_options=True))
.first()
)


def _build_document_prefetch_statements(prefix="", prefetch_options=False):
question_queryset = models.Question.objects.select_related(
"sub_form", "row_form"
).order_by("-formquestion__sort")
Expand Down

0 comments on commit dc0e4eb

Please sign in to comment.