Skip to content

Commit

Permalink
fix: added NurseReadOnly user type
Browse files Browse the repository at this point in the history
  • Loading branch information
aeswibon committed Sep 10, 2023
1 parent 456b9c4 commit 574db3f
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 105 deletions.
3 changes: 2 additions & 1 deletion care/facility/models/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
from care.utils.models.base import BaseModel

READ_ONLY_USER_TYPES = [
User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"],
User.TYPE_VALUE_MAP["StateReadOnlyAdmin"],
User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"],
User.TYPE_VALUE_MAP["NurseReadOnly"],
User.TYPE_VALUE_MAP["StaffReadOnly"],
]

Expand Down
14 changes: 3 additions & 11 deletions care/facility/models/daily_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
COVID_CATEGORY_CHOICES,
PatientBaseModel,
)
from care.facility.models.base import covert_choice_dict
from care.facility.models.base import READ_ONLY_USER_TYPES, covert_choice_dict
from care.facility.models.bed import AssetBed
from care.facility.models.json_schema.daily_round import (
BLOOD_PRESSURE,
Expand Down Expand Up @@ -499,11 +499,7 @@ def save(self, *args, **kwargs):
@staticmethod
def has_write_permission(request):
if "/analyse" not in request.get_full_path():
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 READ_ONLY_USER_TYPES:
return False
return DailyRound.has_read_permission(request)

Expand Down Expand Up @@ -559,11 +555,7 @@ 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 READ_ONLY_USER_TYPES:
return False
return (
request.user.is_superuser
Expand Down
8 changes: 2 additions & 6 deletions care/facility/models/mixins/permissions/asset.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from dry_rest_permissions.generics import DRYPermissions

from care.facility.models.base import READ_ONLY_USER_TYPES
from care.facility.models.mixins.permissions.base import BasePermissionMixin
from care.users.models import User


class IsAssetUser:
Expand Down Expand Up @@ -30,11 +30,7 @@ def has_object_read_permission(self, request):
return True

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 READ_ONLY_USER_TYPES:
return False

return True
Expand Down
19 changes: 4 additions & 15 deletions care/facility/models/mixins/permissions/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from care.facility.models.base import READ_ONLY_USER_TYPES
from care.users.models import User


Expand All @@ -8,11 +9,7 @@ def has_read_permission(request):

@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 READ_ONLY_USER_TYPES:
return False
return (
request.user.is_superuser
Expand All @@ -36,11 +33,7 @@ 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 READ_ONLY_USER_TYPES:
return False
return (request.user.is_superuser) or (
(hasattr(self, "created_by") and request.user == self.created_by)
Expand All @@ -57,11 +50,7 @@ def has_object_update_permission(self, request):
)

def has_object_destroy_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 READ_ONLY_USER_TYPES:
return False
return request.user.is_superuser or (
hasattr(self, "created_by") and request.user == self.created_by
Expand Down
19 changes: 4 additions & 15 deletions care/facility/models/mixins/permissions/facility.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from care.facility.models.base import READ_ONLY_USER_TYPES
from care.facility.models.mixins.permissions.base import BasePermissionMixin
from care.users.models import User

Expand Down Expand Up @@ -65,11 +66,7 @@ def has_object_read_permission(self, request):
)

def has_object_write_permission(self, request):
if request.user.user_type in (
User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"],
User.TYPE_VALUE_MAP["StateReadOnlyAdmin"],
User.TYPE_VALUE_MAP["StaffReadOnly"],
):
if request.user.user_type in READ_ONLY_USER_TYPES:
return False
if request.user.user_type < User.TYPE_VALUE_MAP["Staff"]: # todo Temporary
return False
Expand All @@ -92,11 +89,7 @@ class FacilityRelatedPermissionMixin(BasePermissionMixin):
def has_write_permission(request):
from care.facility.models.facility import Facility

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 READ_ONLY_USER_TYPES:
return False

facility = False
Expand Down Expand Up @@ -129,11 +122,7 @@ 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 READ_ONLY_USER_TYPES:
return False
return (
super().has_write_permission(request)
Expand Down
31 changes: 6 additions & 25 deletions care/facility/models/mixins/permissions/patient.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from care.facility.models import Facility, User
from care.facility.models.base import READ_ONLY_USER_TYPES
from care.facility.models.mixins.permissions.base import BasePermissionMixin


Expand All @@ -7,11 +8,7 @@ class PatientPermissionMixin(BasePermissionMixin):
def has_write_permission(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"],
):
if request.user.user_type in READ_ONLY_USER_TYPES:
return False
return (
request.user.is_superuser
Expand Down Expand Up @@ -55,11 +52,7 @@ 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"],
):
if request.user.user_type in READ_ONLY_USER_TYPES:
return False
doctor_allowed = False
if self.last_consultation:
Expand Down Expand Up @@ -100,11 +93,7 @@ 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 in (
User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"],
User.TYPE_VALUE_MAP["StateReadOnlyAdmin"],
User.TYPE_VALUE_MAP["StaffReadOnly"],
):
if request.user.user_type in READ_ONLY_USER_TYPES:
return False
new_facility = Facility.objects.filter(
id=request.data.get("facility", None)
Expand All @@ -117,11 +106,7 @@ def has_object_transfer_permission(self, request):
class PatientRelatedPermissionMixin(BasePermissionMixin):
@staticmethod
def has_write_permission(request):
if request.user.user_type in (
User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"],
User.TYPE_VALUE_MAP["StateReadOnlyAdmin"],
User.TYPE_VALUE_MAP["StaffReadOnly"],
):
if request.user.user_type in READ_ONLY_USER_TYPES:
return False
return (
request.user.is_superuser
Expand Down Expand Up @@ -154,11 +139,7 @@ def has_object_read_permission(self, request):
)

def has_object_update_permission(self, request):
if request.user.user_type in (
User.TYPE_VALUE_MAP["DistrictReadOnlyAdmin"],
User.TYPE_VALUE_MAP["StateReadOnlyAdmin"],
User.TYPE_VALUE_MAP["StaffReadOnly"],
):
if request.user.user_type in READ_ONLY_USER_TYPES:
return False
return (
request.user.is_superuser
Expand Down
13 changes: 3 additions & 10 deletions care/facility/models/patient_sample.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from django.db import models

from care.facility.models import FacilityBaseModel, PatientRegistration, reverse_choices
from care.facility.models.base import READ_ONLY_USER_TYPES
from care.users.models import User

SAMPLE_TYPE_CHOICES = [
Expand Down Expand Up @@ -152,11 +153,7 @@ def flow(self):

@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 READ_ONLY_USER_TYPES:
return False
return (
request.user.is_superuser
Expand Down Expand Up @@ -190,11 +187,7 @@ 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 READ_ONLY_USER_TYPES:
return False
if not self.has_object_read_permission(request):
return False
Expand Down
7 changes: 2 additions & 5 deletions care/facility/models/prescription_supplier.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from django.db import models

from care.facility.models import FacilityBaseModel
from care.facility.models.base import READ_ONLY_USER_TYPES
from care.users.models import User


Expand Down Expand Up @@ -59,10 +60,6 @@ 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 READ_ONLY_USER_TYPES:
return False
return self.has_object_read_permission(request)
18 changes: 4 additions & 14 deletions care/users/migrations/0009_alter_user_user_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,6 @@
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"),
Expand All @@ -27,9 +17,10 @@ class Migration(migrations.Migration):
(2, "Transportation"),
(3, "Pharmacist"),
(5, "Volunteer"),
(9, "StaffReadOnly"),
(10, "Staff"),
(12, "Nurse"),
(7, "StaffReadOnly"),
(8, "Staff"),
(9, "NurseReadOnly"),
(10, "Nurse"),
(15, "Doctor"),
(20, "Reserved"),
(21, "WardAdmin"),
Expand All @@ -43,5 +34,4 @@ class Migration(migrations.Migration):
]
),
),
migrations.RunPython(forwards_func),
]
7 changes: 4 additions & 3 deletions care/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ class User(AbstractUser):
"Transportation": 2,
"Pharmacist": 3,
"Volunteer": 5,
"StaffReadOnly": 9,
"Staff": 10,
"Nurse": 12,
"StaffReadOnly": 7,
"Staff": 8,
"NurseReadOnly": 9,
"Nurse": 10,
"Doctor": 15,
"Reserved": 20,
"WardAdmin": 21,
Expand Down

0 comments on commit 574db3f

Please sign in to comment.