Exemplo n.º 1
0
    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)
Exemplo n.º 2
0
def update_contact(c_id, **kwargs):
    try:
        contact_obj = ContactList.objects.filter(id=c_id).first()
        if contact_obj:
            if kwargs['phone_number']:
                number = kwargs['phone_number']
                contact_obj.number = number

            if kwargs["contact_name"]:
                name = kwargs["contact_name"]
                contact_obj.name = name

            if kwargs["contact_email"]:
                email = kwargs["contact_email"]
                contact_obj.email = email

            contact_obj.save()

            response = SuccessResponse(
                msg=SuccessConstants.CONTACT_UPDATE_SUCCESS)

        else:
            response = ErrorResponse(msg=ErrorConstants.CONTACT_NOT_FOUND)

        return response

    except Exception as e:
        logger.error(ErrorConstants.CONTACT_UPDATE_ERROR + str(e),
                     exc_info=True)
        return None
Exemplo n.º 3
0
def create_contact(**kwargs):
    try:
        number = kwargs["phone_number"]
        email = kwargs["contact_email"]
        if not ContactList.objects.filter(email=email):
            contact_obj = ContactList.objects.create(number=number,
                                                     email=email)
        else:
            response = ErrorResponse(
                msg=ErrorConstants.CONTACT_WITH_PROVIDED_EMAIL_EXIST)
            return response

        if kwargs["contact_name"] != "":
            name = kwargs["contact_name"]
            contact_obj.name = name

        contact_obj.save()

        response = PublishContactResponse(contact_obj)
        return response

    except Exception as e:
        logger.error(ErrorConstants.CONTACT_CREATION_ERROR + str(e),
                     exc_info=True)
        return None
Exemplo n.º 4
0
def get_contacts():
    try:
        if 'contact_list' in r_cache:
            contact_obj_list_json = r_cache.get('contact_list')
        else:
            contact_obj = ContactList.objects.all()
            contact_obj_list = []
            if contact_obj:
                for contacts in contact_obj:
                    contact_obj_dict = {
                        "id": contacts.id,
                        "name": contacts.name,
                        "number": contacts.number,
                        "email": contacts.email
                    }
                    contact_obj_list.append(contact_obj_dict)
                    contact_obj_list_json = json.dumps(contact_obj_list)
                r_cache.set(get_contact_list(), contact_obj_list_json,
                            CacheNameSpace.CONTACT_LIST[1])

            else:
                response = ErrorResponse(msg=ErrorConstants.CONTACT_NOT_FOUND)
                return response

        response = ContactListResponse(json.loads(contact_obj_list_json))
        return response

    except Exception as e:
        logger.error(ErrorConstants.CONTACT_LISTING_ERROR + str(e),
                     exc_info=True)
        return None
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
    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)
Exemplo n.º 8
0
def delete_contact(c_id):
    try:
        contact_obj = ContactList.objects.filter(id=c_id).first()
        if not contact_obj:
            response = ErrorResponse(msg=ErrorConstants.CONTACT_NOT_FOUND)
        else:
            contact_obj.delete()
            response = SuccessResponse(
                msg=SuccessConstants.CONTACT_DELETE_SUCCESS)

        return response

    except Exception as e:
        logger.error(ErrorConstants.CONTACT_DELETE_ERROR + str(e),
                     exc_info=True)
        return None
Exemplo n.º 9
0
    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)