def update_assigned_to_id(self, request, *args, **kwargs): try: instance = self.get_object() serializer = None validation_serializer = InspectionSerializer(instance, context={'request': request}) user_in_group = validation_serializer.data.get('user_in_group') if request.data.get('current_user') and user_in_group: serializer = UpdateAssignedToIdSerializer( instance=instance, data={ 'assigned_to_id': request.user.id, } ) elif user_in_group: serializer = UpdateAssignedToIdSerializer(instance=instance, data=request.data) if serializer: serializer.is_valid(raise_exception=True) if serializer.is_valid(): serializer.save() return_serializer = InspectionSerializer(instance=instance, context={'request': request} ) headers = self.get_success_headers(return_serializer.data) return Response( return_serializer.data, status=status.HTTP_201_CREATED, headers=headers ) else: return Response(validation_serializer.data, status=status.HTTP_201_CREATED ) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def workflow_action(self, request, instance=None, create_inspection=None, *args, **kwargs): print("workflow action") print(request.data) try: with transaction.atomic(): # email recipient #recipient_id = None if not instance: instance = self.get_object() comms_log_id = request.data.get('inspection_comms_log_id') if comms_log_id and comms_log_id is not 'null': workflow_entry = instance.comms_logs.get( id=comms_log_id) else: workflow_entry = self.add_comms_log(request, instance, workflow=True) temporary_document_collection_id = request.data.get('temporary_document_collection_id') if temporary_document_collection_id: temp_doc_collection, created = TemporaryDocumentCollection.objects.get_or_create( id=temporary_document_collection_id) if temp_doc_collection: for doc in temp_doc_collection.documents.all(): save_comms_log_document_obj(instance, workflow_entry, doc) temp_doc_collection.delete() # Set Inspection status depending on workflow type workflow_type = request.data.get('workflow_type') if workflow_type == 'send_to_manager': instance.send_to_manager(request) elif workflow_type == 'request_amendment': instance.request_amendment(request) elif workflow_type == 'endorse': instance.endorse(request) elif workflow_type == 'close': instance.close(request) #if not workflow_type or workflow_type in ('', ''): if create_inspection: instance.region_id = None if not request.data.get('region_id') else request.data.get('region_id') instance.district_id = None if not request.data.get('district_id') else request.data.get('district_id') instance.assigned_to_id = None if not request.data.get('assigned_to_id') else request.data.get('assigned_to_id') instance.inspection_type_id = None if not request.data.get('inspection_type_id') else request.data.get('inspection_type_id') instance.allocated_group_id = None if not request.data.get('allocated_group_id') else request.data.get('allocated_group_id') instance.call_email_id = None if not request.data.get('call_email_id') else request.data.get('call_email_id') instance.legal_case_id = None if not request.data.get('legal_case_id') else request.data.get('legal_case_id') instance.details = None if not request.data.get('details') else request.data.get('details') #elif workflow_type not in ('send_to_manager', 'request_amendment'): # instance.assigned_to_id = None if not request.data.get('assigned_to_id') else request.data.get('assigned_to_id') else: instance.assigned_to_id = None instance.allocated_group_id = None if not request.data.get('allocated_group_id') else request.data.get('allocated_group_id') #recipient_id = instance.inspection_team_lead_id instance.save() # Needed for create inspection if create_inspection: instance = self.modify_inspection_team(request, instance, workflow=True, user_id=instance.assigned_to_id) # send email if workflow_type == 'send_to_manager': email_data = prepare_mail( request, instance, workflow_entry, send_mail, recipient_id=instance.inspection_team_lead_id, email_type='send_to_manager') elif workflow_type == 'request_amendment': email_data = prepare_mail( request, instance, workflow_entry, send_mail, email_type='request_amendment') elif workflow_type == 'endorse': email_data = prepare_mail( request, instance, workflow_entry, send_mail, email_type='endorse') else: email_data = prepare_mail(request, instance, workflow_entry, send_mail) serializer = InspectionCommsLogEntrySerializer(instance=workflow_entry, data=email_data, partial=True) serializer.is_valid(raise_exception=True) if serializer.is_valid(): serializer.save() return_serializer = InspectionSerializer(instance=instance, context={'request': request} ) headers = self.get_success_headers(return_serializer.data) return Response( return_serializer.data, status=status.HTTP_201_CREATED, headers=headers ) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def update(self, request, workflow=False, *args, **kwargs): try: with transaction.atomic(): # 1. Save Location if ( request.data.get('location', {}).get('geometry', {}).get('coordinates', {}) or request.data.get('location', {}).get('properties', {}).get('postcode', {}) or request.data.get('location', {}).get('properties', {}).get('details', {}) ): location_request_data = request.data.get('location') returned_location = save_location(location_request_data) if returned_location: request.data.update({'location_id': returned_location.get('id')}) instance = self.get_object() # record individual inspected before update individual_inspected_id = instance.individual_inspected_id # record organisation inspected before update organisation_inspected_id = instance.organisation_inspected_id # record party_inspected before update party_inspected = instance.party_inspected if request.data.get('renderer_data'): self.form_data(request) if instance.inspection_type and not request.data.get('inspection_type_id') and 'inspection_type_id' in request.data.keys(): del request.data['inspection_type_id'] serializer = SaveInspectionSerializer(instance, data=request.data) serializer.is_valid(raise_exception=True) if serializer.is_valid(): serializer.save() instance.log_user_action( InspectionUserAction.ACTION_SAVE_INSPECTION_.format( instance.number), request) # Log individual_inspected update if applicable if instance.party_inspected == 'individual' and individual_inspected_id and \ (individual_inspected_id != instance.individual_inspected_id or \ party_inspected != instance.party_inspected): prev_individual_inspected = EmailUser.objects.get(id=individual_inspected_id) instance.log_user_action( InspectionUserAction.ACTION_CHANGE_INDIVIDUAL_INSPECTED.format( prev_individual_inspected.get_full_name(), instance.individual_inspected.get_full_name()), request) # Log organisation_inspected update if applicable if instance.party_inspected == 'organisation' and organisation_inspected_id and \ (organisation_inspected_id != instance.organisation_inspected_id or \ party_inspected != instance.party_inspected): prev_organisation_inspected = Organisation.objects.get(id=organisation_inspected_id) instance.log_user_action( InspectionUserAction.ACTION_CHANGE_ORGANISATION_INSPECTED.format( prev_organisation_inspected.name, instance.organisation_inspected.name), request) headers = self.get_success_headers(serializer.data) return_serializer = InspectionSerializer(instance, context={'request': request}) return Response( return_serializer.data, status=status.HTTP_201_CREATED, headers=headers ) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))
def modify_inspection_team(self, request, instance=None, workflow=False, user_id=None, *args, **kwargs): try: with transaction.atomic(): if not instance: instance = self.get_object() if workflow: action = 'add' # 'add', 'remove or 'clear' user_id = user_id else: action = request.data.get('action') # 'add', 'remove or 'clear' user_id = request.data.get('user_id') # ensure user_id is int if user_id: user_id = int(user_id) #if action and user_list: if action and user_id: #users = EmailUser.objects.filter(id__in=user_list) user = EmailUser.objects.get(id=user_id) team_member_list = instance.inspection_team.all() #if action == 'set' and users: # instance.inspection_team.set(user_list) if action == 'add': if user not in team_member_list: instance.inspection_team.add(user) instance.log_user_action( InspectionUserAction.ACTION_ADD_TEAM_MEMBER.format( user.get_full_name()), request) if not instance.inspection_team_lead or not team_member_list: instance.inspection_team_lead = user instance.log_user_action( InspectionUserAction.ACTION_MAKE_TEAM_LEAD.format( user.get_full_name()), request) if action == 'remove': if user in team_member_list: instance.inspection_team.remove(user) instance.log_user_action( InspectionUserAction.ACTION_REMOVE_TEAM_MEMBER.format( user.get_full_name()), request) team_member_list = instance.inspection_team.all() if team_member_list and not instance.inspection_team_lead_id in team_member_list: instance.inspection_team_lead = team_member_list[0] else: instance.inspection_team_lead_id = None if action == 'make_team_lead': if user not in team_member_list: instance.inspection_team.add(user) instance.inspection_team_lead = user instance.log_user_action( InspectionUserAction.ACTION_MAKE_TEAM_LEAD.format( user.get_full_name()), request) instance.save() if workflow: return instance else: serializer = InspectionSerializer(instance, context={'request': request}) return Response( serializer.data, status=status.HTTP_201_CREATED, ) # List view - no modification else: serializer = InspectionSerializer(instance, context={'request': request}) return Response( serializer.data, status=status.HTTP_200_OK, ) except serializers.ValidationError: print(traceback.print_exc()) raise except ValidationError as e: print(traceback.print_exc()) raise serializers.ValidationError(repr(e.error_dict)) except Exception as e: print(traceback.print_exc()) raise serializers.ValidationError(str(e))