Skip to content

Commit

Permalink
moved vue internal code into vue_repr.py
Browse files Browse the repository at this point in the history
Change-Id: I1092270956a125d2fb9dfa2ddf6ce475ecea0606
  • Loading branch information
schnetzzz committed Nov 7, 2023
1 parent 3de4236 commit 17eec29
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 65 deletions.
41 changes: 30 additions & 11 deletions cmk/gui/validation/visitors/vue_repr.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
29 changes: 4 additions & 25 deletions cmk/gui/wato/pages/global_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
settings"""

import abc
import json
from collections.abc import Collection, Iterable, Iterator
from typing import Any, Callable, Final

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 11 additions & 29 deletions cmk/gui/wato/pages/rulesets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 17eec29

Please sign in to comment.