From 36a60809033dfa3b8d34b45a0369f0224e5b94ab Mon Sep 17 00:00:00 2001 From: David Vogt Date: Tue, 14 Jan 2025 16:55:25 +0100 Subject: [PATCH] fix(structure): correctly sort questions and table rows 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. --- caluma/caluma_form/structure.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/caluma/caluma_form/structure.py b/caluma/caluma_form/structure.py index 687b1b090..503af7216 100644 --- a/caluma/caluma_form/structure.py +++ b/caluma/caluma_form/structure.py @@ -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): @@ -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( @@ -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):