Skip to content

Commit

Permalink
feat: also update table summaries on table question change
Browse files Browse the repository at this point in the history
This makes sure a newly applied summary config leads to the generation
of all the summaries. This has the potential of timing out, if there is
a massive amount of Answers. We expect them to be in the lower 100s, so
this doesn't justify an async approach.
  • Loading branch information
open-dynaMIX committed Nov 21, 2024
1 parent 06fbc33 commit bf49b0a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 6 deletions.
16 changes: 16 additions & 0 deletions caluma/extensions/events/form.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,22 @@ def update_table_summary_from_row(instance, *args, **kwargs):
update_table_summary(instance=ad)


@on(post_save, sender=caluma_form_models.Question, raise_exception=True)
@transaction.atomic
def update_table_summary_from_table_question(instance, *args, **kwargs):
if instance.type != "table" or "summary-question" not in instance.meta:
return

# Call `update_table_summary()` for one AnswerDocument per every existing Answer
updated_answers = []
for ad in caluma_form_models.AnswerDocument.objects.filter(
answer__question_id=instance.slug
):
if ad.answer not in updated_answers:
updated_answers.append(ad.answer)
update_table_summary(instance=ad)


def _make_csv_summary(table_answer):
def get_lines(answer_docs, row_question_slugs):
for ad in answer_docs:
Expand Down
31 changes: 25 additions & 6 deletions caluma/extensions/tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,7 @@ def add_table_with_summary(
form_question_factory(form=row_form, question=row_question_3)

question_factory(type=Question.TYPE_TEXTAREA, slug="summary", is_hidden="true")
question_factory(type=Question.TYPE_TEXTAREA, slug="summary2", is_hidden="true")
return main_doc, main_form, table_question, row_form, row_question_1, row_question_3


Expand Down Expand Up @@ -685,6 +686,13 @@ def test_table_summary(
== "row1;row2;row3\r\n23.5;;bar\r\n23.5;;\r\n"
)

table_question.meta = {"summary-question": "summary2", "summary-mode": "csv"}
table_question.save()
assert (
main_doc.answers.get(question_id="summary2").value
== "row1;row2;row3\r\n23.5;;bar\r\n23.5;;\r\n"
)


def test_table_summary_errors(
caplog,
Expand All @@ -707,22 +715,33 @@ def test_table_summary_errors(
# save row document with faulty summary config
table_question.meta = {"summary-question": "summary"}
table_question.save()
row_document = document_factory(form=row_form)
save_answer(question=table_question, document=main_doc, value=[row_document.pk])
assert len(caplog.records) == 1
assert (
caplog.messages[0]
== "Updating table summary: missing info in TQ meta: sq=summary, sm=None"
)
row_document = document_factory(form=row_form)
save_answer(question=table_question, document=main_doc, value=[row_document.pk])
assert len(caplog.records) == 2
assert (
caplog.messages[1]
== "Updating table summary: missing info in TQ meta: sq=summary, sm=None"
)

# save row document with faulty summary config
table_question.meta = {"summary-question": "summary", "summary-mode": "missing"}
table_question.save()
assert len(caplog.records) == 3
assert (
caplog.messages[2]
== f'Updating table summary: summary mode "missing" does not exist. '
f"Must be one of {settings.TABLE_SUMMARY_MODES}"
)
row_document = document_factory(form=row_form)
save_answer(question=table_question, document=main_doc, value=[row_document.pk])
assert len(caplog.records) == 2
assert len(caplog.records) == 4
assert (
caplog.messages[1]
caplog.messages[3]
== f'Updating table summary: summary mode "missing" does not exist. '
f"Must be one of {settings.TABLE_SUMMARY_MODES}"
)
Expand All @@ -732,8 +751,8 @@ def test_table_summary_errors(
table_question.save()
row_document = document_factory(form=row_form)
save_answer(question=table_question, document=main_doc, value=[row_document.pk])
assert len(caplog.records) == 3
assert len(caplog.records) == 5
assert (
caplog.messages[2]
caplog.messages[4]
== "Updating table summary: missing info in TQ meta: sq=None, sm=csv"
)

0 comments on commit bf49b0a

Please sign in to comment.