Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/cumulativeresults api #58

Merged
merged 3 commits into from
May 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions mpbackend/excluded_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"/api/v1/question/",
"/api/v1/answer/",
"/api/v1/postalcoderesult/",
"/api/v1/cumulativeresult/",
"/api/account/",
]

Expand Down
15 changes: 15 additions & 0 deletions profiles/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from profiles.models import (
Answer,
CumulativeResultCount,
Option,
PostalCode,
PostalCodeResult,
Expand Down Expand Up @@ -149,3 +150,17 @@ class PostalCodeTypeSerializer(serializers.ModelSerializer):
class Meta:
model = PostalCodeType
fields = "__all__"


class CumulativeResultSerializer(serializers.ModelSerializer):
class Meta:
model = CumulativeResultCount
fields = "__all__"

def to_representation(self, instance):
type_name = self.context.get("type_name")
representation = super().to_representation(instance)
representation["sum_of_count"] = instance.get_sum_of_count(
postal_code_type_name=type_name
)
return representation
43 changes: 42 additions & 1 deletion profiles/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
from rest_framework import status, viewsets
from rest_framework.authtoken.models import Token
from rest_framework.decorators import action
from rest_framework.mixins import CreateModelMixin
from rest_framework.exceptions import ParseError
from rest_framework.mixins import CreateModelMixin, ListModelMixin
from rest_framework.permissions import AllowAny, IsAuthenticated
from rest_framework.response import Response
from rest_framework.viewsets import GenericViewSet
Expand All @@ -27,6 +28,7 @@
from profiles.api.serializers import (
AnswerRequestSerializer,
AnswerSerializer,
CumulativeResultSerializer,
InConditionResponseSerializer,
OptionSerializer,
PostalCodeResultSerializer,
Expand All @@ -44,6 +46,7 @@
)
from profiles.models import (
Answer,
CumulativeResultCount,
Option,
PostalCode,
PostalCodeResult,
Expand Down Expand Up @@ -738,6 +741,44 @@ class PostalCodeViewSet(viewsets.ReadOnlyModelViewSet):
register_view(PostalCodeViewSet, "postalcode")


@extend_schema_view(
list=extend_schema(
parameters=[
POSTAL_CODE_TYPE_PARAM,
],
description="Returns cumulative result count for every result.",
)
)
class CumulativeResultsViewSet(ListModelMixin, GenericViewSet):
queryset = CumulativeResultCount.objects.all()
serializer_class = CumulativeResultSerializer

def list(self, request, *args, **kwargs):
queryset = self.queryset
type_name = ""
postal_code_type_id = request.query_params.get(
"postal_code_type",
PostalCodeType.objects.get(type_name=PostalCodeType.HOME_POSTAL_CODE).id,
)
try:
postal_code_type_id = int(postal_code_type_id)
except ValueError:
raise ParseError("'postal_code_type' must be int")
postal_code_type = PostalCodeType.objects.filter(id=postal_code_type_id).first()
if not postal_code_type:
queryset = CumulativeResultCount.objects.none()
else:
type_name = postal_code_type.type_name
page = self.paginate_queryset(queryset)
serializer = self.serializer_class(
page, many=True, context={"type_name": type_name}
)
return self.get_paginated_response(serializer.data)


register_view(CumulativeResultsViewSet, "cumulativeresult")


class PostalCodeTypeViewSet(viewsets.ReadOnlyModelViewSet):
queryset = PostalCodeType.objects.all()
serializer_class = PostalCodeTypeSerializer
Expand Down
23 changes: 23 additions & 0 deletions profiles/migrations/0022_cumulativeresultcount.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Generated by Django 4.1.13 on 2024-04-30 07:23

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("profiles", "0021_result_num_options"),
]

operations = [
migrations.CreateModel(
name="CumulativeResultCount",
fields=[],
options={
"proxy": True,
"indexes": [],
"constraints": [],
},
bases=("profiles.result",),
),
]
12 changes: 12 additions & 0 deletions profiles/tests/api/test_postal_code_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,3 +305,15 @@ def test_non_existing_postal_code_string(api_client):
response = api_client.get(url)
assert response.status_code == 200
assert response.json()["count"] == 0


@pytest.mark.django_db
def test_cumulative_results(
api_client, results, postal_code_types, postal_code_results
):
url = "/api/v1/cumulativeresult/"
response = api_client.get(url)
assert response.status_code == 200
json_data = response.json()
assert json_data["count"] == results.count()
assert json_data["results"][0]["sum_of_count"] == 6
19 changes: 19 additions & 0 deletions profiles/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Answer,
Option,
PostalCode,
PostalCodeResult,
PostalCodeType,
Question,
QuestionCondition,
Expand Down Expand Up @@ -65,6 +66,24 @@ def postal_code_types():
return PostalCodeType.objects.all()


@pytest.mark.django_db
@pytest.fixture
def postal_code_results(postal_codes, postal_code_types, results):
PostalCodeResult.objects.create(
postal_code=postal_codes.first(),
postal_code_type=postal_code_types.first(),
result=results.first(),
count=4,
)
PostalCodeResult.objects.create(
postal_code=postal_codes.last(),
postal_code_type=postal_code_types.first(),
result=results.first(),
count=2,
)
return PostalCodeResult.objects.all()


@pytest.mark.django_db
@pytest.fixture
def questions_test_result():
Expand Down
Loading