Fakultas Ilmu Komputer UI

Commit 3adac60b authored by Dave Nathanael's avatar Dave Nathanael
Browse files

[GREEN] Implement CSV export for InvestigationCase

parent 3ce596a3
Pipeline #47925 failed with stages
in 13 minutes and 56 seconds
from rest_framework_csv.renderers import CSVRenderer
INVESTIGATION_CASE_RENDERER_FIELDS = [
"id",
"case_subject__name",
"case_subject__age",
"case_subject__is_male",
"case_subject__district",
"outcome",
"is_positive",
"case_relation",
"medical_symptoms",
"risk_factors",
"is_referral_needed",
"medical_facility_reference",
"created_at",
"author__name",
]
class InvestigationCaseCSVRenderer(CSVRenderer):
header = INVESTIGATION_CASE_RENDERER_FIELDS
from django.urls import path
from apps.exportables.views import CaseCountsView
from apps.exportables.views import CaseCountsView, ExportInvestigationCaseView
urlpatterns = [
path("", CaseCountsView.as_view()),
path("investigation-cases-csv/", ExportInvestigationCaseView.as_view()),
]
from django.http import JsonResponse
from django.db.models import Max
from rest_framework import status
from rest_framework.response import Response
from rest_framework.views import APIView
from apps.cases.models import (
......@@ -25,6 +26,10 @@ from apps.exportables.utils import (
map_outcome_value,
map_sex_value,
)
from apps.exportables.renderers import (
InvestigationCaseCSVRenderer,
INVESTIGATION_CASE_RENDERER_FIELDS,
)
class CaseCountsView(APIView):
......@@ -33,11 +38,11 @@ class CaseCountsView(APIView):
case_subjects = CaseSubject.objects.all()
investigation_cases = (InvestigationCase.objects
.values("case_subject")
.annotate(latest_investigation_case=Max("created_at"))
.order_by()
.values("case_subject__id", "is_positive")
)
.values("case_subject")
.annotate(latest_investigation_case=Max("created_at"))
.order_by()
.values("case_subject__id", "is_positive")
)
outcomes = {}
......@@ -82,3 +87,26 @@ class CaseCountsView(APIView):
}
return JsonResponse(data=data, status=status.HTTP_200_OK)
class ExportInvestigationCaseView(APIView):
renderer_classes = (InvestigationCaseCSVRenderer,)
def get(self, request, format=None):
cases = InvestigationCase.objects.all()
query_is_male = self.request.query_params.get('is_male', None)
query_age = self.request.query_params.get('age', None)
query_district = self.request.query_params.get('district', None)
if query_is_male is not None:
cases = cases.filter(case_subject__is_male=(query_is_male.lower() == 'true'))
if query_age is not None:
cases = cases.filter(case_subject__age=int(query_age))
if query_district is not None:
cases = cases.filter(case_subject__district=query_district)
data = list(cases.prefetch_related("case_subject",
"reference_case", "author").values(*INVESTIGATION_CASE_RENDERER_FIELDS))
headers = {"Content-Disposition": 'attachment; filename="cases.csv"'}
return Response(data, headers=headers)
......@@ -10,6 +10,7 @@ django-cors-headers==3.2.0
django-filter==2.2.0
django-sendgrid-v5==0.8.1
djangorestframework==3.11.0
djangorestframework-csv==2.1.0
entrypoints==0.3
factory-boy==2.12.0
Faker==4.0.1
......@@ -31,3 +32,4 @@ sqlparse==0.3.0
text-unidecode==1.3
toml==0.10.0
typed-ast==1.4.1
unicodecsv==0.14.1
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