Fakultas Ilmu Komputer UI

Commit c9e06777 authored by Jonathan Christopher Jakub's avatar Jonathan Christopher Jakub
Browse files

[GREEN] Modify new endpoints to refer to main id instead of revision id

parent 2eaa2679
Pipeline #34906 passed with stages
in 3 minutes and 32 seconds
# Generated by Django 3.0.1 on 2020-03-04 22:17
from django.db import migrations, models
import django.db.models.deletion
import uuid
......@@ -38,54 +40,7 @@ class Migration(migrations.Migration):
options={
"verbose_name_plural": "case subjects",
"db_table": "case_subject",
},
),
migrations.CreateModel(
name="InvestigationCase",
fields=[
(
"revision_id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("case_id", models.UUIDField(default=uuid.uuid4)),
("case_relation", models.CharField(blank=True, max_length=128)),
("medical_symptoms", models.TextField(default="{}")),
("risk_factors", models.TextField(default="{}")),
("is_referral_needed", models.BooleanField(db_index=True)),
(
"medical_facility_reference",
models.CharField(blank=True, max_length=128),
),
("deleted_at", models.DateTimeField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("is_active", models.BooleanField(db_index=True, default=True)),
(
"author",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="investigation_case",
to="accounts.Account",
),
),
(
"case_subject",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="investigation_case",
to="cases.CaseSubject",
),
),
],
options={
"verbose_name_plural": "investigation cases",
"db_table": "investigation_case",
"ordering": ["-created_at"],
},
),
migrations.CreateModel(
......@@ -101,6 +56,7 @@ class Migration(migrations.Migration):
),
),
("case_id", models.UUIDField(default=uuid.uuid4)),
("case_subject_id", models.UUIDField(blank=True, null=True)),
("outcome", models.CharField(max_length=256)),
("deleted_at", models.DateTimeField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
......@@ -115,18 +71,11 @@ class Migration(migrations.Migration):
to="accounts.Account",
),
),
(
"case_subject",
models.ForeignKey(
on_delete=django.db.models.deletion.CASCADE,
related_name="positive_case",
to="cases.CaseSubject",
),
),
],
options={
"verbose_name_plural": "positive cases",
"db_table": "positive_case",
"ordering": ["-created_at"],
},
),
migrations.CreateModel(
......@@ -142,6 +91,8 @@ class Migration(migrations.Migration):
),
),
("case_id", models.UUIDField(default=uuid.uuid4)),
("investigation_case_id", models.UUIDField(blank=True, null=True)),
("positive_case_id", models.UUIDField(blank=True, null=True)),
(
"is_referred",
models.BooleanField(blank=True, db_index=True, null=True),
......@@ -171,41 +122,54 @@ class Migration(migrations.Migration):
to="accounts.Account",
),
),
],
options={
"verbose_name_plural": "monitoring cases",
"db_table": "monitoring_case",
"ordering": ["-created_at"],
},
),
migrations.CreateModel(
name="InvestigationCase",
fields=[
(
"investigation_case",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="investigation_monitoring_case",
to="cases.InvestigationCase",
"revision_id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("case_id", models.UUIDField(default=uuid.uuid4)),
("case_subject_id", models.UUIDField(blank=True, null=True)),
("reference_case_id", models.UUIDField(blank=True, null=True)),
("case_relation", models.CharField(blank=True, max_length=128)),
("medical_symptoms", models.TextField(default="{}")),
("risk_factors", models.TextField(default="{}")),
("is_referral_needed", models.BooleanField(db_index=True)),
(
"medical_facility_reference",
models.CharField(blank=True, max_length=128),
),
("deleted_at", models.DateTimeField(blank=True, null=True)),
("created_at", models.DateTimeField(auto_now_add=True)),
("is_active", models.BooleanField(db_index=True, default=True)),
(
"positive_case",
"author",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="positive_monitoring_case",
to="cases.PositiveCase",
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="investigation_case",
to="accounts.Account",
),
),
],
options={
"verbose_name_plural": "monitoring cases",
"db_table": "monitoring_case",
"verbose_name_plural": "investigation cases",
"db_table": "investigation_case",
"ordering": ["-created_at"],
},
),
migrations.AddField(
model_name="investigationcase",
name="reference_case",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="investigation_case",
to="cases.PositiveCase",
),
),
]
......@@ -26,20 +26,19 @@ class CaseSubject(models.Model):
class Meta:
db_table = "case_subject"
verbose_name_plural = "case subjects"
ordering = ["-created_at"]
def delete(self):
if self.deleted_at is None:
self.is_active = False
self.deleted_at = datetime.now(tz=pytz.UTC)
self.deleted_at = datetime.now(tz=pytz.timezone("Asia/Jakarta"))
self.save()
class PositiveCase(models.Model):
revision_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
case_id = models.UUIDField(default=uuid.uuid4)
case_subject = models.ForeignKey(
CaseSubject, on_delete=models.CASCADE, related_name="positive_case"
)
case_subject_id = models.UUIDField(blank=True, null=True)
outcome = models.CharField(max_length=256)
author = models.ForeignKey(
Account,
......@@ -57,27 +56,26 @@ class PositiveCase(models.Model):
class Meta:
db_table = "positive_case"
verbose_name_plural = "positive cases"
ordering = ["-created_at"]
def delete(self):
if self.deleted_at is None:
self.is_active = False
self.deleted_at = datetime.now(tz=pytz.UTC)
self.deleted_at = datetime.now(tz=pytz.timezone("Asia/Jakarta"))
self.save()
@property
def case_subject(self):
return CaseSubject.objects.filter(
is_active=True, deleted_at__isnull=True, subject_id=self.case_subject_id
).first()
class InvestigationCase(models.Model):
revision_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
case_id = models.UUIDField(default=uuid.uuid4)
case_subject = models.ForeignKey(
CaseSubject, on_delete=models.CASCADE, related_name="investigation_case"
)
reference_case = models.ForeignKey(
PositiveCase,
blank=True,
null=True,
on_delete=models.CASCADE,
related_name="investigation_case",
)
case_subject_id = models.UUIDField(blank=True, null=True)
reference_case_id = models.UUIDField(blank=True, null=True)
case_relation = models.CharField(max_length=128, blank=True)
medical_symptoms = models.TextField(default="{}")
risk_factors = models.TextField(default="{}")
......@@ -99,31 +97,32 @@ class InvestigationCase(models.Model):
class Meta:
db_table = "investigation_case"
verbose_name_plural = "investigation cases"
ordering = ["-created_at"]
def delete(self):
if self.deleted_at is None:
self.is_active = False
self.deleted_at = datetime.now(tz=pytz.UTC)
self.deleted_at = datetime.now(tz=pytz.timezone("Asia/Jakarta"))
self.save()
@property
def reference_case(self):
return PositiveCase.objects.filter(
is_active=True, deleted_at__isnull=True, case_id=self.reference_case_id
).first()
@property
def case_subject(self):
return CaseSubject.objects.filter(
is_active=True, deleted_at__isnull=True, subject_id=self.case_subject_id
).first()
class MonitoringCase(models.Model):
revision_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
case_id = models.UUIDField(default=uuid.uuid4)
investigation_case = models.ForeignKey(
InvestigationCase,
blank=True,
null=True,
on_delete=models.CASCADE,
related_name="investigation_monitoring_case",
)
positive_case = models.ForeignKey(
PositiveCase,
blank=True,
null=True,
on_delete=models.CASCADE,
related_name="positive_monitoring_case",
)
investigation_case_id = models.UUIDField(blank=True, null=True)
positive_case_id = models.UUIDField(blank=True, null=True)
is_referred = models.BooleanField(blank=True, null=True, db_index=True)
is_checked = models.BooleanField(blank=True, null=True, db_index=True)
checking_date = models.DateField(blank=True, null=True)
......@@ -147,6 +146,7 @@ class MonitoringCase(models.Model):
class Meta:
db_table = "monitoring_case"
verbose_name_plural = "monitoring cases"
ordering = ["-created_at"]
def save(self, *args, **kwargs):
self.clean()
......@@ -155,7 +155,7 @@ class MonitoringCase(models.Model):
def clean(self):
super(MonitoringCase, self).clean()
reference_cases = [self.positive_case, self.investigation_case]
reference_cases = [self.positive_case_id, self.investigation_case_id]
if sum([case is not None for case in reference_cases]) > 1:
raise ValidationError(
"Monitoring Case should refer to only 1 type of case (Investigation|Positive)"
......@@ -164,5 +164,17 @@ class MonitoringCase(models.Model):
def delete(self):
if self.deleted_at is None:
self.is_active = False
self.deleted_at = datetime.now(tz=pytz.UTC)
self.deleted_at = datetime.now(tz=pytz.timezone("Asia/Jakarta"))
self.save()
@property
def investigation_case(self):
return InvestigationCase.objects.filter(
is_active=True, deleted_at__isnull=True, case_id=self.investigation_case_id
).first()
@property
def positive_case(self):
return PositiveCase.objects.filter(
is_active=True, deleted_at__isnull=True, case_id=self.positive_case_id
).first()
......@@ -8,6 +8,8 @@ from apps.cases.models import (
class CaseSubjectSerializer(serializers.ModelSerializer):
is_active = serializers.BooleanField(read_only=True)
class Meta:
model = CaseSubject
fields = [
......@@ -19,29 +21,71 @@ class CaseSubjectSerializer(serializers.ModelSerializer):
"address",
"district",
"sub_district",
"is_active",
]
class InvestigationCaseSummarySerializer(serializers.ModelSerializer):
case_subject = CaseSubjectSerializer(read_only=True)
class PositiveCaseSerializer(serializers.ModelSerializer):
is_active = serializers.BooleanField(read_only=True)
class Meta:
model = InvestigationCase
model = PositiveCase
fields = [
"revision_id",
"case_id",
"case_subject_id",
"outcome",
"author",
"is_active",
]
class PositiveCaseSummarySerializer(serializers.ModelSerializer):
case_subject = serializers.SerializerMethodField()
is_active = serializers.BooleanField(read_only=True)
class Meta:
model = PositiveCase
fields = [
"revision_id",
"case_id",
"case_subject",
"reference_case",
"outcome",
"author",
"is_active",
]
def get_case_subject(self, instance):
case_subject = instance.case_subject
if case_subject:
return CaseSubjectSerializer(case_subject).data
class InvestigationCaseSerializer(serializers.ModelSerializer):
is_active = serializers.BooleanField(read_only=True)
class Meta:
model = InvestigationCase
fields = [
"revision_id",
"case_id",
"case_subject_id",
"reference_case_id",
"case_relation",
"medical_symptoms",
"risk_factors",
"is_referral_needed",
"medical_facility_reference",
"author",
"is_active",
]
class InvestigationCaseSerializer(serializers.ModelSerializer):
class InvestigationCaseSummarySerializer(serializers.ModelSerializer):
case_subject = serializers.SerializerMethodField()
reference_case = serializers.SerializerMethodField()
is_active = serializers.BooleanField(read_only=True)
class Meta:
model = InvestigationCase
fields = [
......@@ -55,17 +99,30 @@ class InvestigationCaseSerializer(serializers.ModelSerializer):
"is_referral_needed",
"medical_facility_reference",
"author",
"is_active",
]
def get_case_subject(self, instance):
case_subject = instance.case_subject
if case_subject:
return CaseSubjectSerializer(case_subject).data
def get_reference_case(self, instance):
reference_case = instance.reference_case
if reference_case:
return PositiveCaseSerializer(reference_case).data
class MonitoringCaseSerializer(serializers.ModelSerializer):
is_active = serializers.BooleanField(read_only=True)
class MonitoringCaseSummarySerializer(serializers.ModelSerializer):
class Meta:
model = MonitoringCase
fields = [
"revision_id",
"case_id",
"investigation_case",
"positive_case",
"investigation_case_id",
"positive_case_id",
"is_referred",
"is_checked",
"is_regularly_treated",
......@@ -73,30 +130,38 @@ class MonitoringCaseSummarySerializer(serializers.ModelSerializer):
"treatment_end_date",
"outcome",
"author",
"is_active",
]
class PositiveCaseSummarySerializer(serializers.ModelSerializer):
case_subject = CaseSubjectSerializer(read_only=True)
class MonitoringCaseSummarySerializer(serializers.ModelSerializer):
investigation_case = serializers.SerializerMethodField()
positive_case = serializers.SerializerMethodField()
is_active = serializers.BooleanField(read_only=True)
class Meta:
model = PositiveCase
model = MonitoringCase
fields = [
"revision_id",
"case_id",
"case_subject",
"investigation_case",
"positive_case",
"is_referred",
"is_checked",
"is_regularly_treated",
"treatment_start_date",
"treatment_end_date",
"outcome",
"author",
"is_active",
]
def get_investigation_case(self, instance):
investigation_case = instance.investigation_case
if investigation_case:
return InvestigationCaseSerializer(investigation_case).data
class PositiveCaseSerializer(serializers.ModelSerializer):
class Meta:
model = PositiveCase
fields = [
"revision_id",
"case_id",
"case_subject",
"outcome",
"author",
]
def get_positive_case(self, instance):
positive_case = instance.positive_case
if positive_case:
return PositiveCaseSerializer(positive_case).data
......@@ -12,7 +12,7 @@ class CaseSubjectViewTest(TestCase):
def setUp(self):
self.case_subject_1 = CaseSubjectFactory(is_active=True)
self.case_subject_2 = CaseSubjectFactory(
is_active=True, deleted_at=datetime.now(tz=pytz.UTC)
is_active=True, deleted_at=datetime.now(tz=pytz.timezone("Asia/Jakarta"))
)
self.case_subject_3 = CaseSubjectFactory(is_active=False)
......@@ -192,10 +192,6 @@ class CaseSubjectViewTest(TestCase):
self.assertEqual(current_deleted_count, prev_deleted_count + 1)
def test_delete_inactive_case_subject_fails(self):
url = (
"/cases/case-subjects/"
+ str(self.case_subject_3.revision_id)
+ "/"
)
url = "/cases/case-subjects/" + str(self.case_subject_3.revision_id) + "/"
response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
......@@ -31,7 +31,7 @@ class InvestigationCaseViewTest(TestCase):
author=self.author,
case_subject_id=self.case_subject.subject_id,
reference_case_id=self.reference_case.case_id,
deleted_at=datetime.now(tz=pytz.UTC),
deleted_at=datetime.now(tz=pytz.timezone("Asia/Jakarta")),
)
self.other_inactive_case = InvestigationCaseFactory(
author=self.author,
......@@ -149,9 +149,13 @@ class InvestigationCaseViewTest(TestCase):
current_active_investigation_case_revision_count,
prev_active_investigation_case_revision_count,
)
def test_edit_inactive_investigation_case_fails(self):
url = "/cases/investigation-cases/" + str(self.other_inactive_case.revision_id) + "/"
url = (
"/cases/investigation-cases/"
+ str(self.other_inactive_case.revision_id)
+ "/"
)
data = {
"case_subject_id": str(self.other_inactive_case.case_subject_id),
"reference_case_id": str(self.reference_case.case_id),
......@@ -167,7 +171,6 @@ class InvestigationCaseViewTest(TestCase):
)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
def test_edit_investigation_case_fails_with_incomplete_data(self):
url = "/cases/investigation-cases/" + str(self.case.revision_id) + "/"
......
......@@ -38,13 +38,14 @@ class MonitoringCaseViewTest(TestCase):
self.other_deleted_case = MonitoringCaseFactory(
author=self.author,
positive_case_id=self.positive_case.case_id,
deleted_at=datetime.now(tz=pytz.UTC),
deleted_at=datetime.now(tz=pytz.timezone("Asia/Jakarta")),
)
self.other_inactive_case = MonitoringCaseFactory(
author=self.author, positive_case_id=self.positive_case.case_id, is_active=False,
author=self.author,
positive_case_id=self.positive_case.case_id,
is_active=False,
)
def test_list_monitoring_cases_success(self):
url = "/cases/monitoring-cases/"
......@@ -121,9 +122,11 @@ class MonitoringCaseViewTest(TestCase):
current_active_monitoring_case_revision_count,
prev_active_monitoring_case_revision_count,
)
def test_edit_inactive_monitoring_case_fails(self):
url = "/cases/monitoring-cases/" + str(self.other_inactive_case.revision_id) + "/"
url = (
"/cases/monitoring-cases/" + str(self.other_inactive_case.revision_id) + "/"
)
data = {
"positive_case_id": str(self.positive_case.revision_id),
......@@ -163,9 +166,7 @@ class MonitoringCaseViewTest(TestCase):
def test_delete_inactive_monitoring_case_fails(self):
url = (
"/cases/monitoring-cases/"
+ str(self.other_inactive_case.revision_id)
+ "/"
"/cases/monitoring-cases/" + str(self.other_inactive_case.revision_id) + "/"
)
response = self.client.delete(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)
......@@ -22,10 +22,12 @@ class PositiveCaseViewTest(TestCase):
self.other_deleted_case = PositiveCaseFactory(
author=self.author,
case_subject_id=self.case_subject.subject_id,
deleted_at=datetime.now(tz=pytz.UTC),
deleted_at=datetime.now(tz=pytz.timezone("Asia/Jakarta")),
)
self.other_inactive_case = PositiveCaseFactory(
author=self.author, case_subject_id=self.case_subject.subject_id, is_active=False,
<