Fakultas Ilmu Komputer UI

Commit 34d913fc authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

[GREEN] Implement csv fields formatter and field renamer

parent 6122aa02
Pipeline #49751 failed with stages
in 1 minute and 31 seconds
from rest_framework_csv.renderers import CSVRenderer
INVESTIGATION_CASE_RENDERER_FIELDS = [
"id",
"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",
]
from apps.exportables.constants import INVESTIGATION_CASE_HEADER_FIELDS
class InvestigationCaseCSVRenderer(CSVRenderer):
header = INVESTIGATION_CASE_RENDERER_FIELDS
header = INVESTIGATION_CASE_HEADER_FIELDS
import json
import time
from django.utils import timezone
from apps.exportables.constants import (
DISTRICT,
DISTRICTS,
......@@ -6,10 +10,15 @@ from apps.exportables.constants import (
NEGATIVE,
POSITIVE,
TOTAL,
UNDETERMINED
UNDETERMINED,
INVESTIGATION_CASE_HEADER_FIELDS,
INVESTIGATION_CASE_RENDERER_FIELDS,
)
# CASE STATISTIC COUNTS
def generate_initial_counts():
return {
POSITIVE: 0,
......@@ -43,3 +52,40 @@ def map_sex_value(sex):
if sex:
return MALE
return FEMALE
# CSV EXPORTS
def format_custom_csv_rows(cases_csv_rows):
formated_rows = []
FIELDS_LENGTH = len(INVESTIGATION_CASE_RENDERER_FIELDS)
for row in cases_csv_rows:
formated_row = {}
for field_name_index in range(FIELDS_LENGTH):
original_field_name = INVESTIGATION_CASE_RENDERER_FIELDS[field_name_index]
target_field_name = INVESTIGATION_CASE_HEADER_FIELDS[field_name_index]
formated_row[target_field_name] = row[original_field_name]
value = formated_row["jenis_kelamin"]
formated_row["jenis_kelamin"] = "Laki-laki" if value else "Perempuan"
value = json.loads(formated_row["gejala_medis"])
selected_values = [key for key in value.keys() if value[key]]
formated_row["gejala_medis"] = "; ".join(selected_values)
value = json.loads(formated_row["faktor_risiko"])
selected_values = [key for key in value.keys() if value[key]]
formated_row["faktor_risiko"] = "; ".join(selected_values)
value = formated_row["tanggal_pencatatan"]
formated_row["tanggal_pencatatan"] = timezone.localtime(value)
formated_rows.append(formated_row)
return formated_rows
......@@ -9,6 +9,7 @@ from apps.cases.models import (
CaseSubject,
InvestigationCase,
)
from apps.commons.permissions import IsAuthenticated
from apps.exportables.constants import (
AGE,
DISTRICT,
......@@ -19,31 +20,32 @@ from apps.exportables.constants import (
POSITIVE,
SEX,
TOTAL,
UNDETERMINED
UNDETERMINED,
INVESTIGATION_CASE_RENDERER_FIELDS,
)
from apps.exportables.utils import (
format_custom_csv_rows,
generate_initial_counts,
generate_initial_group_counts,
map_outcome_value,
map_sex_value,
)
from apps.exportables.renderers import (
InvestigationCaseCSVRenderer,
INVESTIGATION_CASE_RENDERER_FIELDS,
)
from apps.exportables.renderers import InvestigationCaseCSVRenderer
class CaseCountsView(APIView):
permission_classes = (IsAuthenticated,)
def get(self, request, format=None):
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")
)
investigation_cases = (
InvestigationCase.objects
.values("case_subject")
.annotate(latest_investigation_case=Max("created_at"))
.order_by()
.values("case_subject__id", "is_positive")
)
outcomes = {}
......@@ -91,6 +93,7 @@ class CaseCountsView(APIView):
class ExportInvestigationCaseView(APIView):
# permission_classes = (IsAuthenticated,)
renderer_classes = (InvestigationCaseCSVRenderer,)
def get(self, request, format=None):
......@@ -127,9 +130,14 @@ class ExportInvestigationCaseView(APIView):
cases.prefetch_related(
"case_subject",
"reference_case",
"author").values(
*INVESTIGATION_CASE_RENDERER_FIELDS))
"author"
).values(
*INVESTIGATION_CASE_RENDERER_FIELDS
)
)
formated_data = format_custom_csv_rows(data)
filename = "cases-{}.csv".format(timezone.localtime(timezone.now()))
headers = {"Content-Disposition": 'attachment; filename="{}"'.format(filename)}
return Response(data, headers=headers)
return Response(formated_data, headers=headers)
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