From 9de6667a0501574e20345d19ee5aaddcbba583f6 Mon Sep 17 00:00:00 2001 From: juuso-j Date: Mon, 5 Feb 2024 11:16:23 +0200 Subject: [PATCH 1/3] Format --- profiles/tests/conftest.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/profiles/tests/conftest.py b/profiles/tests/conftest.py index 5df5389..62df38b 100644 --- a/profiles/tests/conftest.py +++ b/profiles/tests/conftest.py @@ -42,9 +42,7 @@ def questions(): question="How often do you use following means of public transport?", number="2" ) Question.objects.create(question="Why do you use train?", number="3") - Question.objects.create(question="Questions about car driving", number="4") - return Question.objects.all() From 17069a4b639638563819010b8fca73e360f64d16 Mon Sep 17 00:00:00 2001 From: juuso-j Date: Mon, 5 Feb 2024 11:20:59 +0200 Subject: [PATCH 2/3] Test get_sub_questions_conditions_states endpoint --- profiles/tests/api/test_question.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/profiles/tests/api/test_question.py b/profiles/tests/api/test_question.py index 83294d9..4fe56dc 100644 --- a/profiles/tests/api/test_question.py +++ b/profiles/tests/api/test_question.py @@ -8,6 +8,31 @@ from profiles.models import Answer, PostalCodeResult +@pytest.mark.django_db +@pytest.mark.django_db +def test_sub_questions_conditions_states( + api_client, + users, + answers, + questions, + question_conditions, + sub_questions, + sub_question_conditions, +): + url = reverse("profiles:question-get-sub-questions-conditions-states") + user = users.get(username="car user") + token = Token.objects.create(user=user) + api_client.credentials(HTTP_AUTHORIZATION="Token " + token.key) + response = api_client.get(url) + assert response.status_code == 200 + assert response.json()[0]["state"] is True + user = users.get(username="non car user") + token = Token.objects.create(user=user) + api_client.credentials(HTTP_AUTHORIZATION="Token " + token.key) + response = api_client.get(url) + assert response.json()[0]["state"] is False + + @pytest.mark.django_db def test_questions_condition_states_not_authenticated( api_client, users, questions, question_conditions From 48cfff4719c338e98bafc250596c5b7766b79cbe Mon Sep 17 00:00:00 2001 From: juuso-j Date: Mon, 5 Feb 2024 11:55:55 +0200 Subject: [PATCH 3/3] Add view that returns the condition states for sub questions --- profiles/api/views.py | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/profiles/api/views.py b/profiles/api/views.py index 8e76c8c..d317b83 100644 --- a/profiles/api/views.py +++ b/profiles/api/views.py @@ -217,8 +217,8 @@ def get_questions_with_conditions(self, request): return self.get_paginated_response(serializer.data) @extend_schema( - description="Returns current state of condition for all questions that have a condition." - "If true, the condition has been met and can be displayed for the user.", + description="Returns the current state of conditions for all questions that have a condition." + "If true, the condition has been met and the question can be displayed for the user.", parameters=[], examples=None, responses={ @@ -247,6 +247,38 @@ def get_questions_conditions_states(self, request): else: return Response(serializer.errors, status=400) + @extend_schema( + description="Returns the current state of conditions for all Sub questions that have a condition." + "If true, the condition has been met and the sub question can be displayed for the user.", + parameters=[], + examples=None, + responses={ + 200: OpenApiResponse( + description="List of states, containing the sub question ID and the state." + ) + }, + ) + @action(detail=False, methods=["GET"], permission_classes=[IsAuthenticated]) + def get_sub_questions_conditions_states(self, request): + sub_questions_with_cond_qs = SubQuestion.objects.filter( + sub_question_conditions__isnull=False + ) + user = request.user + states = [] + for sub_question in sub_questions_with_cond_qs: + state = {"id": sub_question.id} + sub_question_condition = SubQuestionCondition.objects.filter( + sub_question=sub_question + ).first() + state["state"] = sub_question_condition_met(sub_question_condition, user) + states.append(state) + serializer = QuestionsConditionsStatesSerializer(data=states, many=True) + if serializer.is_valid(): + validated_data = serializer.validated_data + return Response(validated_data) + else: + return Response(serializer.errors, status=400) + @action( detail=False, methods=["GET"],