Commit 28887d40 authored by Gladys Forte's avatar Gladys Forte

restrictions delegations

parent 93a076f9
......@@ -5,7 +5,7 @@ from rest_framework.response import Response
from app.applicationlayer.utils import model_to_dict
from rest_framework.filters import SearchFilter, OrderingFilter
from django_filters import rest_framework as filters
from app.entities.models import Delegation
from app.entities.models import Delegation, Department
from app.applicationlayer.utils import (
CustomPagination, status_message_response, log_save
)
......@@ -67,27 +67,51 @@ class DelegationViewSet(viewsets.ModelViewSet):
instance = self.get_object()
new_instance = model_to_dict(instance)
# restrictions
if (new_instance['name'].lower() == 'head of department' or
new_instance['name'].lower() == 'sd/od' or
new_instance['name'].lower() == 'change control board' or
new_instance['name'].lower() == 'approver' or
new_instance['name'].lower() == 'vendor/implementor' or
new_instance['name'].lower() == 'requestor'):
print("hello")
exist_in_dept = instance.departments.all()
self.perform_destroy(instance)
log_save(
enums.LogEnum.DELETED.value,
enums.LogEntitiesEnum.DELEGATION.value,
new_instance['id'],
new_instance,
''
)
return Response(status=status.HTTP_204_NO_CONTENT)
# restrictions
if not exist_in_dept.count() > 0:
print(exist_in_dept)
if not (new_instance['name'].lower() == 'head of department' or
new_instance['name'].lower() == 'sd/od' or
new_instance['name'].lower() == 'change control board' or
new_instance['name'].lower() == 'approver' or
new_instance['name'].lower() == 'vendor/implementor' or
new_instance['name'].lower() == 'requestor'):
self.perform_destroy(instance)
log_save(
enums.LogEnum.DELETED.value,
enums.LogEntitiesEnum.DELEGATION.value,
new_instance['id'],
new_instance,
''
)
message = {
'code': 200,
'status': 'success',
'message': 'Delegation Successfully deleted'
}
return Response(message, status=status.HTTP_200_OK)
else:
message = {
'code': 400,
'status': 'failed',
'message': 'Default delegation cannot delete'
}
return Response(message, status=status.HTTP_400_BAD_REQUEST)
else:
message = {
'code': 400,
'status': 'failed',
'message': 'Cannot delete this delegation due to existing record in department'
}
return Response(message, status=status.HTTP_400_BAD_REQUEST)
@transaction.atomic
def update(self, request, *args, **kwargs):
......@@ -98,15 +122,33 @@ class DelegationViewSet(viewsets.ModelViewSet):
serializer.is_valid(raise_exception=True)
old_instance = model_to_dict(instance)
self.perform_update(serializer)
new_instance = serializer.data
log_save(
enums.LogEnum.DELETED.value,
enums.LogEntitiesEnum.DELEGATION.value,
old_instance['id'],
old_instance,
new_instance
)
return Response(serializer.data)
# restrictions
if not (new_instance['name'].lower() == 'head of department' or
new_instance['name'].lower() == 'sd/od' or
new_instance['name'].lower() == 'change control board' or
new_instance['name'].lower() == 'approver' or
new_instance['name'].lower() == 'vendor/implementor' or
new_instance['name'].lower() == 'requestor'):
self.perform_update(serializer)
new_instance = serializer.data
log_save(
enums.LogEnum.DELETED.value,
enums.LogEntitiesEnum.DELEGATION.value,
old_instance['id'],
old_instance,
new_instance
)
return Response(serializer.data)
else:
message = {
'code': 400,
'status': 'failed',
'message': 'Default delegation cannot edit'
}
return Response(message, status=status.HTTP_400_BAD_REQUEST)
# Generated by Django 2.2 on 2019-11-11 15:49
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('entities', '0035_changerequesttemplateapprovers_is_default'),
]
operations = [
migrations.AlterField(
model_name='department',
name='delegation',
field=models.ManyToManyField(related_name='departments', to='entities.Delegation'),
),
]
......@@ -149,7 +149,8 @@ class Department(AuditClass):
)
name = models.CharField(max_length=255)
delegation = models.ManyToManyField(
Delegation
Delegation,
related_name='departments'
)
class Meta:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment