Example #1
0
class Metric(View):

    __request = None
    __response = None
    __helpers = None
    __form = None
    __logger = None
    __user_id = None
    __metric = None
    __correlation_id = None

    def __init__(self):
        self.__request = Request()
        self.__response = Response()
        self.__helpers = Helpers()
        self.__form = Form()
        self.__metric = MetricModule()
        self.__logger = self.__helpers.get_logger(__name__)
        self.__form.add_validator(ExtraRules())

    @allow_if_authenticated
    def post(self, request, metric_id):

        self.__correlation_id = request.META[
            "X-Correlation-ID"] if "X-Correlation-ID" in request.META else ""
        self.__request.set_request(request)

        request_data = self.__request.get_request_data(
            "post", {
                "title": "",
                "description": "",
                "source": "",
                "application": "",
                "metric": "",
                "x_axis": "",
                "y_axis": ""
            })

        self.__form.add_inputs({
            'title': {
                'value': request_data["title"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'description': {
                'value': request_data["description"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'source': {
                'value': request_data["source"],
                'validate': {
                    'any_of': {
                        'param': [["newrelic"]],
                        'error': _('Error! Source is invalid.')
                    }
                }
            },
            'application': {
                'value': request_data["application"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'metric': {
                'value': request_data["metric"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'x_axis': {
                'value': request_data["x_axis"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            },
            'y_axis': {
                'value': request_data["y_axis"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {}
            }
        })

        self.__form.process()

        if not self.__form.is_passed():
            return JsonResponse(
                self.__response.send_errors_failure(self.__form.get_errors(),
                                                    {}, self.__correlation_id))

        result = self.__metric.update_one_by_id(
            metric_id, {
                "title":
                self.__form.get_sinput("title"),
                "description":
                self.__form.get_sinput("description"),
                "source":
                self.__form.get_sinput("source"),
                "x_axis":
                self.__form.get_sinput("x_axis"),
                "y_axis":
                self.__form.get_sinput("y_axis"),
                "data":
                '{"application":"%s", "metric":"%s"}' %
                (self.__form.get_sinput("application"),
                 self.__form.get_sinput("metric"))
            })

        if result:
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Metric updated successfully.")
                    }], {}, self.__correlation_id))
        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while updating metric.")
                }], {}, self.__correlation_id))

    @allow_if_authenticated
    def delete(self, request, metric_id):

        self.__correlation_id = request.META[
            "X-Correlation-ID"] if "X-Correlation-ID" in request.META else ""
        self.__user_id = request.user.id

        if self.__metric.delete_one_by_id(metric_id):
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Metric deleted successfully.")
                    }], {}, self.__correlation_id))

        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while deleting metric.")
                }], {}, self.__correlation_id))
Example #2
0
class Metric(View, Controller):
    """Update and Delete Metric Private Endpoint Controller"""

    def __init__(self):
        self.__metric = MetricModule()

    @allow_if_authenticated
    def post(self, request, metric_id):

        request_data = self.get_request_data(request, "post", {
            "title": "",
            "description": "",
            "source": "",
            "application": "",
            "metric": "",
            "x_axis": "",
            "y_axis": ""
        })

        self.form().add_inputs({
            'title': {
                'value': request_data["title"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 60],
                        'error': _('Error! Metric title must be 1 to 60 characters long.')
                    }
                }
            },
            'description': {
                'value': request_data["description"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [0, 150],
                        'error': _('Error! Metric description must be less than 150 characters long.')
                    },
                    'optional': {}
                }
            },
            'source': {
                'value': request_data["source"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'any_of': {
                        'param': [["newrelic"]],
                        'error': _('Error! Source is invalid.')
                    }
                }
            },
            'application': {
                'value': request_data["application"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 60],
                        'error': _('Error! Application must be 1 to 60 characters long.')
                    }
                }
            },
            'metric': {
                'value': request_data["metric"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 60],
                        'error': _('Error! Metric must be 1 to 60 characters long.')
                    }
                }
            },
            'x_axis': {
                'value': request_data["x_axis"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 40],
                        'error': _('Error! X-Axis label must be 1 to 40 characters long.')
                    }
                }
            },
            'y_axis': {
                'value': request_data["y_axis"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 40],
                        'error': _('Error! Y-Axis label must be 1 to 40 characters long.')
                    }
                }
            }
        })

        self.form().process()

        if not self.form().is_passed():
            return self.json(self.form().get_errors())

        current_metric = self.__metric.get_one_by_title(self.form().get_sinput("title"))

        if current_metric and not current_metric["id"] == metric_id:
            return self.json([{
                "type": "error",
                "message": _("Error! Metric title is used before.")
            }])

        result = self.__metric.update_one_by_id(metric_id, {
            "title": self.form().get_sinput("title"),
            "description": self.form().get_sinput("description"),
            "source": self.form().get_sinput("source"),
            "x_axis": self.form().get_sinput("x_axis"),
            "y_axis": self.form().get_sinput("y_axis"),
            "data": '{"application":"%s", "metric":"%s"}' % (
                self.form().get_sinput("application"),
                self.form().get_sinput("metric")
            )
        })

        if result:
            return self.json([{
                "type": "success",
                "message": _("Metric updated successfully.")
            }])
        else:
            return self.json([{
                "type": "error",
                "message": _("Error! Something goes wrong while updating metric.")
            }])

    @allow_if_authenticated
    def delete(self, request, metric_id):

        if self.__metric.delete_one_by_id(metric_id):
            return self.json([{
                "type": "success",
                "message": _("Metric deleted successfully.")
            }])
        else:
            return self.json([{
                "type": "error",
                "message": _("Error! Something goes wrong while deleting metric.")
            }])
Example #3
0
class Metric(View):
    """Update and Delete Metric Private Endpoint Controller"""
    def __init__(self):
        self.__request = Request()
        self.__response = Response()
        self.__helpers = Helpers()
        self.__form = Form()
        self.__metric = MetricModule()
        self.__logger = self.__helpers.get_logger(__name__)
        self.__user_id = None
        self.__correlation_id = ""
        self.__form.add_validator(ExtraRules())

    @allow_if_authenticated
    def post(self, request, metric_id):

        self.__correlation_id = request.META[
            "X-Correlation-ID"] if "X-Correlation-ID" in request.META else ""
        self.__request.set_request(request)

        request_data = self.__request.get_request_data(
            "post", {
                "title": "",
                "description": "",
                "source": "",
                "application": "",
                "metric": "",
                "x_axis": "",
                "y_axis": ""
            })

        self.__form.add_inputs({
            'title': {
                'value': request_data["title"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 60],
                        'error':
                        _('Error! Metric title must be 1 to 60 characters long.'
                          )
                    }
                }
            },
            'description': {
                'value': request_data["description"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [0, 150],
                        'error':
                        _('Error! Metric description must be less than 150 characters long.'
                          )
                    },
                    'optional': {}
                }
            },
            'source': {
                'value': request_data["source"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'any_of': {
                        'param': [["newrelic"]],
                        'error': _('Error! Source is invalid.')
                    }
                }
            },
            'application': {
                'value': request_data["application"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 60],
                        'error':
                        _('Error! Application must be 1 to 60 characters long.'
                          )
                    }
                }
            },
            'metric': {
                'value': request_data["metric"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 60],
                        'error':
                        _('Error! Metric must be 1 to 60 characters long.')
                    }
                }
            },
            'x_axis': {
                'value': request_data["x_axis"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 40],
                        'error':
                        _('Error! X-Axis label must be 1 to 40 characters long.'
                          )
                    }
                }
            },
            'y_axis': {
                'value': request_data["y_axis"],
                'sanitize': {
                    'strip': {}
                },
                'validate': {
                    'length_between': {
                        'param': [1, 40],
                        'error':
                        _('Error! Y-Axis label must be 1 to 40 characters long.'
                          )
                    }
                }
            }
        })

        self.__form.process()

        if not self.__form.is_passed():
            return JsonResponse(
                self.__response.send_errors_failure(self.__form.get_errors(),
                                                    {}, self.__correlation_id))

        current_metric = self.__metric.get_one_by_title(
            self.__form.get_sinput("title"))

        if current_metric and not current_metric["id"] == metric_id:
            return JsonResponse(
                self.__response.send_private_failure(
                    [{
                        "type": "error",
                        "message": _("Error! Metric title is used before.")
                    }], {}, self.__correlation_id))

        result = self.__metric.update_one_by_id(
            metric_id, {
                "title":
                self.__form.get_sinput("title"),
                "description":
                self.__form.get_sinput("description"),
                "source":
                self.__form.get_sinput("source"),
                "x_axis":
                self.__form.get_sinput("x_axis"),
                "y_axis":
                self.__form.get_sinput("y_axis"),
                "data":
                '{"application":"%s", "metric":"%s"}' %
                (self.__form.get_sinput("application"),
                 self.__form.get_sinput("metric"))
            })

        if result:
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Metric updated successfully.")
                    }], {}, self.__correlation_id))
        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while updating metric.")
                }], {}, self.__correlation_id))

    @allow_if_authenticated
    def delete(self, request, metric_id):

        self.__correlation_id = request.META[
            "X-Correlation-ID"] if "X-Correlation-ID" in request.META else ""
        self.__user_id = request.user.id

        if self.__metric.delete_one_by_id(metric_id):
            return JsonResponse(
                self.__response.send_private_success(
                    [{
                        "type": "success",
                        "message": _("Metric deleted successfully.")
                    }], {}, self.__correlation_id))

        else:
            return JsonResponse(
                self.__response.send_private_failure([{
                    "type":
                    "error",
                    "message":
                    _("Error! Something goes wrong while deleting metric.")
                }], {}, self.__correlation_id))