Skip to content

Commit

Permalink
fix ZU-Hospital#39 add soft delete
Browse files Browse the repository at this point in the history
  • Loading branch information
MohamedHamed12 committed May 13, 2024
1 parent fdbc0ea commit b59ccc8
Show file tree
Hide file tree
Showing 18 changed files with 132 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 5.0.3 on 2024-05-13 17:33

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("accounts", "0004_doctor_deleted_doctor_deleted_by_cascade_and_more"),
]

operations = [
migrations.RemoveField(
model_name="doctor",
name="is_deleted",
),
migrations.RemoveField(
model_name="employee",
name="is_deleted",
),
migrations.RemoveField(
model_name="patient",
name="is_deleted",
),
]
2 changes: 1 addition & 1 deletion project/accounts/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Profile(SafeDeleteModel):
notes= models.TextField(blank=True,null=True)
address = models.JSONField(null=True, blank=True)
phone = models.JSONField(null=True, blank=True)
is_deleted = models.BooleanField(default=False)

created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
class Meta:
Expand Down
3 changes: 1 addition & 2 deletions project/accounts/serializers/doctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class DoctorSerializer(serializers.ModelSerializer):
address =AddressSerializer( required=False)
class Meta:
model = Doctor
# fields = '__all__'
exclude = ['is_deleted']
fields = '__all__'



Expand Down
3 changes: 1 addition & 2 deletions project/accounts/serializers/employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ class EmployeeSerializer(serializers.ModelSerializer):
address =AddressSerializer( required=False)
class Meta:
model = Employee
# fields = '__all__'
exclude = ['is_deleted']
fields = '__all__'



Expand Down
7 changes: 3 additions & 4 deletions project/accounts/serializers/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
class UserImageSerializer(serializers.ModelSerializer):
class Meta:
model = UserImage
# fields = '__all__'
exclude = ['is_deleted']
fields = '__all__'



Expand All @@ -19,8 +18,8 @@ class PatientSerializer(serializers.ModelSerializer):
class Meta:
model = Patient


exclude = ['is_deleted']
fields = '__all__'
# exclude = ['is_deleted']
def validate_address(self, value):
address_serializer = AddressSerializer(data=value)
if not address_serializer.is_valid() or value!=address_serializer.data:
Expand Down
2 changes: 1 addition & 1 deletion project/accounts/tests/test_delete.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def test_delete_doctor_restore(self):
url=reverse('doctor-restore')
response = self.client.post(
url, format='json', HTTP_AUTHORIZATION='Bearer ' + self.staff_token,data=data)

# print(response.data)
self.assertEqual(response.status_code, 200)
self.assertEqual(Doctor.objects.count(), 1)
self.assertEqual(len( Doctor.deleted_objects.all()), 0)
Expand Down
10 changes: 5 additions & 5 deletions project/accounts/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@
path('change-password/', ChangePasswordView.as_view(), name='change-password'),


path('patients/restore/', PatientViewSet.as_view({'post': 'restore'}), name='patient-restore'),
path('patients/deleted/', PatientViewSet.as_view({'get': 'get_deleted'}), name='patient-get-deleted'),

path('doctors/restore/', DoctorViewSet.as_view({'post': 'restore'}), name='doctor-restore'),
path('doctors/deleted/', DoctorViewSet.as_view({'get': 'get_deleted'}), name='doctor-get-deleted'),

path('employees/restore/', EmployeeViewSet.as_view({'post': 'restore'}), name='employee-restore'),
path('employees/deleted/', EmployeeViewSet.as_view({'get': 'get_deleted'}), name='employee-get-deleted'),

path('employees/restore/', RestoreEmployeeView.as_view(), name='employee-restore'),
path('patients/restore/', RestorePatientView.as_view(), name='patient-restore'),
path('doctors/restore/', RestoreDoctorView.as_view(), name='doctor-restore'),

]
24 changes: 15 additions & 9 deletions project/accounts/views/doctor.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,7 @@ def destroy(self, request, *args, **kwargs):
return Response(status=status.HTTP_204_NO_CONTENT)
else:
return super().destroy(request, *args, **kwargs)
def restore(self, request, *args, **kwargs):
serializer = RestoreDoctorSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)



def get_deleted(self, request, *args, **kwargs):
paginator = self.pagination_class()
Expand All @@ -84,3 +76,17 @@ def get_deleted(self, request, *args, **kwargs):
serializer = self.get_serializer(result_page, many=True)
return paginator.get_paginated_response(serializer.data)


from rest_framework.generics import GenericAPIView
class RestoreDoctorView(GenericAPIView):
serializer_class = RestoreDoctorSerializer
permission_classes = [IsAuthenticated,CustomPermission]

def post(self, request, *args, **kwargs):
serializer = RestoreDoctorSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)
24 changes: 13 additions & 11 deletions project/accounts/views/employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,7 @@ def destroy(self, request, *args, **kwargs):
return Response(status=status.HTTP_204_NO_CONTENT)
else:
return super().destroy(request, *args, **kwargs)
def restore(self, request, *args, **kwargs):
serializer = RestoreEmployeeSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)



def get_deleted(self, request, *args, **kwargs):
paginator = self.pagination_class()
Expand All @@ -90,6 +82,16 @@ def get_deleted(self, request, *args, **kwargs):
return paginator.get_paginated_response(serializer.data)




from rest_framework.generics import GenericAPIView
class RestoreEmployeeView(GenericAPIView):
serializer_class = RestoreEmployeeSerializer
permission_classes = [IsAuthenticated,CustomPermission]

def post(self, request, *args, **kwargs):
serializer = RestoreEmployeeSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)
24 changes: 15 additions & 9 deletions project/accounts/views/patient.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,20 +74,26 @@ def destroy(self, request, *args, **kwargs):
return Response(status=status.HTTP_204_NO_CONTENT)
else:
return super().destroy(request, *args, **kwargs)
def restore(self, request, *args, **kwargs):
serializer = RestorePatientSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)



def get_deleted(self, request, *args, **kwargs):
paginator = self.pagination_class()
deleted_patients = Patient.deleted_objects.all()
result_page = paginator.paginate_queryset(deleted_patients, request)
serializer = self.get_serializer(result_page, many=True)
return paginator.get_paginated_response(serializer.data)


from rest_framework.generics import GenericAPIView
class RestorePatientView(GenericAPIView):
serializer_class = RestorePatientSerializer

permission_classes = [IsAuthenticated,CustomPermission]
def post(self, request, *args, **kwargs):
serializer = RestorePatientSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)
2 changes: 1 addition & 1 deletion project/visit/filters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Meta:
'end_at': ['year', 'month', 'day', 'hour', 'minute', 'second', 'gt','lt','gte','lte'],
'doctors': ['exact'],
'patient': ['exact'],
'is_deleted': ['exact'],
# 'is_deleted': ['exact'],
'created_at': ['year', 'month', 'day', 'hour', 'minute', 'second', 'gt','lt','gte','lte'],

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Generated by Django 5.0.3 on 2024-05-13 17:33

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("visit", "0003_attachment_deleted_attachment_deleted_by_cascade_and_more"),
]

operations = [
migrations.RemoveField(
model_name="attachment",
name="is_deleted",
),
migrations.RemoveField(
model_name="visit",
name="is_deleted",
),
]
2 changes: 0 additions & 2 deletions project/visit/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class Visit(SafeDeleteModel):
patient = models.ForeignKey('accounts.Patient', on_delete=models.CASCADE)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
is_deleted = models.BooleanField(default=False)
notes = models.TextField(blank=True)
def save(self, *args, **kwargs):
self.visit_number = Visit.objects.count()+1
Expand All @@ -43,7 +42,6 @@ class Attachment(SafeDeleteModel):
notes = models.TextField(blank=True)
created_at = models.DateTimeField(auto_now_add=True)
updated_at = models.DateTimeField(auto_now=True)
is_deleted = models.BooleanField(default=False)
def save(self, *args, **kwargs):
self.file_type = self.file.name.split('.')[-1]
super(Attachment, self).save(*args, **kwargs)
Expand Down
3 changes: 1 addition & 2 deletions project/visit/serializers/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ class AttachmentSerializer(serializers.ModelSerializer):
file_type=serializers.CharField(read_only=True)
class Meta:
model = Attachment
# fields = '__all__'
exclude = ['is_deleted']
fields = '__all__'

class RestoreAttachmentSerializer(serializers.Serializer):
id = serializers.IntegerField()
Expand Down
3 changes: 1 addition & 2 deletions project/visit/serializers/visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,7 @@ class VisitSerializer(serializers.ModelSerializer):
attachment = AttachmentSerializer( read_only=True, many=True,source='visit_attachments', required=False)
class Meta:
model = Visit
# fields = '__all__'
exclude = ['is_deleted']
fields = '__all__'


class RestoreVisitSerializer(serializers.Serializer):
Expand Down
4 changes: 2 additions & 2 deletions project/visit/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@
# path('patients/restore/', PatientViewSet.as_view({'post': 'restore'}), name='patient-restore'),
# path('patients/deleted/', PatientViewSet.as_view({'get': 'get_deleted'}), name='patient-get-deleted'),

path('visits/restore/', VisitViewSet.as_view({'post': 'restore'}), name='visit-restore'),
path('visits/deleted/', VisitViewSet.as_view({'get': 'get_deleted'}), name='visit-get-deleted'),

path('attachments/restore/', AttachmentViewSet.as_view({'post': 'restore'}), name='attachment-restore'),
path('attachments/deleted/', AttachmentViewSet.as_view({'get': 'get_deleted'}), name='attachment-get-deleted'),
path('visits/restore/', RestoreVisitView.as_view(), name='visit-restore'),
path('attachments/restore/', RestoreAttachmentView.as_view(), name='attachment-restore'),
]
23 changes: 13 additions & 10 deletions project/visit/views/attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,23 +56,26 @@ def destroy(self, request, *args, **kwargs):
return Response(status=status.HTTP_204_NO_CONTENT)
else:
return super().destroy(request, *args, **kwargs)
def restore(self, request, *args, **kwargs):
serializer = RestoreAttachmentSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)



def get_deleted(self, request, *args, **kwargs):
paginator = self.pagination_class()
deleted_attachments = Attachment.deleted_objects.all()
result_page = paginator.paginate_queryset( deleted_attachments, request)
serializer = self.get_serializer(result_page, many=True)
return paginator.get_paginated_response(serializer.data)

from rest_framework.generics import GenericAPIView
class RestoreAttachmentView(GenericAPIView):
serializer_class=RestoreAttachmentSerializer
pagination_class = CustomPagination
def post(self, request, *args, **kwargs):
serializer = RestoreAttachmentSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)



Expand Down
22 changes: 13 additions & 9 deletions project/visit/views/visit.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,7 @@ def destroy(self, request, *args, **kwargs):
return Response(status=status.HTTP_204_NO_CONTENT)
else:
return super().destroy(request, *args, **kwargs)
def restore(self, request, *args, **kwargs):
serializer = RestoreVisitSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)



def get_deleted(self, request, *args, **kwargs):
Expand All @@ -72,7 +65,18 @@ def get_deleted(self, request, *args, **kwargs):
result_page = paginator.paginate_queryset(deleted_visits, request)
serializer = self.get_serializer(result_page, many=True)
return paginator.get_paginated_response(serializer.data)

from rest_framework.generics import GenericAPIView
class RestoreVisitView(GenericAPIView):
serializer_class=RestoreVisitSerializer
pagination_class = CustomPagination
def post(self, request, *args, **kwargs):
serializer = RestoreVisitSerializer(data=request.data)
if not serializer.is_valid():
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

instance = serializer.validated_data['id']
instance.undelete()
return Response(status=status.HTTP_200_OK)



Expand Down

0 comments on commit b59ccc8

Please # to comment.