diff --git a/apps/accounts/migrations/0005_auto_20200515_0038.py b/apps/accounts/migrations/0005_auto_20200515_0038.py index 06ad9767e71301cf01e9ff4119c9b385cc340295..744e184138c28947144a469d74d6bf33b5860833 100644 --- a/apps/accounts/migrations/0005_auto_20200515_0038.py +++ b/apps/accounts/migrations/0005_auto_20200515_0038.py @@ -14,6 +14,11 @@ class Migration(migrations.Migration): migrations.AlterField( model_name='account', name='phone_number', - field=models.CharField(max_length=64, validators=[django.core.validators.RegexValidator(message='Phone number has 9-15 digits, allowed to have + prefix', regex='^\\+?\\d{9,15}$')]), + field=models.CharField( + max_length=64, + validators=[ + django.core.validators.RegexValidator( + message='Phone number has 9-15 digits, allowed to have + prefix', + regex='^\\+?\\d{9,15}$')]), ), ] diff --git a/apps/cases/filters.py b/apps/cases/filters.py index 4fc53df483f81487c91741e64419c4e9430a369d..3b1d2c346dd16b84fe182c56489189a281d67c08 100644 --- a/apps/cases/filters.py +++ b/apps/cases/filters.py @@ -45,12 +45,14 @@ INVESTIGATION_CASE_FILTERSET_FIELDS = ( "reference_case__is_referral_needed", "reference_case__medical_facility_reference", "reference_case__outcome", + "reference_case__is_positive", "case_relation", "medical_symptoms", "risk_factors", "is_referral_needed", "medical_facility_reference", "outcome", + "is_positive", "author__name", "created_at", ) @@ -67,12 +69,14 @@ INVESTIGATION_CASE_SEARCH_FIELDS = ( "reference_case__risk_factors", "reference_case__medical_facility_reference", "reference_case__outcome", + "reference_case__is_positive", "reference_case__created_at", "case_relation", "medical_symptoms", "risk_factors", "medical_facility_reference", "outcome", + "is_positive", "author__name", ) @@ -90,6 +94,7 @@ INVESTIGATION_CASE_ORDERING_FIELDS = ( "reference_case__is_referral_needed", "reference_case__medical_facility_reference", "reference_case__outcome", + "reference_case__is_positive", "reference_case__created_at", "case_relation", "medical_symptoms", @@ -97,6 +102,7 @@ INVESTIGATION_CASE_ORDERING_FIELDS = ( "is_referral_needed", "medical_facility_reference", "outcome", + "is_positive", "author__name", "created_at", ) @@ -115,6 +121,7 @@ MONITORING_CASE_FILTERSET_FIELDS = ( "investigation_case__reference_case__is_referral_needed", "investigation_case__reference_case__medical_facility_reference", "investigation_case__reference_case__outcome", + "investigation_case__reference_case__is_positive", "is_referred", "is_checked", "is_medicated", @@ -138,6 +145,7 @@ MONITORING_CASE_SEARCH_FIELDS = ( "investigation_case__reference_case__risk_factors", "investigation_case__reference_case__medical_facility_reference", "investigation_case__reference_case__outcome", + "investigation_case__reference_case__is_positive", "investigation_case__reference_case__created_at", "is_referred", "is_checked", @@ -163,6 +171,7 @@ MONITORING_CASE_ORDERING_FIELDS = ( "investigation_case__reference_case__is_referral_needed", "investigation_case__reference_case__medical_facility_reference", "investigation_case__reference_case__outcome", + "investigation_case__reference_case__is_positive", "investigation_case__reference_case__created_at", "is_referred", "is_checked", diff --git a/apps/cases/migrations/0012_auto_20200518_1532.py b/apps/cases/migrations/0012_auto_20200518_1532.py new file mode 100644 index 0000000000000000000000000000000000000000..2625d81c4c501995bb77ecf67c77ac78586b6936 --- /dev/null +++ b/apps/cases/migrations/0012_auto_20200518_1532.py @@ -0,0 +1,23 @@ +# Generated by Django 3.0.1 on 2020-05-18 08:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('cases', '0011_monitoring_checking_date'), + ] + + operations = [ + migrations.AddField( + model_name='investigationcase', + name='is_positive', + field=models.BooleanField(db_index=True, null=True), + ), + migrations.AddField( + model_name='investigationcasehistory', + name='is_positive', + field=models.BooleanField(db_index=True, null=True), + ), + ] diff --git a/apps/cases/models.py b/apps/cases/models.py index c045a522616989cfdafbd02a72cfe770de83e3de..804e029762d01ede020abf8c3f6415f1d1a1e10b 100644 --- a/apps/cases/models.py +++ b/apps/cases/models.py @@ -79,6 +79,7 @@ class InvestigationCaseHistory(HistoryModel): is_referral_needed = models.BooleanField(db_index=True) medical_facility_reference = models.CharField(max_length=128, blank=True) outcome = models.CharField(max_length=256, blank=True, null=True) + is_positive = models.BooleanField(db_index=True, null=True) author = models.ForeignKey( Account, blank=True, @@ -129,6 +130,7 @@ class InvestigationCase(HistoryEnabledModel): is_referral_needed = models.BooleanField(db_index=True) medical_facility_reference = models.CharField(max_length=128, blank=True) outcome = models.CharField(max_length=256, blank=True, null=True) + is_positive = models.BooleanField(db_index=True, null=True) author = models.ForeignKey( Account, blank=True, diff --git a/apps/cases/serializers.py b/apps/cases/serializers.py index 038cd46e4924c15e887af46feb6fc66d918af937..610ce15b329bd703af18390004b574d10d7bd5db 100644 --- a/apps/cases/serializers.py +++ b/apps/cases/serializers.py @@ -63,6 +63,7 @@ class InvestigationCaseSummarySerializer(serializers.ModelSerializer): "is_referral_needed", "medical_facility_reference", "outcome", + "is_positive", "author", "created_at", ] @@ -88,7 +89,13 @@ class InvestigationCaseSerializer(serializers.ModelSerializer): def save(self): account = self.context.get('request').user.account - super(InvestigationCaseSerializer, self).save(author=account) + + outcome = self.validated_data.get('outcome', None) + is_positive = None + if outcome is not None: + is_positive = True if "+" in outcome else False + + super(InvestigationCaseSerializer, self).save(author=account, is_positive=is_positive) def to_representation(self, instance): serializer = InvestigationCaseSummarySerializer(instance) diff --git a/apps/cases/tests/test_units/test_investigation_cases.py b/apps/cases/tests/test_units/test_investigation_cases.py index dbf8ff41197a788d7c24886415c4ffaa2367f769..5459f8520aa7702827bd66e25daeac2ff0bf3e5d 100644 --- a/apps/cases/tests/test_units/test_investigation_cases.py +++ b/apps/cases/tests/test_units/test_investigation_cases.py @@ -34,6 +34,7 @@ class InvestigationCaseViewTest(APITestCase): case_subject=cls.case_subject, author=cls.author, outcome="BTA+", + is_positive=True, ) cls.case = InvestigationCaseFactory( author=cls.author, @@ -209,6 +210,40 @@ class InvestigationCaseViewTest(APITestCase): investigation_case_history_current_count, investigation_case_history_prev_count + 1 ) + def test_edit_investigation_to_positive_success(self): + url = self.BASE_URL + str(self.case.id) + "/" + + data = { + "case_subject": str(self.case_subject.id), + "reference_case": str(self.reference_case.id), + "case_relation": self.case.case_relation, + "medical_symptoms": self.case.medical_symptoms, + "risk_factors": self.case.risk_factors, + "is_referral_needed": not self.case.is_referral_needed, + "medical_facility_reference": self.case.medical_facility_reference, + "outcome": "BTA+", + } + + investigation_case_prev_count = InvestigationCase.objects.all().count() + positive_case_prev_count = InvestigationCase.objects.filter(is_positive=True).count() + + response = self.client.put(path=url, data=data, format="json",) + self.assertEqual(response.status_code, status.HTTP_200_OK) + self.assertEqual( + response.data["is_referral_needed"], not self.case.is_referral_needed + ) + + investigation_case_current_count = InvestigationCase.objects.all().count() + positive_case_current_count = InvestigationCase.objects.filter(is_positive=True).count() + + self.assertEqual( + investigation_case_current_count, investigation_case_prev_count + ) + + self.assertEqual( + positive_case_current_count, positive_case_prev_count + 1 + ) + def test_edit_investigation_case_fails_with_incomplete_data(self): url = self.BASE_URL + str(self.case.id) + "/" diff --git a/apps/cases/views.py b/apps/cases/views.py index 541c12d1aee1c6463257cfb7a961b777ff9cf44d..aa5179f07459fd6f99f50a51fe4182d7d9d5eedc 100644 --- a/apps/cases/views.py +++ b/apps/cases/views.py @@ -71,7 +71,7 @@ class InvestigationCaseViewSet(viewsets.ModelViewSet): query_include_positive = self.request.query_params.get('include_positive', None) if query_include_positive is not None: return self.queryset - return self.queryset.filter(Q(outcome__isnull=True) | Q(outcome__icontains="-")) + return self.queryset.exclude(is_positive=True) return self.queryset def get_serializer_class(self): @@ -85,8 +85,7 @@ class InvestigationCaseViewSet(viewsets.ModelViewSet): class InvestigationPositiveCaseAPIView(ListAPIView): serializer_class = InvestigationCaseSummarySerializer - queryset = InvestigationCase.objects.filter( - outcome__isnull=False).exclude(outcome__icontains="-") + queryset = InvestigationCase.objects.filter(is_positive=True) filter_backends = (DjangoFilterBackend, SearchFilter, OrderingFilter) permission_classes = (IsAuthenticated,) pagination_class = PageNumberPagination