From 17eec2978540839f1b28b942301bd35e2e0e931d Mon Sep 17 00:00:00 2001 From: Andreas Boesl Date: Tue, 7 Nov 2023 09:15:50 +0100 Subject: [PATCH] moved vue internal code into vue_repr.py Change-Id: I1092270956a125d2fb9dfa2ddf6ce475ecea0606 --- cmk/gui/validation/visitors/vue_repr.py | 41 ++++++++++++++++++------- cmk/gui/wato/pages/global_settings.py | 29 +++-------------- cmk/gui/wato/pages/rulesets.py | 40 +++++++----------------- 3 files changed, 45 insertions(+), 65 deletions(-) diff --git a/cmk/gui/validation/visitors/vue_repr.py b/cmk/gui/validation/visitors/vue_repr.py index 4fc9c9783fe..eb07bf94354 100644 --- a/cmk/gui/validation/visitors/vue_repr.py +++ b/cmk/gui/validation/visitors/vue_repr.py @@ -4,13 +4,14 @@ # conditions defined in the file COPYING, which is part of this source code package. import json import pprint -import traceback import typing import uuid from contextlib import contextmanager from dataclasses import asdict, dataclass from typing import Any +from cmk.utils.exceptions import MKGeneralException + from cmk.gui.exceptions import MKUserError from cmk.gui.htmllib.html import html from cmk.gui.http import request @@ -249,7 +250,7 @@ def create_vue_visitor( return vue_visitor -def process_validation_errors(vue_visitor: VueGeneratingVisitor) -> None: +def _process_validation_errors(vue_visitor: VueGeneratingVisitor) -> None: """This functions introduces validation errors from the vue-world into the CheckMK-GUI-world The CheckMK-GUI works with a global parameter user_errors. These user_errors include the field_id of the broken input field and the error text @@ -270,13 +271,31 @@ def create_vue_app_config(vue_visitor: VueGeneratingVisitor, app_id: str) -> dic ) -def render_vue(valuespec, field_id, value, do_validate=False): +def render_vue(valuespec, field_id, default_value): + """Renders the valuespec via vue within a div + The value to be rendered + - might be taken from the request, which also enables validation + - might be the default value, which is not validated since some mandatory fields might be empty + """ logger.warning("RENDER APP CONFIG") - try: - vue_visitor = create_vue_visitor(valuespec, value, do_validate=do_validate) - vue_app_config = create_vue_app_config(vue_visitor, field_id) - logger.warning("%s", pprint.pformat(vue_app_config, width=180)) - html.div("", data_cmk_vue_app=json.dumps(vue_app_config)) - except Exception: - # Debug only. This block will vanish - logger.warning("".join(traceback.format_exc())) + if request.has_var(field_id): + value = json.loads(request.get_str_input_mandatory(field_id)) + do_validate = True + else: + value = default_value + do_validate = False + + vue_visitor = create_vue_visitor(valuespec, value, do_validate=do_validate) + vue_app_config = create_vue_app_config(vue_visitor, field_id) + logger.warning("%s", pprint.pformat(vue_app_config, width=180)) + html.div("", data_cmk_vue_app=json.dumps(vue_app_config)) + + +def parse_and_validate_vue(valuespec: ValueSpec, field_id: str) -> Any: + """Computes/validates the value from a vue formular field""" + if not request.has_var(field_id): + raise MKGeneralException("Formular data is missing in request") + value_from_frontend = json.loads(request.get_str_input_mandatory(field_id)) + vue_visitor = create_vue_visitor(valuespec, value_from_frontend, do_validate=True) + _process_validation_errors(vue_visitor) + return vue_visitor.raw_value diff --git a/cmk/gui/wato/pages/global_settings.py b/cmk/gui/wato/pages/global_settings.py index 92d47dc0f5a..11f1079f8c0 100644 --- a/cmk/gui/wato/pages/global_settings.py +++ b/cmk/gui/wato/pages/global_settings.py @@ -6,7 +6,6 @@ settings""" import abc -import json from collections.abc import Collection, Iterable, Iterator from typing import Any, Callable, Final @@ -43,11 +42,7 @@ from cmk.gui.utils.html import HTML from cmk.gui.utils.transaction_manager import transactions from cmk.gui.utils.urls import makeactionuri, makeuri_contextless -from cmk.gui.validation.visitors.vue_repr import ( - create_vue_visitor, - process_validation_errors, - render_vue, -) +from cmk.gui.validation.visitors.vue_repr import parse_and_validate_vue, render_vue from cmk.gui.valuespec import Checkbox, Transform from cmk.gui.watolib.config_domain_name import ( ABCConfigDomain, @@ -333,16 +328,8 @@ def action(self) -> ActionResult: ) else: new_value = self._valuespec.from_html_vars("ve") - if use_vue_rendering() and request.has_var(self._vue_field_id()): - logger.warning("ACTION PHASE") - value_from_frontend = json.loads( - request.get_str_input_mandatory(self._vue_field_id()) - ) - vue_visitor = create_vue_visitor( - self._valuespec, value_from_frontend, do_validate=True - ) - process_validation_errors(vue_visitor) - new_value = vue_visitor.raw_value + if use_vue_rendering(): + new_value = parse_and_validate_vue(self._valuespec, self._vue_field_id()) self._valuespec.validate_value(new_value, "ve") self._current_settings[self._varname] = new_value @@ -408,16 +395,8 @@ def page(self) -> None: html.tt(self._varname) if use_vue_rendering(): - if request.has_var(self._vue_field_id()): - visitor_value = json.loads(request.get_str_input_mandatory(self._vue_field_id())) - do_validate = True - else: - visitor_value = value - do_validate = False forms.section(_("Current setting as VUE")) - render_vue( - self._valuespec, self._vue_field_id(), visitor_value, do_validate=do_validate - ) + render_vue(self._valuespec, self._vue_field_id(), value) forms.section(_("Current setting")) self._valuespec.render_input("ve", value) diff --git a/cmk/gui/wato/pages/rulesets.py b/cmk/gui/wato/pages/rulesets.py index c645b41f561..c7dd3a593af 100644 --- a/cmk/gui/wato/pages/rulesets.py +++ b/cmk/gui/wato/pages/rulesets.py @@ -62,11 +62,7 @@ from cmk.gui.utils.output_funnel import output_funnel from cmk.gui.utils.transaction_manager import transactions from cmk.gui.utils.urls import DocReference, make_confirm_delete_link, makeuri, makeuri_contextless -from cmk.gui.validation.visitors.vue_repr import ( - create_vue_visitor, - process_validation_errors, - render_vue, -) +from cmk.gui.validation.visitors.vue_repr import parse_and_validate_vue, render_vue from cmk.gui.valuespec import ( Checkbox, Dictionary, @@ -1903,17 +1899,16 @@ def _update_rule_from_vars(self) -> None: self._rule.update_conditions(self._get_rule_conditions_from_vars()) # VALUE - value = self._ruleset.valuespec().from_html_vars("ve") - if use_vue_rendering() and request.has_var(self._vue_field_id()): - value_from_frontend = json.loads(request.get_str_input_mandatory(self._vue_field_id())) - vue_visitor = create_vue_visitor( - self._ruleset.valuespec(), value_from_frontend, do_validate=True - ) - process_validation_errors(vue_visitor) - value = vue_visitor.raw_value + value = parse_and_validate_vue(self._ruleset.valuespec(), self._vue_field_id()) + # For testing, validate this datatype/value again within legacy valuespec + # This should not throw any errors + self._ruleset.valuespec().validate_datatype(value, "ve") + self._ruleset.valuespec().validate_value(value, "ve") + else: + value = self._ruleset.valuespec().from_html_vars("ve") + self._ruleset.valuespec().validate_value(value, "ve") - self._ruleset.valuespec().validate_value(value, "ve") self._rule.value = value def _get_condition_type_from_vars(self) -> str | None: @@ -1983,24 +1978,11 @@ def page(self) -> None: forms.section() html.prevent_password_auto_completion() try: - valuespec.validate_datatype(self._rule.value, "ve") if use_vue_rendering(): - if request.has_var(self._vue_field_id()): - visitor_value = json.loads( - request.get_str_input_mandatory(self._vue_field_id()) - ) - do_validate = True - else: - visitor_value = self._rule.value - do_validate = False forms.section(_("Current setting as VUE")) - render_vue( - self._ruleset.valuespec(), - self._vue_field_id(), - visitor_value, - do_validate=do_validate, - ) + render_vue(self._ruleset.valuespec(), self._vue_field_id(), self._rule.value) + valuespec.validate_datatype(self._rule.value, "ve") valuespec.render_input("ve", self._rule.value) except Exception as e: if active_config.debug: