Skip to content

Commit

Permalink
Merge pull request #5 from City-of-Turku/feature/get-sub-questions-co…
Browse files Browse the repository at this point in the history
…nditions-states

Feature/get sub questions conditions states
  • Loading branch information
juuso-j authored Feb 8, 2024
2 parents d5261af + 48cfff4 commit ec5c961
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 4 deletions.
36 changes: 34 additions & 2 deletions profiles/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand Down Expand Up @@ -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"],
Expand Down
25 changes: 25 additions & 0 deletions profiles/tests/api/test_question.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 0 additions & 2 deletions profiles/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()


Expand Down

0 comments on commit ec5c961

Please sign in to comment.