Skip to content

Commit

Permalink
fix(structure): correctly sort questions and table rows
Browse files Browse the repository at this point in the history
Table rows were sorted, but backwards; questions were not sorted at all,
and thus might have lead to unpredictable behaviour. We noe explicitly
sort this correctly, therefore making things a bit more testable.
  • Loading branch information
winged committed Feb 26, 2025
1 parent 85a4d55 commit 36a6080
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions caluma/caluma_form/structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from functools import singledispatch
from typing import List, Optional

from .models import Question
from .models import FormQuestion, Question


def object_local_memoise(method):
Expand Down Expand Up @@ -123,7 +123,7 @@ def children(self):
# for the answerdocument_set. Sorting in DB would re-issue the query
rows = sorted(
self.answer.answerdocument_set.all(),
key=lambda answer_document: answer_document.sort,
key=lambda answer_document: -answer_document.sort,
)
return [
FieldSet(
Expand Down Expand Up @@ -243,15 +243,17 @@ def get_field(
@object_local_memoise
def children(self):
answers = {ans.question_id: ans for ans in self.document.answers.all()}
formquestions = FormQuestion.objects.filter(form=self.form).order_by("-sort")

return [
Field.factory(
document=self.document,
form=self.form,
question=question,
answer=answers.get(question.slug),
question=fq.question,
answer=answers.get(fq.question.slug),
parent=self,
)
for question in self.form.questions.all()
for fq in formquestions
]

def set_answer(self, question_slug, answer):
Expand Down

0 comments on commit 36a6080

Please sign in to comment.