Commit ed98e131 authored by Gladys Forte's avatar Gladys Forte

using decorator

parent b2262f80
......@@ -35,6 +35,7 @@ from app.applicationlayer.cms.template.attachment.serializers import ChangeReque
from app.applicationlayer.cms.template.header.serializers import ChangeRequestTemplatesSerializer
from app.applicationlayer.cms.template.header.serializers import ChangeRequestTemplatesSerializerList
from app.applicationlayer.cms.template.header.table_filters import HeaderFilterSet
from app.helper.decorators import *
# from django_filters import rest_framework as filters
......@@ -341,55 +342,56 @@ class ChangeRequestTemplatesViewset(viewsets.ModelViewSet):
class ChangeRequestTemplatePost(APIView):
@TemplateValidation
@transaction.atomic()
def post(self, request):
template_header = request.data
try:
# restrict superuser department
if (template_header['created_by_department'] == 'DEPARTMENT-20190923-0000001' or
template_header['requested_to_department'] == 'DEPARTMENT-20190923-0000001'):
message = {
'code': 400,
'status': 'failed',
'message': 'Superuser department cannot be selected',
}
return Response(message,
status=status.HTTP_400_BAD_REQUEST)
tmp_approvers = template_header['tmp_approvers']
# Check if Vendor and Requestor are existing on routing table
if len(tmp_approvers) < 2:
message = {
'code': 400,
'status': 'failed',
'message': 'Please make sure to add an Approver, Vendor and Requestor into routing table',
}
return Response(message,
status=status.HTTP_400_BAD_REQUEST)
else:
result = validation_existing_vendor_requestor(tmp_approvers)
if result is False:
message = {
'code': 400,
'status': 'failed',
'message': 'Please add Vendor/Implementor and Requestor into routing table',
}
return Response(message,
status=status.HTTP_400_BAD_REQUEST)
# Do not allow adding an approver for the same level
validation_result = validation_approver_same_level(tmp_approvers)
if validation_result is not None:
message = {
'code': 400,
'status': 'failed',
'message': validation_result + ' is already existing for the same level of approval.',
}
return Response(message,
status=status.HTTP_400_BAD_REQUEST)
# if (template_header['created_by_department'] == 'DEPARTMENT-20190923-0000001' or
# template_header['requested_to_department'] == 'DEPARTMENT-20190923-0000001'):
# message = {
# 'code': 400,
# 'status': 'failed',
# 'message': 'Superuser department cannot be selected',
# }
# return Response(message,
# status=status.HTTP_400_BAD_REQUEST)
# tmp_approvers = template_header['tmp_approvers']
# # Check if Vendor and Requestor are existing on routing table
# if len(tmp_approvers) < 2:
# message = {
# 'code': 400,
# 'status': 'failed',
# 'message': 'Please make sure to add an Approver, Vendor and Requestor into routing table',
# }
# return Response(message,
# status=status.HTTP_400_BAD_REQUEST)
# else:
# result = validation_existing_vendor_requestor(tmp_approvers)
# if result is False:
# message = {
# 'code': 400,
# 'status': 'failed',
# 'message': 'Please add Vendor/Implementor and Requestor into routing table',
# }
# return Response(message,
# status=status.HTTP_400_BAD_REQUEST)
# # Do not allow adding an approver for the same level
# validation_result = validation_approver_same_level(tmp_approvers)
# if validation_result is not None:
# message = {
# 'code': 400,
# 'status': 'failed',
# 'message': validation_result + ' is already existing for the same level of approval.',
# }
# return Response(message,
# status=status.HTTP_400_BAD_REQUEST)
data_list_approver = []
data_list_stake = []
......
......@@ -7,6 +7,11 @@ from rest_framework.authtoken.models import Token
from app.entities.models import User, Department, Company
from app.entities import enums
from django.db.models import Q
from app.businesslayer.changerequest.change_request_template import (
tmp_add_edit_delete,
validation_approver_same_level,
validation_existing_vendor_requestor
)
def error_safe(function):
......@@ -218,3 +223,56 @@ class rms:
return function(self, request, *args, **kwargs)
return wrapper
def TemplateValidation(function):
@wraps(function)
def wrapper(self, request, *args, **kwargs):
template_header = request.data
if (template_header['created_by_department'] == 'DEPARTMENT-20190923-0000001' or
template_header['requested_to_department'] == 'DEPARTMENT-20190923-0000001'):
message = {
'code': 400,
'status': 'failed',
'message': 'Superuser department cannot be selected',
}
return Response(message,
status=status.HTTP_400_BAD_REQUEST)
tmp_approvers = template_header['tmp_approvers']
# Check if Vendor and Requestor are existing on routing table
if len(tmp_approvers) < 2:
message = {
'code': 400,
'status': 'failed',
'message': 'Please make sure to add an Approver, Vendor and Requestor into routing table',
}
return Response(message,
status=status.HTTP_400_BAD_REQUEST)
else:
result = validation_existing_vendor_requestor(tmp_approvers)
if result is False:
message = {
'code': 400,
'status': 'failed',
'message': 'Please add Vendor/Implementor and Requestor into routing table',
}
return Response(message,
status=status.HTTP_400_BAD_REQUEST)
# Do not allow adding an approver for the same level
validation_result = validation_approver_same_level(tmp_approvers)
if validation_result is not None:
message = {
'code': 400,
'status': 'failed',
'message': validation_result + ' is already existing for the same level of approval.',
}
return Response(message,
status=status.HTTP_400_BAD_REQUEST)
return function(self, request, *args, **kwargs)
return wrapper
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