diff --git a/care/facility/models/asset.py b/care/facility/models/asset.py index fdeaff4263..9b548a6da1 100644 --- a/care/facility/models/asset.py +++ b/care/facility/models/asset.py @@ -45,6 +45,12 @@ class RoomType(enum.Enum): Facility, on_delete=models.PROTECT, null=False, blank=False ) + def delete(self, *args, **kwargs): + UserDefaultAssetLocation.objects.filter(location=self).update(deleted=True) + FacilityDefaultAssetLocation.objects.filter(location=self).update(deleted=True) + + super().delete(*args, **kwargs) + class Asset(BaseModel): class AssetType(enum.Enum): diff --git a/care/facility/models/bed.py b/care/facility/models/bed.py index 3f055db8a8..5214a4ecb7 100644 --- a/care/facility/models/bed.py +++ b/care/facility/models/bed.py @@ -53,6 +53,7 @@ def save(self, *args, **kwargs) -> None: def delete(self, *args, **kwargs) -> None: AssetBed.objects.filter(bed=self).update(deleted=True) + ConsultationBed.objects.filter(bed=self).update(deleted=True) super().delete(*args, **kwargs) diff --git a/care/facility/models/facility.py b/care/facility/models/facility.py index 327a310ee7..97340b1070 100644 --- a/care/facility/models/facility.py +++ b/care/facility/models/facility.py @@ -198,6 +198,17 @@ def save(self, *args, **kwargs) -> None: facility=self, user=self.created_by, created_by=self.created_by ) + def delete(self, *args, **kwargs): + from care.facility.models import FacilityDefaultAssetLocation, PatientSample + + FacilityDefaultAssetLocation.objects.filter(facility=self).update(deleted=True) + FacilityUser.objects.filter(facility=self).update(deleted=True) + PatientSample.objects.filter(testing_facility=self).update( + testing_facility=None + ) + + super().delete(*args, **kwargs) + CSV_MAPPING = { "name": "Facility Name", "facility_type": "Facility Type", diff --git a/care/facility/models/patient.py b/care/facility/models/patient.py index 36fa2c9c62..eb01ebddc0 100644 --- a/care/facility/models/patient.py +++ b/care/facility/models/patient.py @@ -476,6 +476,13 @@ def save(self, *args, **kwargs) -> None: self._alias_recovery_to_recovered() super().save(*args, **kwargs) + def delete(self, *args, **kwargs): + from care.facility.models import PatientSample + + PatientSample.objects.filter(patient=self).update(deleted=True) + + super().delete(*args, **kwargs) + CSV_MAPPING = { # Patient Details "external_id": "Patient ID", diff --git a/care/facility/models/patient_consultation.py b/care/facility/models/patient_consultation.py index de097a16ce..e1a76f3e4c 100644 --- a/care/facility/models/patient_consultation.py +++ b/care/facility/models/patient_consultation.py @@ -238,6 +238,19 @@ def save(self, *args, **kwargs): """ super(PatientConsultation, self).save(*args, **kwargs) + def delete(self, *args, **kwargs): + from care.facility.models import ( + ConsultationBed, + InvestigationValue, + PatientSample, + ) + + ConsultationBed.objects.filter(consultation=self).update(deleted=True) + InvestigationValue.objects.filter(consultation=self).update(deleted=True) + PatientSample.objects.filter(consultation=self).update(deleted=True) + + super().delete(*args, **kwargs) + class Meta: constraints = [ models.CheckConstraint( diff --git a/care/facility/models/patient_investigation.py b/care/facility/models/patient_investigation.py index 9e94e23153..897f331ce3 100644 --- a/care/facility/models/patient_investigation.py +++ b/care/facility/models/patient_investigation.py @@ -15,6 +15,11 @@ class PatientInvestigationGroup(BaseModel): def __str__(self) -> str: return self.name + def delete(self, *args, **kwargs): + InvestigationValue.objects.filter(group=self).update(deleted=True) + + super().delete(*args, **kwargs) + class PatientInvestigation(BaseModel): name = models.CharField(max_length=500, blank=False, null=False) @@ -31,6 +36,11 @@ class PatientInvestigation(BaseModel): def __str__(self) -> str: return self.name + " in " + self.unit + " as " + self.investigation_type + def delete(self, *args, **kwargs): + InvestigationValue.objects.filter(investigation=self).update(deleted=True) + + super().delete(*args, **kwargs) + class InvestigationSession(BaseModel): external_id = models.UUIDField( @@ -49,6 +59,11 @@ class Meta: ), ] + def delete(self, *args, **kwargs): + InvestigationValue.objects.filter(session=self).update(deleted=True) + + super().delete(*args, **kwargs) + class InvestigationValue(BaseModel): investigation = models.ForeignKey( diff --git a/care/users/models.py b/care/users/models.py index e5578e340d..abafa90fb5 100644 --- a/care/users/models.py +++ b/care/users/models.py @@ -347,9 +347,21 @@ def check_username_exists(username): return User.objects.get_entire_queryset().filter(username=username).exists() def delete(self, *args, **kwargs): + from care.facility.models.facility import ( + FacilityUser, + PatientSample, + UserDefaultAssetLocation, + ) + self.deleted = True self.save() + UserDefaultAssetLocation.objects.filter(user=self).update(deleted=True) + FacilityUser.objects.filter(user=self).update(deleted=True) + FacilityUser.objects.filter(created_by=self).update(deleted=True) + PatientSample.objects.filter(created_by=self).update(created_by=None) + PatientSample.objects.filter(last_edited_by=self).update(last_edited_by=None) + def get_absolute_url(self): return reverse("users:detail", kwargs={"username": self.username})