Fakultas Ilmu Komputer UI

Commit 6e864b1c authored by Josh Sudung's avatar Josh Sudung
Browse files

Merge branch 'staging' into 'ci-fix'

# Conflicts:
#   .gitlab-ci.yml
parents 8b4aa67f 5f1cbb65
Pipeline #35198 passed with stages
in 12 minutes and 49 seconds
# Generated by Django 3.0.1 on 2020-02-24 09:13
# Generated by Django 3.0.1 on 2020-03-03 15:02
from django.conf import settings
from django.db import migrations, models
......@@ -34,6 +34,7 @@ class Migration(migrations.Migration):
("is_admin", models.BooleanField(default=False)),
("is_verified", models.BooleanField(default=False)),
("is_active", models.BooleanField(default=False)),
("created_at", models.DateTimeField(auto_now_add=True)),
(
"user",
models.OneToOneField(
......@@ -42,5 +43,6 @@ class Migration(migrations.Migration):
),
),
],
options={"verbose_name_plural": "accounts", "db_table": "account",},
),
]
import uuid
from django.contrib.auth.models import User
from django.db import models
......@@ -14,3 +13,10 @@ class Account(models.Model):
is_admin = models.BooleanField(default=False)
is_verified = models.BooleanField(default=False)
is_active = models.BooleanField(default=False)
created_at = models.DateTimeField(auto_now_add=True)
objects = models.Manager()
class Meta:
db_table = "account"
verbose_name_plural = "accounts"
......@@ -2,7 +2,7 @@ from rest_framework import serializers
from apps.accounts.models import Account
class AccountSummarySerializer(serializers.ModelSerializer):
class AccountSerializer(serializers.ModelSerializer):
username = serializers.CharField(source="user.username", required=False)
class Meta:
......
......@@ -12,8 +12,7 @@ class UserFactory(factory.DjangoModelFactory):
class Meta:
model = User
username = "username"
password = "justpass"
username = factory.Sequence(lambda n: "user_" + str(n))
class AccountFactory(factory.DjangoModelFactory):
......
......@@ -92,6 +92,24 @@ class AccountViewTest(TestCase):
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
self.assertEqual(officer_current_count, officer_prev_count + 1)
def test_fail_create_new_account_with_no_auth_info(self):
url = "/accounts/"
_account_id = self.faker.email()
data = {
"name": self.faker.name(),
"email": _account_id,
"phone_number": self.faker.phone_number(),
"area": self.faker.city(),
"is_admin": False,
}
response = self.client.post(
path=url, data=data, content_type="application/json", format="json",
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
def test_edit_account(self):
url = "/accounts/" + str(self.officer.id) + "/"
......@@ -113,6 +131,21 @@ class AccountViewTest(TestCase):
expected_returned_data = data
expected_returned_data["username"] = self.officer.user.username
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertJSONEqual(
str(response.content, encoding="utf8"), expected_returned_data
)
def test_edit_account_fail_without_complete_fields(self):
url = "/accounts/" + str(self.officer.id) + "/"
data = {
"id": str(self.officer.id),
"phone_number": self.faker.phone_number(),
"area": self.faker.city(),
}
response = self.client.put(
path=url, data=data, content_type="application/json", format="json",
)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
......@@ -5,7 +5,7 @@ from rest_framework.response import Response
from apps.accounts.models import Account
from apps.accounts.serializers import (
AccountSummarySerializer,
AccountSerializer,
AccountRegisterSerializer,
)
......@@ -15,12 +15,12 @@ class AccountViewSet(viewsets.ViewSet):
def list(self, request):
queryset = self.queryset
serializer = AccountSummarySerializer(queryset, many=True)
serializer = AccountSerializer(queryset, many=True)
return Response(serializer.data, status=status.HTTP_200_OK)
def retrieve(self, request, pk=None):
instance = get_object_or_404(self.queryset, pk=pk)
serializer = AccountSummarySerializer(instance)
serializer = AccountSerializer(instance)
return Response(serializer.data, status=status.HTTP_200_OK)
def create(self, request):
......@@ -34,12 +34,12 @@ class AccountViewSet(viewsets.ViewSet):
account = Account.objects.create(user=user, **serializer.validated_data)
return Response(
AccountSummarySerializer(account).data, status=status.HTTP_201_CREATED,
AccountSerializer(account).data, status=status.HTTP_201_CREATED,
)
def update(self, request, pk=None):
instance = get_object_or_404(self.queryset, pk=pk)
serializer = AccountSummarySerializer(instance, data=request.data)
serializer = AccountSerializer(instance, data=request.data)
serializer.is_valid(raise_exception=True)
serializer.save()
......
from django.contrib import admin
# Register your models here.
from django.apps import AppConfig
class CasesConfig(AppConfig):
name = "cases"
# 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
class Migration(migrations.Migration):
initial = True
dependencies = [
("accounts", "0001_initial"),
]
operations = [
migrations.CreateModel(
name="CaseSubject",
fields=[
(
"revision_id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("subject_id", models.UUIDField(default=uuid.uuid4)),
("name", models.CharField(max_length=128)),
("age", models.IntegerField()),
("is_male", models.BooleanField(db_index=True)),
("address", models.CharField(max_length=256)),
("district", models.CharField(max_length=128)),
("sub_district", models.CharField(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)),
],
options={
"verbose_name_plural": "case subjects",
"db_table": "case_subject",
"ordering": ["-created_at"],
},
),
migrations.CreateModel(
name="PositiveCase",
fields=[
(
"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)),
("outcome", models.CharField(max_length=256)),
("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="positive_case",
to="accounts.Account",
),
),
],
options={
"verbose_name_plural": "positive cases",
"db_table": "positive_case",
"ordering": ["-created_at"],
},
),
migrations.CreateModel(
name="MonitoringCase",
fields=[
(
"revision_id",
models.UUIDField(
default=uuid.uuid4,
editable=False,
primary_key=True,
serialize=False,
),
),
("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),
),
(
"is_checked",
models.BooleanField(blank=True, db_index=True, null=True),
),
("checking_date", models.DateField(blank=True, null=True)),
(
"is_regularly_treated",
models.BooleanField(blank=True, db_index=True, null=True),
),
("treatment_start_date", models.DateField(blank=True, null=True)),
("treatment_end_date", models.DateField(blank=True, null=True)),
("outcome", 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="monitoring_case",
to="accounts.Account",
),
),
],
options={
"verbose_name_plural": "monitoring cases",
"db_table": "monitoring_case",
"ordering": ["-created_at"],
},
),
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_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)),
(
"author",
models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.DO_NOTHING,
related_name="investigation_case",
to="accounts.Account",
),
),
],
options={
"verbose_name_plural": "investigation cases",
"db_table": "investigation_case",
"ordering": ["-created_at"],
},
),
]
import uuid
import pytz
from datetime import datetime
from django.core.exceptions import ValidationError
from django.db import models
from apps.accounts.models import Account
from apps.commons.managers import SoftObjectManager
class CaseSubject(models.Model):
revision_id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
subject_id = models.UUIDField(default=uuid.uuid4)
name = models.CharField(max_length=128)
age = models.IntegerField()
is_male = models.BooleanField(db_index=True)
address = models.CharField(max_length=256)
district = models.CharField(max_length=128)
sub_district = models.CharField(max_length=128)
deleted_at = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True, db_index=True)
objects = SoftObjectManager()
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.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_id = models.UUIDField(blank=True, null=True)
outcome = models.CharField(max_length=256)
author = models.ForeignKey(
Account,
blank=True,
null=True,
on_delete=models.DO_NOTHING,
related_name="positive_case",
)
deleted_at = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True, db_index=True)
objects = SoftObjectManager()
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.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_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="{}")
is_referral_needed = models.BooleanField(db_index=True)
medical_facility_reference = models.CharField(max_length=128, blank=True)
author = models.ForeignKey(
Account,
blank=True,
null=True,
on_delete=models.DO_NOTHING,
related_name="investigation_case",
)
deleted_at = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True, db_index=True)
objects = SoftObjectManager()
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.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_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)
is_regularly_treated = models.BooleanField(blank=True, null=True, db_index=True)
treatment_start_date = models.DateField(blank=True, null=True)
treatment_end_date = models.DateField(blank=True, null=True)
outcome = models.CharField(max_length=128, blank=True)
author = models.ForeignKey(
Account,
blank=True,
null=True,
on_delete=models.DO_NOTHING,
related_name="monitoring_case",
)
deleted_at = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True, db_index=True)
objects = SoftObjectManager()
class Meta:
db_table = "monitoring_case"
verbose_name_plural = "monitoring cases"
ordering = ["-created_at"]
def save(self, *args, **kwargs):
self.clean()
return super(MonitoringCase, self).save(*args, **kwargs)
def clean(self):
super(MonitoringCase, self).clean()
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)"
)
def delete(self):
if self.deleted_at is None:
self.is_active = False
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()
from rest_framework import serializers
from apps.cases.models import (
CaseSubject,
InvestigationCase,
MonitoringCase,
PositiveCase,
)
class CaseSubjectSerializer(serializers.ModelSerializer):
is_active = serializers.BooleanField(read_only=True)
class Meta:
model = CaseSubject
fields = [
"revision_id",
"subject_id",
"name",
"age",
"is_male",
"address",
"district",
"sub_district",
"is_active",
]
class PositiveCaseSerializer(serializers.ModelSerializer):
is_active = serializers.BooleanField(read_only=True)
class Meta:
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",
"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",