def get(self, request, *args, **kwargs):
        """
        :param request:
        :param args: language, country
        :param kwargs:
        :return: searched country
        """
        try:
            query_object = country_filtering(request)
            if len(query_object) < 1:
                # logger
                Log.error("Query params didn't provide.", request)
                return Response(prepare_error_response(
                    message="Query params didn't provide."),
                                status=HTTP_400_BAD_REQUEST)

            obj = CountryInfo.objects.filter(query_object)
            serializer = CountryModelSerializer(obj,
                                                context={'request': request},
                                                many=True)

            # logger
            Log.info("Successfully returned", request)
            return Response(prepare_success_response(
                message="Successfully returned", data=serializer.data),
                            status=HTTP_200_OK)
        except Exception as ex:
            # logger
            Log.error(str(ex), request)
            return Response(prepare_error_response(message=str(ex)),
                            status=HTTP_500_INTERNAL_SERVER_ERROR)
Example #2
0
 def test_level(self):
     log = Log(target='console', level=logging.ERROR).get_logger(__name__)
     log.debug('hello,world~')
     log.info('python logs package.')
     log.warning('do not touch it!')
     log.error('runtime error!!')
     log.critical('out of memory!!!')
    def post(self, request, format=None):
        """
        Return auth token
        """
        try:
            serializer = LoginUserNamenSerializer(data=request.data)
            if serializer.is_valid():
                username = serializer.validated_data["username"]
                password = serializer.validated_data["password"]
                auth = authenticate(username=username, password=password)

                if auth is not None:
                    try:
                        refresh = RefreshToken.for_user(auth)
                        token = str(refresh.access_token)
                        data = {"token": token}
                        # logger
                        Log.info(f"success | Authentication success", request)
                        return Response(prepare_success_response(
                            message="Authentication successfull", data=data),
                                        status=HTTP_200_OK)
                    except Exception as ex:
                        # logger
                        Log.error(ex, request)
                        response_param = {
                            "message": "Token generation failed",
                            "data": {},
                            "type": "error"
                        }
                        return Response(response_param, status=HTTP_200_OK)

                else:
                    # logger
                    Log.error(f"error | Employee id or password mistmatch",
                              request)
                    return Response(prepare_error_response(
                        "'Employee id' or 'Password' Mismatch"),
                                    status=HTTP_400_BAD_REQUEST)

            else:
                # logger
                Log.error(f"error | Plz enter correct data.", request)
                return Response(
                    prepare_error_response("Plz, enter correct data."),
                    status=HTTP_400_BAD_REQUEST)

        except Exception as ex:
            Log.error(ex, request)
            return Response(
                {
                    "message": "Internal server error",
                    "data": None,
                    "type": "error"
                },
                status=HTTP_500_INTERNAL_SERVER_ERROR)