From 292347017cc797a3d118dd337321099983d36fbb Mon Sep 17 00:00:00 2001 From: Pranshu1902 Date: Tue, 5 Dec 2023 15:41:23 +0530 Subject: [PATCH 1/4] check if user is from same district --- care/facility/api/viewsets/patient_external_test.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/care/facility/api/viewsets/patient_external_test.py b/care/facility/api/viewsets/patient_external_test.py index 422d0bb512..b73aba80b7 100644 --- a/care/facility/api/viewsets/patient_external_test.py +++ b/care/facility/api/viewsets/patient_external_test.py @@ -139,6 +139,13 @@ def bulk_upsert(self, request, *args, **kwargs): # csv_file = request.FILES[list(request.FILES.keys())[0]] # csv_file.seek(0) # reader = csv.DictReader(io.StringIO(csv_file.read().decode("utf-8-sig"))) + + # check if the user is from same district + user = User.objects.filter(username=request.user).first() + for data in request.data["sample_tests"]: + if user.district != data["district"]: + raise ValidationError({"Error": "User must belong to same district"}) + if "sample_tests" not in request.data: raise ValidationError({"sample_tests": "No Data was provided"}) if not isinstance(request.data["sample_tests"], list): From aeeb7b111ae3f9571a4d919de5fe358b6ca7ddc5 Mon Sep 17 00:00:00 2001 From: Pranshu1902 Date: Tue, 5 Dec 2023 17:37:53 +0530 Subject: [PATCH 2/4] fix logic order --- care/facility/api/viewsets/patient_external_test.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/care/facility/api/viewsets/patient_external_test.py b/care/facility/api/viewsets/patient_external_test.py index b73aba80b7..79be3e1cf1 100644 --- a/care/facility/api/viewsets/patient_external_test.py +++ b/care/facility/api/viewsets/patient_external_test.py @@ -140,16 +140,17 @@ def bulk_upsert(self, request, *args, **kwargs): # csv_file.seek(0) # reader = csv.DictReader(io.StringIO(csv_file.read().decode("utf-8-sig"))) + if "sample_tests" not in request.data: + raise ValidationError({"sample_tests": "No Data was provided"}) + if not isinstance(request.data["sample_tests"], list): + raise ValidationError({"sample_tests": "Data should be provided as a list"}) + # check if the user is from same district user = User.objects.filter(username=request.user).first() for data in request.data["sample_tests"]: if user.district != data["district"]: raise ValidationError({"Error": "User must belong to same district"}) - if "sample_tests" not in request.data: - raise ValidationError({"sample_tests": "No Data was provided"}) - if not isinstance(request.data["sample_tests"], list): - raise ValidationError({"sample_tests": "Data should be provided as a list"}) errors = [] counter = 0 ser_objects = [] From 56820e065e1e921e011b63ff207d14bb1b844154 Mon Sep 17 00:00:00 2001 From: Pranshu1902 Date: Tue, 5 Dec 2023 17:38:49 +0530 Subject: [PATCH 3/4] refactor --- care/facility/api/viewsets/patient_external_test.py | 1 - 1 file changed, 1 deletion(-) diff --git a/care/facility/api/viewsets/patient_external_test.py b/care/facility/api/viewsets/patient_external_test.py index 79be3e1cf1..a95c05a082 100644 --- a/care/facility/api/viewsets/patient_external_test.py +++ b/care/facility/api/viewsets/patient_external_test.py @@ -139,7 +139,6 @@ def bulk_upsert(self, request, *args, **kwargs): # csv_file = request.FILES[list(request.FILES.keys())[0]] # csv_file.seek(0) # reader = csv.DictReader(io.StringIO(csv_file.read().decode("utf-8-sig"))) - if "sample_tests" not in request.data: raise ValidationError({"sample_tests": "No Data was provided"}) if not isinstance(request.data["sample_tests"], list): From ec7bbad48c01e93f1deaa7bd6ce2c005b16b2433 Mon Sep 17 00:00:00 2001 From: Pranshu Aggarwal <70687348+Pranshu1902@users.noreply.github.com> Date: Wed, 6 Dec 2023 17:14:30 +0530 Subject: [PATCH 4/4] refactor Co-authored-by: Aakash Singh --- care/facility/api/viewsets/patient_external_test.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/care/facility/api/viewsets/patient_external_test.py b/care/facility/api/viewsets/patient_external_test.py index a95c05a082..2118ef92f7 100644 --- a/care/facility/api/viewsets/patient_external_test.py +++ b/care/facility/api/viewsets/patient_external_test.py @@ -145,9 +145,8 @@ def bulk_upsert(self, request, *args, **kwargs): raise ValidationError({"sample_tests": "Data should be provided as a list"}) # check if the user is from same district - user = User.objects.filter(username=request.user).first() for data in request.data["sample_tests"]: - if user.district != data["district"]: + if request.user.district != data["district"]: raise ValidationError({"Error": "User must belong to same district"}) errors = []