def destroy(self, request, pk=None): try: if not pk: logger.warning(Warn.CONTACT_ID_REQUIRED) response = SuccessResponse( msg=ErrorConstants.CONTACT_ID_MISSING) return Response(to_dict(response), status=status.HTTP_400_BAD_REQUEST) delete_contact_response = delete_contact(pk) if not delete_contact_response: response = ErrorResponse( msg=ErrorConstants.CONTACT_DELETE_ERROR) return Response(to_dict(response), status=status.HTTP_500_INTERNAL_SERVER_ERROR) elif delete_contact_response.success is False: response = ErrorResponse(msg=delete_contact_response.msg) return Response(to_dict(response), status=status.HTTP_202_ACCEPTED) return Response(to_dict(delete_contact_response), status=status.HTTP_200_OK) except Exception as e: logger.error(ErrorConstants.EXCEPTIONAL_ERROR + str(e), exc_info=True) response = ErrorResponse() return Response(to_dict(response), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def update(self, request, pk=None): try: if not pk: logger.warning(Warn.CONTACT_ID_REQUIRED) response = SuccessResponse( msg=ErrorConstants.CONTACT_ID_MISSING) return Response(to_dict(response), status=status.HTTP_400_BAD_REQUEST) data = request.data c_id = pk phone_number = int(data.get("number", 0)) contact_name = data.get("name", "") contact_email = data.get("email", "") update_contact_response = update_contact( c_id, phone_number=phone_number, contact_name=contact_name, contact_email=contact_email) if not update_contact_response: response = ErrorResponse( msg=ErrorConstants.CONTACT_UPDATE_ERROR) return Response(to_dict(response), status=status.HTTP_500_INTERNAL_SERVER_ERROR) elif update_contact_response.success is False: response = ErrorResponse(msg=update_contact_response.msg) return Response(to_dict(response), status=status.HTTP_202_ACCEPTED) return Response(to_dict(update_contact_response), status=status.HTTP_200_OK) except Exception as e: logger.error(ErrorConstants.EXCEPTIONAL_ERROR + str(e), exc_info=True) response = ErrorResponse() return Response(to_dict(response), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def create(self, request): try: data = request.data phone_number = int(data.get("number", 0)) contact_name = data.get("name", "") contact_email = data.get("email") if contact_email is None: response = ErrorResponse(msg=ErrorConstants.EMAIL_NOT_PROVIDED) return Response(to_dict(response), status=status.HTTP_400_BAD_REQUEST) create_contact_response = create_contact( phone_number=phone_number, contact_name=contact_name, contact_email=contact_email) if not create_contact_response: response = ErrorResponse( msg=ErrorConstants.CONTACT_CREATION_ERROR) return Response(to_dict(response), status=status.HTTP_500_INTERNAL_SERVER_ERROR) elif create_contact_response.success is False: response = ErrorResponse(msg=create_contact_response.msg) return Response(to_dict(response), status=status.HTTP_202_ACCEPTED) return Response(to_dict(create_contact_response), status=status.HTTP_200_OK) except Exception as e: logger.error(ErrorConstants.EXCEPTIONAL_ERROR + str(e), exc_info=True) response = ErrorResponse() return Response(to_dict(response), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def list(self, request): try: contact_list_response = get_contacts() if not contact_list_response: response = ErrorResponse( msg=ErrorConstants.CONTACT_LISTING_ERROR) return Response(to_dict(response), status=status.HTTP_404_NOT_FOUND) elif contact_list_response.success is False: response = ErrorResponse(msg=contact_list_response.msg) return Response(to_dict(response), status=status.HTTP_202_ACCEPTED) return Response(to_dict(contact_list_response), status=status.HTTP_200_OK) except Exception as e: logger.error(ErrorConstants.EXCEPTIONAL_ERROR + str(e), exc_info=True) response = ErrorResponse() return Response(to_dict(response), status=status.HTTP_500_INTERNAL_SERVER_ERROR)
def get_queryset(self): try: queryset = ContactList.objects.all() name = self.request.query_params.get('name', None) email = self.request.query_params.get('email', None) if name is not None: queryset = queryset.filter(name__icontains=name) if email is not None: queryset = queryset.filter(email__icontains=email) return queryset except Exception as e: logger.error(ErrorConstants.EXCEPTIONAL_ERROR + str(e), exc_info=True) response = ErrorResponse() return Response(to_dict(response), status=status.HTTP_500_INTERNAL_SERVER_ERROR)