class MetricEdit(View, Controller): """Metric Edit Page Controller""" template_name = 'templates/admin/metric/edit.html' @login_if_not_authenticated def get(self, request, metric_id): self.__metric = MetricModule() metric = self.__metric.get_one_by_id(metric_id) if not metric: raise Http404("Metric not found.") self.autoload_options() self.autoload_user( request.user.id if request.user.is_authenticated else None) self.context_push({ "page_title": _("Edit Metric ยท %s") % self.context_get("app_name", os.getenv("APP_NAME", "Silverback")), "metric": metric }) metric["data"] = json.loads(metric["data"]) return render(request, self.template_name, self.context_get())
class BuilderSystemMetrics(View, Controller): """Add and Remove Builder System Metrics Private Endpoint Controller""" def __init__(self): self.__settings = Settings() self.__metric = MetricModule() @allow_if_authenticated_and_has_permission("manage_settings") def post(self, request): request_data = self.get_request_data(request, "post", {"metric_id": ""}) if request_data["metric_id"] == "" or not self.__metric.get_one_by_id( request_data["metric_id"].replace("m-", "")): return self.json([{ "type": "error", "message": _("Error! Metric is required.") }]) metrics = self.__settings.get_value_by_key("builder_metrics", json.dumps([])) metrics = json.loads(metrics) if request_data["metric_id"] in metrics: return self.json([{ "type": "success", "message": _("Metric added successfully.") }]) metrics.append(request_data["metric_id"]) result = self.__settings.update_options( {"builder_metrics": json.dumps(metrics)}) if result: return self.json([{ "type": "success", "message": _("Metric added successfully.") }]) else: return self.json([{ "type": "error", "message": _("Error! Something goes wrong while adding metric.") }]) @allow_if_authenticated_and_has_permission("manage_settings") def delete(self, request, metric_id): metrics = self.__settings.get_value_by_key("builder_metrics", json.dumps([])) metrics = json.loads(metrics) if metric_id not in metrics: return self.json([{ "type": "success", "message": _("Metric deleted successfully.") }]) metrics.remove(metric_id) result = self.__settings.update_options( {"builder_metrics": json.dumps(metrics)}) if result: return self.json([{ "type": "success", "message": _("Metric deleted successfully.") }]) else: return self.json([{ "type": "error", "message": _("Error! Something goes wrong while deleting metric.") }])
class BuilderSystemMetrics(View): __request = None __response = None __helpers = None __form = None __logger = None __user_id = None __settings = None __metric = None __correlation_id = None def __init__(self): self.__request = Request() self.__response = Response() self.__helpers = Helpers() self.__settings = Settings() self.__metric = MetricModule() self.__form = Form() self.__logger = self.__helpers.get_logger(__name__) self.__form.add_validator(ExtraRules()) @allow_if_authenticated_and_has_permission("manage_settings") def post(self, request): 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", {"metric_id": ""}) if request_data["metric_id"] == "" or not self.__metric.get_one_by_id( request_data["metric_id"]): return JsonResponse( self.__response.send_private_failure( [{ "type": "error", "message": _("Error! Metric is required.") }], {}, self.__correlation_id)) metrics = self.__settings.get_value_by_key("builder_metrics", json.dumps([])) metrics = json.loads(metrics) if request_data["metric_id"] in metrics: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric added successfully.") }], {}, self.__correlation_id)) metrics.append(request_data["metric_id"]) result = self.__settings.update_options( {"builder_metrics": json.dumps(metrics)}) if result: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric added successfully.") }], {}, self.__correlation_id)) else: return JsonResponse( self.__response.send_private_failure([{ "type": "error", "message": _("Error! Something goes wrong while adding metric.") }], {}, self.__correlation_id)) @allow_if_authenticated_and_has_permission("manage_settings") def delete(self, request, metric_id): self.__correlation_id = request.META[ "X-Correlation-ID"] if "X-Correlation-ID" in request.META else "" metrics = self.__settings.get_value_by_key("builder_metrics", json.dumps([])) metrics = json.loads(metrics) if metric_id not in metrics: return JsonResponse( self.__response.send_private_success( [{ "type": "success", "message": _("Metric deleted successfully.") }], {}, self.__correlation_id)) metrics.remove(metric_id) result = self.__settings.update_options( {"builder_metrics": json.dumps(metrics)}) if result: 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))