Skip to content

Commit

Permalink
lint care/facility/models/
Browse files Browse the repository at this point in the history
  • Loading branch information
sainak committed Sep 24, 2024
1 parent 9214baa commit a7c7927
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 157 deletions.
2 changes: 1 addition & 1 deletion care/facility/api/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class MedicalHistorySerializer(serializers.Serializer):
last_edited = UserBaseMinimumSerializer(read_only=True)
created_by = UserBaseMinimumSerializer(read_only=True)
vaccine_name = serializers.ChoiceField(
choices=PatientRegistration.vaccineChoices, required=False, allow_null=True
choices=PatientRegistration.VaccineChoices, required=False, allow_null=True
)

assigned_to_object = UserBaseMinimumSerializer(source="assigned_to", read_only=True)
Expand Down
13 changes: 0 additions & 13 deletions care/facility/models/ambulance.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,6 @@ class Ambulance(FacilityBaseModel):
)
owner_is_smart_phone = models.BooleanField(default=True)

# primary_district = models.IntegerField(choices=DISTRICT_CHOICES, blank=False)
# secondary_district = models.IntegerField(choices=DISTRICT_CHOICES, blank=True, null=True)
# third_district = models.IntegerField(choices=DISTRICT_CHOICES, blank=True, null=True)

primary_district = models.ForeignKey(
District,
on_delete=models.PROTECT,
Expand Down Expand Up @@ -122,15 +118,6 @@ def has_object_update_permission(self, request):
)
)

# class Meta:
# constraints = [
# models.CheckConstraint(
# name="ambulance_free_or_price",
# check=models.Q(price_per_km__isnull=False)
# | models.Q(has_free_service=True),
# )
# ]


class AmbulanceDriver(FacilityBaseModel):
ambulance = models.ForeignKey(Ambulance, on_delete=models.CASCADE)
Expand Down
6 changes: 5 additions & 1 deletion care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class AssetType(enum.Enum):

AssetTypeChoices = [(e.value, e.name) for e in AssetType]

AssetClassChoices = [(e.name, e.value._name) for e in AssetClasses]
AssetClassChoices = [(e.name, e.value._name) for e in AssetClasses] # noqa: SLF001


class Status(enum.Enum):
Expand Down Expand Up @@ -162,6 +162,7 @@ def resolved_middleware(self):
"hostname": hostname,
"source": "facility",
}
return None

class Meta:
constraints = [
Expand Down Expand Up @@ -312,3 +313,6 @@ class AssetServiceEdit(models.Model):

class Meta:
ordering = ["-edited_on"]

def __str__(self):
return f"{self.asset_service.asset.name} - {self.serviced_on}"
13 changes: 8 additions & 5 deletions care/facility/models/daily_round.py
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ def save(self, *args, **kwargs):
if self.output is not None:
self.total_output_calculated = sum([x["quantity"] for x in self.output])

super(DailyRound, self).save(*args, **kwargs)
super().save(*args, **kwargs)

@staticmethod
def has_read_permission(request):
Expand All @@ -585,8 +585,8 @@ def has_read_permission(request):
return request.user.is_superuser or (
(request.user in consultation.patient.facility.users.all())
or (
request.user == consultation.assigned_to
or request.user == consultation.patient.assigned_to
request.user
in (consultation.assigned_to, consultation.patient.assigned_to)
)
or (
request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]
Expand Down Expand Up @@ -620,8 +620,11 @@ def has_object_read_permission(self, request):
and request.user in self.consultation.patient.facility.users.all()
)
or (
self.consultation.assigned_to == request.user
or request.user == self.consultation.patient.assigned_to
request.user
in (
self.consultation.assigned_to,
self.consultation.patient.assigned_to,
)
)
or (
request.user.user_type >= User.TYPE_VALUE_MAP["DistrictLabAdmin"]
Expand Down
3 changes: 2 additions & 1 deletion care/facility/models/encounter_symptom.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ class EncounterSymptom(BaseModel, ConsultationRelatedPermissionMixin):

def save(self, *args, **kwargs):
if self.other_symptom and self.symptom != Symptom.OTHERS:
raise ValueError("Other Symptom should be empty when Symptom is not OTHERS")
msg = "Other Symptom should be empty when Symptom is not OTHERS"
raise ValueError(msg)

if self.clinical_impression_status != ClinicalImpressionStatus.ENTERED_IN_ERROR:
if self.onset_date and self.cure_date:
Expand Down
27 changes: 10 additions & 17 deletions care/facility/models/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,19 @@ class EventType(models.Model):
created_date = models.DateTimeField(auto_now_add=True)
is_active = models.BooleanField(default=True)

def get_descendants(self):
descendants = list(self.children.all())
for child in self.children.all():
descendants.extend(child.get_descendants())
return descendants
def __str__(self) -> str:
return f"{self.model} - {self.name}"

def save(self, *args, **kwargs):
if self.description is not None and not self.description.strip():
self.description = None
return super().save(*args, **kwargs)

def __str__(self) -> str:
return f"{self.model} - {self.name}"
def get_descendants(self):
descendants = list(self.children.all())
for child in self.children.all():
descendants.extend(child.get_descendants())
return descendants


class PatientConsultationEvent(models.Model):
Expand All @@ -68,16 +68,9 @@ class PatientConsultationEvent(models.Model):
max_length=10, choices=ChangeType, default=ChangeType.CREATED
)

def __str__(self) -> str:
return f"{self.id} - {self.consultation_id} - {self.event_type} - {self.change_type}"

class Meta:
ordering = ["-created_date"]
indexes = [models.Index(fields=["consultation", "is_latest"])]
# constraints = [
# models.UniqueConstraint(
# fields=["consultation", "event_type", "is_latest"],
# condition=models.Q(is_latest=True),
# name="unique_consultation_event_type_is_latest",
# )
# ]

def __str__(self) -> str:
return f"{self.id} - {self.consultation_id} - {self.event_type} - {self.change_type}"
17 changes: 9 additions & 8 deletions care/facility/models/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,27 +90,27 @@ class FacilityFeature(models.IntegerChoices):
(5, "Hotel"),
(6, "Lodge"),
(7, "TeleMedicine"),
# (8, "Govt Hospital"), # Change from "Govt Hospital" to "Govt Medical College Hospitals"
# 8, "Govt Hospital" # Change from "Govt Hospital" to "Govt Medical College Hospitals"
(9, "Govt Labs"),
(10, "Private Labs"),
# Use 8xx for Govt owned hospitals and health centres
(800, "Primary Health Centres"),
# (801, "24x7 Public Health Centres"), # Change from "24x7 Public Health Centres" to "Primary Health Centres"
# 801, "24x7 Public Health Centres" # Change from "24x7 Public Health Centres" to "Primary Health Centres"
(802, "Family Health Centres"),
(803, "Community Health Centres"),
# (820, "Urban Primary Health Center"), # Change from "Urban Primary Health Center" to "Primary Health Centres"
# 820, "Urban Primary Health Center" # Change from "Urban Primary Health Center" to "Primary Health Centres"
(830, "Taluk Hospitals"),
# (831, "Taluk Headquarters Hospitals"), # Change from "Taluk Headquarters Hospitals" to "Taluk Hospitals"
# 831, "Taluk Headquarters Hospitals" # Change from "Taluk Headquarters Hospitals" to "Taluk Hospitals"
(840, "Women and Child Health Centres"),
# (850, "General hospitals"), # Change from "General hospitals" to "District Hospitals"
# 850, "General hospitals" # Change from "General hospitals" to "District Hospitals"
(860, "District Hospitals"),
(870, "Govt Medical College Hospitals"),
(900, "Co-operative hospitals"),
(910, "Autonomous healthcare facility"),
# Use 9xx for Labs
# (950, "Corona Testing Labs"), # Change from "Corona Testing Labs" to "Govt Labs"
# 950, "Corona Testing Labs" # Change from "Corona Testing Labs" to "Govt Labs"
# Use 10xx for Corona Care Center
# (1000, "Corona Care Centre"), # Change from "Corona Care Centre" to "Other"
# 1000, "Corona Care Centre" # Change from "Corona Care Centre" to "Other"
(1010, "COVID-19 Domiciliary Care Center"),
# Use 11xx for First Line Treatment Centre
(1100, "First Line Treatment Centre"),
Expand Down Expand Up @@ -207,7 +207,8 @@ def check_if_spoke_is_not_ancestor(base_id: int, spoke_id: int):
"hub_id", flat=True
)
if spoke_id in ancestors_of_base:
raise serializers.ValidationError("This facility is already an ancestor hub")
msg = "This facility is already an ancestor hub"
raise serializers.ValidationError(msg)
for ancestor in ancestors_of_base:
check_if_spoke_is_not_ancestor(ancestor, spoke_id)

Expand Down
22 changes: 10 additions & 12 deletions care/facility/models/file_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ class FileCategory(models.TextChoices):
class Meta:
abstract = True

def delete(self, *args):
self.deleted = True
self.save(update_fields=["deleted"])

def save(self, *args, **kwargs):
if "force_insert" in kwargs or (not self.internal_name):
internal_name = str(uuid4()) + str(int(time.time()))
Expand All @@ -60,14 +56,18 @@ def save(self, *args, **kwargs):
self.internal_name = internal_name
return super().save(*args, **kwargs)

def delete(self, *args):
self.deleted = True
self.save(update_fields=["deleted"])

def get_extension(self):
parts = self.internal_name.split(".")
return f".{parts[-1]}" if len(parts) > 1 else ""

def signed_url(
self, duration=60 * 60, mime_type=None, bucket_type=BucketType.PATIENT
):
config, bucket_name = get_client_config(bucket_type, True)
config, bucket_name = get_client_config(bucket_type, external=True)
s3 = boto3.client("s3", **config)
params = {
"Bucket": bucket_name,
Expand All @@ -82,7 +82,7 @@ def signed_url(
)

def read_signed_url(self, duration=60 * 60, bucket_type=BucketType.PATIENT):
config, bucket_name = get_client_config(bucket_type, True)
config, bucket_name = get_client_config(bucket_type, external=True)
s3 = boto3.client("s3", **config)
return s3.generate_presigned_url(
"get_object",
Expand Down Expand Up @@ -128,8 +128,6 @@ class FileUpload(BaseFileUpload):
all data will be private and file access will be given on a NEED TO BASIS ONLY
"""

# TODO : Periodic tasks that removes files that were never uploaded

class FileType(models.IntegerChoices):
OTHER = 0, "OTHER"
PATIENT = 1, "PATIENT"
Expand Down Expand Up @@ -164,6 +162,9 @@ class FileType(models.IntegerChoices):
FileTypeChoices = [(x.value, x.name) for x in FileType]
FileCategoryChoices = [(x.value, x.name) for x in BaseFileUpload.FileCategory]

def __str__(self):
return f"{self.FileTypeChoices[self.file_type][1]} - {self.name}{' (Archived)' if self.is_archived else ''}"

def save(self, *args, **kwargs):
from care.facility.models import PatientConsent

Expand Down Expand Up @@ -192,7 +193,7 @@ def save(self, *args, **kwargs):
).exclude(pk=self.pk if self.is_archived else None)
)
if not new_consent
else models.Value(True)
else models.Value(value=True)
)
)
.filter(has_files=True)
Expand All @@ -203,6 +204,3 @@ def save(self, *args, **kwargs):
consultation.save()

return super().save(*args, **kwargs)

def __str__(self):
return f"{self.FileTypeChoices[self.file_type][1]} - {self.name}{' (Archived)' if self.is_archived else ''}"
Empty file.
18 changes: 9 additions & 9 deletions care/facility/models/mixins/permissions/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ 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)
Expand Down Expand Up @@ -60,9 +60,9 @@ def has_object_write_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 (
Expand Down Expand Up @@ -99,9 +99,9 @@ def has_object_update_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 (
Expand Down
Loading

0 comments on commit a7c7927

Please sign in to comment.