Fakultas Ilmu Komputer UI

Commit b1f1ae70 authored by Dave Nathanael's avatar Dave Nathanael
Browse files

Implement pagination and filter for Case models

parent 73b4cecf
from django_filters import FilterSet
from apps.cases.models import (
CaseSubject,
InvestigationCase,
MonitoringCase,
PositiveCase,
)
class CaseSubjectFilter(FilterSet):
class Meta:
model = CaseSubject
fields = [
"age",
"is_male",
"address",
"district",
"sub_district",
]
class PositiveCaseFilter(FilterSet):
class Meta:
model = PositiveCase
fields = [
"case_subject_id",
"outcome",
"author",
]
class InvestigationCaseFilter(FilterSet):
class Meta:
model = InvestigationCase
fields = [
"case_subject_id",
"reference_case_id",
"case_relation",
"medical_symptoms",
"risk_factors",
"is_referral_needed",
"medical_facility_reference",
"author",
]
class MonitoringCaseFilter(FilterSet):
class Meta:
model = MonitoringCase
fields = [
"investigation_case_id",
"positive_case_id",
"is_referred",
"is_checked",
"is_regularly_treated",
"treatment_start_date",
"treatment_end_date",
"outcome",
"author",
]
......@@ -35,21 +35,56 @@ class CaseSubjectViewTest(APITestCase):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
data = [
{
"revision_id": str(self.case_subject_1.revision_id),
"subject_id": str(self.case_subject_1.subject_id),
"name": self.case_subject_1.name,
"age": self.case_subject_1.age,
"is_male": self.case_subject_1.is_male,
"address": self.case_subject_1.address,
"district": self.case_subject_1.district,
"sub_district": self.case_subject_1.sub_district,
"is_active": self.case_subject_1.is_active,
},
]
data = {
"count": 1,
"next": None,
"previous": None,
"results": [
{
"revision_id": str(self.case_subject_1.revision_id),
"subject_id": str(self.case_subject_1.subject_id),
"name": self.case_subject_1.name,
"age": self.case_subject_1.age,
"is_male": self.case_subject_1.is_male,
"address": self.case_subject_1.address,
"district": self.case_subject_1.district,
"sub_district": self.case_subject_1.sub_district,
"is_active": self.case_subject_1.is_active,
},
],
}
self.assertJSONEqual(json.dumps(response.data), data)
def test_list_case_subjects_paginate_failed(self):
url = "/cases/case-subjects/?page=100"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
def test_list_case_subjects_filter_success(self):
url = "/cases/case-subjects/?district=" + self.case_subject_1.district
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
def test_list_case_subjects_filter_failed(self):
url = "/cases/case-subjects/?district=1234567890"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_retrieve_case_subject_success(self):
url = "/cases/case-subjects/" + str(self.case_subject_1.revision_id) + "/"
......
......@@ -54,6 +54,50 @@ class InvestigationCaseViewTest(APITestCase):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
self.assertIn('"next":null', response_string)
self.assertIn('"previous":null', response_string)
self.assertIn(str(self.case.revision_id), response_string)
self.assertIn(str(self.case.case_id), response_string)
self.assertIn(str(self.case.case_subject.revision_id), response_string)
self.assertIn(str(self.case.case_subject.subject_id), response_string)
self.assertIn(str(self.case.reference_case.revision_id), response_string)
self.assertIn(str(self.case.reference_case.case_id), response_string)
self.assertIn(str(self.case.reference_case.author.id), response_string)
self.assertIn(str(self.case.author.id), response_string)
def test_list_investigation_case_paginate_failed(self):
url = "/cases/investigation-cases/?page=100"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
def test_list_investigation_case_filter_success(self):
url = "/cases/investigation-cases/?risk_factors=" + self.case.risk_factors
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
def test_list_investigation_case_filter_failed(self):
url = "/cases/investigation-cases/?risk_factors=1234567890"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_retrieve_investigation_case_success(self):
url = "/cases/investigation-cases/" + str(self.case.revision_id) + "/"
......
......@@ -52,6 +52,42 @@ class MonitoringCaseViewTest(TestCase):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":2', response_string)
self.assertIn('"next":null', response_string)
self.assertIn('"previous":null', response_string)
def test_list_monitoring_case_paginate_failed(self):
url = "/cases/monitoring-cases/?page=100"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
def test_list_monitoring_case_filter_success(self):
url = "/cases/monitoring-cases/?outcome=" + self.case_1.outcome
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":2', response_string)
def test_list_monitoring_case_filter_failed(self):
url = "/cases/monitoring-cases/?outcome=1234567890"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_retrieve_monitoring_case_success(self):
url = "/cases/monitoring-cases/" + str(self.case_2.revision_id) + "/"
......
......@@ -44,6 +44,47 @@ class PositiveCaseViewTest(APITestCase):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
self.assertIn('"next":null', response_string)
self.assertIn('"previous":null', response_string)
self.assertIn(str(self.case.revision_id), response_string)
self.assertIn(str(self.case.case_id), response_string)
self.assertIn(str(self.case.case_subject.revision_id), response_string)
self.assertIn(str(self.case.case_subject.subject_id), response_string)
self.assertIn(str(self.case.author.id), response_string)
def test_list_positive_case_paginate_failed(self):
url = "/cases/positive-cases/?page=100"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"detail":"Invalid page."', response_string)
def test_list_positive_case_filter_success(self):
url = "/cases/positive-cases/?outcome=" + self.case.outcome
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":1', response_string)
def test_list_positive_case_filter_failed(self):
url = "/cases/positive-cases/?outcome=1234567890"
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
response_string = response.rendered_content.decode("utf-8")
self.assertIn('"count":0', response_string)
def test_retrieve_positive_case_success(self):
url = "/cases/positive-cases/" + str(self.case.revision_id) + "/"
......
......@@ -4,6 +4,9 @@ from django.contrib.auth.models import User
from django.shortcuts import get_object_or_404
from rest_framework import status, viewsets
from rest_framework.response import Response
from rest_framework.pagination import PageNumberPagination
from django_filters import FilterSet
from django_filters.rest_framework import DjangoFilterBackend
from apps.cases.models import (
CaseSubject,
......@@ -20,19 +23,29 @@ from apps.cases.serializers import (
PositiveCaseSummarySerializer,
PositiveCaseSerializer,
)
from apps.cases.filters import (
CaseSubjectFilter,
InvestigationCaseFilter,
MonitoringCaseFilter,
PositiveCaseFilter,
)
from apps.commons.permissions import IsAuthorOrAdministrator
class CaseSubjectViewSet(viewsets.ViewSet):
serializer_class = CaseSubjectSerializer
queryset = CaseSubject.objects.active_revisions()
filter_backends = (DjangoFilterBackend,)
permission_classes = [
IsAuthorOrAdministrator,
]
def list(self, request):
serializer = self.serializer_class(self.queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
paginator = PageNumberPagination()
filtered_set = CaseSubjectFilter(request.GET, queryset=self.queryset).qs
context = paginator.paginate_queryset(filtered_set, request)
serializer = self.serializer_class(context, many=True)
return paginator.get_paginated_response(serializer.data)
def retrieve(self, request, pk=None):
instance = get_object_or_404(self.queryset, pk=pk)
......@@ -75,13 +88,17 @@ class CaseSubjectViewSet(viewsets.ViewSet):
class InvestigationCaseViewSet(viewsets.ViewSet):
queryset = InvestigationCase.objects.active_revisions()
filter_backends = (DjangoFilterBackend,)
permission_classes = [
IsAuthorOrAdministrator,
]
def list(self, request):
serializer = InvestigationCaseSummarySerializer(self.queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
paginator = PageNumberPagination()
filtered_set = InvestigationCaseFilter(request.GET, queryset=self.queryset).qs
context = paginator.paginate_queryset(filtered_set, request)
serializer = InvestigationCaseSummarySerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)
def retrieve(self, request, pk=None):
instance = get_object_or_404(self.queryset, pk=pk)
......@@ -118,10 +135,14 @@ class InvestigationCaseViewSet(viewsets.ViewSet):
class MonitoringCaseViewSet(viewsets.ViewSet):
queryset = MonitoringCase.objects.active_revisions()
filter_backends = (DjangoFilterBackend,)
def list(self, request):
serializer = MonitoringCaseSummarySerializer(self.queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
paginator = PageNumberPagination()
filtered_set = MonitoringCaseFilter(request.GET, queryset=self.queryset).qs
context = paginator.paginate_queryset(filtered_set, request)
serializer = MonitoringCaseSummarySerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)
def retrieve(self, request, pk=None):
instance = get_object_or_404(self.queryset, pk=pk)
......@@ -159,13 +180,17 @@ class MonitoringCaseViewSet(viewsets.ViewSet):
class PositiveCaseViewSet(viewsets.ViewSet):
queryset = PositiveCase.objects.active_revisions()
filter_backends = (DjangoFilterBackend,)
permission_classes = [
IsAuthorOrAdministrator,
]
def list(self, request):
serializer = PositiveCaseSummarySerializer(self.queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
paginator = PageNumberPagination()
filtered_set = PositiveCaseFilter(request.GET, queryset=self.queryset).qs
context = paginator.paginate_queryset(filtered_set, request)
serializer = PositiveCaseSummarySerializer(context, many=True)
return paginator.get_paginated_response(serializer.data)
def retrieve(self, request, pk=None):
instance = get_object_or_404(self.queryset, pk=pk)
......
......@@ -120,6 +120,7 @@ AUTH_PASSWORD_VALIDATORS = [
REST_FRAMEWORK = {
"DEFAULT_PAGINATION_CLASS": "rest_framework.pagination.PageNumberPagination",
"PAGE_SIZE": 10,
"DEFAULT_FILTER_BACKENDS": ("django_filters.rest_framework.DjangoFilterBackend",),
"DEFAULT_AUTHENTICATION_CLASSES": [
"rest_framework.authentication.TokenAuthentication",
],
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment