Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implementing manual logic of on_delete option in M2M models #1344

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
8946d5f
used prefetch_related method to optimize the query performed to fetch…
yaswanthsaivendra May 23, 2023
7d410fd
Revert "add direnv support (#1304)"
yaswanthsaivendra May 26, 2023
7179fdf
Revert "Revert "add direnv support (#1304)""
yaswanthsaivendra May 26, 2023
9cb5f7c
Revert "used prefetch_related method to optimize the query performed …
yaswanthsaivendra May 26, 2023
d2134a9
Merge branch 'coronasafe:master' into master
yaswanthsaivendra May 26, 2023
7000a38
used prefetch where we are querying the skills using UserAssigned Ser…
yaswanthsaivendra May 26, 2023
a75b98d
Revert "used prefetch where we are querying the skills using UserAssi…
yaswanthsaivendra May 27, 2023
f59ff6b
made neccessary changes for prefetching the skills
yaswanthsaivendra May 27, 2023
ec69b24
Merge branch 'coronasafe:master' into master
yaswanthsaivendra May 29, 2023
e6541ef
Revert "made neccessary changes for prefetching the skills"
yaswanthsaivendra May 30, 2023
c82bc1c
Manually cascade delete M2M models when related model is deleted
yaswanthsaivendra Jun 2, 2023
615fa24
changes it to soft delete from hard delete - fix
yaswanthsaivendra Jun 3, 2023
26d78a4
Merge branch 'master' into delete_m2m_models
yaswanthsaivendra Jun 7, 2023
c7ea42f
fixed circular import
yaswanthsaivendra Jun 7, 2023
1c797ad
Merge branch 'master' into delete_m2m_models
yaswanthsaivendra Jun 8, 2023
c3dc2ec
Merge branch 'master' into delete_m2m_models
vigneshhari Jun 25, 2023
804ac53
Merge branch 'master' into delete_m2m_models
sainak Jul 19, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions care/facility/models/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
1 change: 1 addition & 0 deletions care/facility/models/bed.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
11 changes: 11 additions & 0 deletions care/facility/models/facility.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
7 changes: 7 additions & 0 deletions care/facility/models/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
13 changes: 13 additions & 0 deletions care/facility/models/patient_consultation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
15 changes: 15 additions & 0 deletions care/facility/models/patient_investigation.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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(
Expand All @@ -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(
Expand Down
12 changes: 12 additions & 0 deletions care/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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})

Expand Down