From 456b9c45ee807d36c11cb2a6c38bbb4575abca8b Mon Sep 17 00:00:00 2001 From: Abhiuday Date: Sat, 9 Sep 2023 23:04:19 +0530 Subject: [PATCH] feat: added nurse role --- .../models/mixins/permissions/facility.py | 8 +- .../models/mixins/permissions/patient.py | 104 ++++++------------ care/users/api/serializers/user.py | 3 +- .../migrations/0009_alter_user_user_type.py | 47 ++++++++ care/users/models.py | 1 + 5 files changed, 85 insertions(+), 78 deletions(-) create mode 100644 care/users/migrations/0009_alter_user_user_type.py diff --git a/care/facility/models/mixins/permissions/facility.py b/care/facility/models/mixins/permissions/facility.py index 468463822f..edb2b1fab6 100644 --- a/care/facility/models/mixins/permissions/facility.py +++ b/care/facility/models/mixins/permissions/facility.py @@ -65,10 +65,10 @@ def has_object_read_permission(self, request): ) def has_object_write_permission(self, request): - if ( - request.user.user_type == User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StateReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StaffReadOnly"] + if request.user.user_type in ( + User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StateReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StaffReadOnly"], ): return False if request.user.user_type < User.TYPE_VALUE_MAP["Staff"]: # todo Temporary diff --git a/care/facility/models/mixins/permissions/patient.py b/care/facility/models/mixins/permissions/patient.py index f014b4f2a4..37abb7007c 100644 --- a/care/facility/models/mixins/permissions/patient.py +++ b/care/facility/models/mixins/permissions/patient.py @@ -7,10 +7,10 @@ class PatientPermissionMixin(BasePermissionMixin): def has_write_permission(request): if request.user.asset: return False - if ( - request.user.user_type == User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StateReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StaffReadOnly"] + if request.user.user_type in ( + User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StateReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StaffReadOnly"], ): return False return ( @@ -22,9 +22,9 @@ def has_write_permission(request): def has_object_read_permission(self, request): doctor_allowed = False if self.last_consultation: - doctor_allowed = ( - self.last_consultation.assigned_to == request.user - or request.user == self.assigned_to + doctor_allowed = request.user in ( + self.last_consultation.assigned_to, + self.assigned_to, ) return request.user.is_superuser or ( (hasattr(self, "created_by") and request.user == self.created_by) @@ -55,18 +55,18 @@ def has_object_read_permission(self, request): def has_object_write_permission(self, request): if request.user.asset: return False + if request.user.user_type in ( + User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StateReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StaffReadOnly"], + ): + return False doctor_allowed = False if self.last_consultation: - doctor_allowed = ( - self.last_consultation.assigned_to == request.user - or request.user == self.assigned_to + doctor_allowed = request.user in ( + self.last_consultation.assigned_to, + self.assigned_to, ) - if ( - request.user.user_type == User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StateReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StaffReadOnly"] - ): - return False return request.user.is_superuser or ( (hasattr(self, "created_by") and request.user == self.created_by) or (doctor_allowed) @@ -92,43 +92,7 @@ def has_object_write_permission(self, request): ) def has_object_update_permission(self, request): - if request.user.asset: - return False - doctor_allowed = False - if self.last_consultation: - doctor_allowed = ( - self.last_consultation.assigned_to == request.user - or request.user == self.assigned_to - ) - if ( - request.user.user_type == User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StateReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StaffReadOnly"] - ): - return False - return ( - request.user.is_superuser - or (hasattr(self, "created_by") and request.user == self.created_by) - or (self.facility and self.facility == request.user.home_facility) - or (doctor_allowed) - or ( - request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"] - and ( - request.user.district == self.district - or ( - self.facility - and request.user.district == self.facility.district - ) - ) - ) - or ( - request.user.user_type >= User.TYPE_VALUE_MAP["StateLabAdmin"] - and ( - request.user.state == self.state - or (self.facility and request.user.state == self.facility.state) - ) - ) - ) + return self.has_object_write_permission(request) def has_object_icmr_sample_permission(self, request): return self.has_object_read_permission(request) @@ -136,10 +100,10 @@ def has_object_icmr_sample_permission(self, request): def has_object_transfer_permission(self, request): if request.user.asset: return False - if ( - request.user.user_type == User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StateReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StaffReadOnly"] + if request.user.user_type in ( + User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StateReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StaffReadOnly"], ): return False new_facility = Facility.objects.filter( @@ -153,10 +117,10 @@ def has_object_transfer_permission(self, request): class PatientRelatedPermissionMixin(BasePermissionMixin): @staticmethod def has_write_permission(request): - if ( - request.user.user_type == User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StateReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StaffReadOnly"] + if request.user.user_type in ( + User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StateReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StaffReadOnly"], ): return False return ( @@ -172,10 +136,7 @@ def has_object_read_permission(self, request): self.patient.facility and request.user in self.patient.facility.users.all() ) - or ( - self.assigned_to == request.user - or request.user == self.patient.assigned_to - ) + or request.user in (self.assigned_to, self.patient.assigned_to) or ( request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"] and ( @@ -193,10 +154,10 @@ def has_object_read_permission(self, request): ) def has_object_update_permission(self, request): - if ( - request.user.user_type == User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StateReadOnlyAdmin"] - or request.user.user_type == User.TYPE_VALUE_MAP["StaffReadOnly"] + if request.user.user_type in ( + User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StateReadOnlyAdmin"], + User.TYPE_VALUE_MAP["StaffReadOnly"], ): return False return ( @@ -205,10 +166,7 @@ def has_object_update_permission(self, request): self.patient.facility and self.patient.facility == request.user.home_facility ) - or ( - self.assigned_to == request.user - or request.user == self.patient.assigned_to - ) + or request.user in (self.assigned_to, self.patient.assigned_to) or ( request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"] and ( diff --git a/care/users/api/serializers/user.py b/care/users/api/serializers/user.py index 0613b3437f..c7f2df0b7d 100644 --- a/care/users/api/serializers/user.py +++ b/care/users/api/serializers/user.py @@ -197,7 +197,8 @@ def validate(self, attrs): ) if ( - self.context["created_by"].user_type == User.TYPE_VALUE_MAP["Staff"] + self.context["created_by"].user_type + in (User.TYPE_VALUE_MAP["Staff"], User.TYPE_VALUE_MAP["Nurse"]) and validated["user_type"] == User.TYPE_VALUE_MAP["Doctor"] ): pass diff --git a/care/users/migrations/0009_alter_user_user_type.py b/care/users/migrations/0009_alter_user_user_type.py new file mode 100644 index 0000000000..b4fc82d886 --- /dev/null +++ b/care/users/migrations/0009_alter_user_user_type.py @@ -0,0 +1,47 @@ +# Generated by Django 4.2.2 on 2023-09-09 17:02 + +from django.db import migrations, models + + +def forwards_func(apps, schema_editor): + user_model = apps.get_model("users", "User") + user_list = [] + for user in user_model.objects.filter(user_type=10): + user.user_type = 12 + user_list.append(user) + + user_model.objects.bulk_update(user_list, ["user_type"]) + + +class Migration(migrations.Migration): + dependencies = [ + ("users", "0008_rename_skill_and_add_new_20230817_1937"), + ] + + operations = [ + migrations.AlterField( + model_name="user", + name="user_type", + field=models.IntegerField( + choices=[ + (2, "Transportation"), + (3, "Pharmacist"), + (5, "Volunteer"), + (9, "StaffReadOnly"), + (10, "Staff"), + (12, "Nurse"), + (15, "Doctor"), + (20, "Reserved"), + (21, "WardAdmin"), + (23, "LocalBodyAdmin"), + (25, "DistrictLabAdmin"), + (29, "DistrictReadOnlyAdmin"), + (30, "DistrictAdmin"), + (35, "StateLabAdmin"), + (39, "StateReadOnlyAdmin"), + (40, "StateAdmin"), + ] + ), + ), + migrations.RunPython(forwards_func), + ] diff --git a/care/users/models.py b/care/users/models.py index 571960fcfe..2efa8c0762 100644 --- a/care/users/models.py +++ b/care/users/models.py @@ -185,6 +185,7 @@ class User(AbstractUser): "Volunteer": 5, "StaffReadOnly": 9, "Staff": 10, + "Nurse": 12, "Doctor": 15, "Reserved": 20, "WardAdmin": 21,