def _get_search_vars(self) -> HTTPVariables: search_vars = {} if request.has_var("host_search_host"): search_vars[ "host_search_host"] = request.get_ascii_input_mandatory( "host_search_host") for varname, value in request.itervars(prefix="host_search_change_"): if html.get_checkbox(varname) is False: continue search_vars[varname] = value attr_ident = varname.split("host_search_change_", 1)[1] # The URL variable naming scheme is not clear. Try to match with "attr_" prefix # and without. We should investigate and clean this up. attr_prefix = "host_search_attr_%s" % attr_ident search_vars.update(request.itervars(prefix=attr_prefix)) attr_prefix = "host_search_%s" % attr_ident search_vars.update(request.itervars(prefix=attr_prefix)) for varname, value in request.itervars(): if varname.startswith(("_", "host_search_")) or varname == "mode": continue search_vars[varname] = value search_vars["mode"] = "folder" return list(search_vars.items())
def _get_audit_log_options_from_request(self): options = {} for name, vs in self._audit_log_options(): if not list(request.itervars("options_" + name)): continue try: value = vs.from_html_vars("options_" + name) vs.validate_value(value, "options_" + name) options[name] = value except MKUserError as e: user_errors.add(e) return options
def _open_csv_file(self) -> CSVReader: try: csv_file = self._file_path().open(encoding="utf-8") except IOError: raise MKUserError( None, _("Failed to read the previously uploaded CSV file. Please upload it again.") ) if list(request.itervars(prefix="_preview")): params = self._vs_parse_params().from_html_vars("_preview") else: params = { "has_title_line": True, } self._vs_parse_params().validate_value(params, "_preview") self._params = params assert self._params is not None self._has_title_line = self._params.get("has_title_line", False) # try to detect the CSV format to be parsed if "field_delimiter" in params: csv_dialect = self._get_custom_csv_dialect(params["field_delimiter"]) else: try: csv_dialect = csv.Sniffer().sniff(csv_file.read(2048), delimiters=",;\t:") csv_file.seek(0) except csv.Error as e: if "Could not determine delimiter" in str(e): # Failed to detect the CSV files field delimiter character. Using ";" now. If # you need another one, please specify it manually. csv_dialect = self._get_custom_csv_dialect(";") csv_file.seek(0) else: raise return csv.reader(csv_file, csv_dialect)
def render_input(self, varprefix: str, value: dict[str, Any]) -> None: # The display mode differs when the valuespec is activated vars_copy = dict(request.itervars()) # The timeperiod mode can be set by either the GUI switch or by the value itself # GUI switch overrules the information stored in the value if request.has_var(self.tp_toggle_var): is_active = self._is_switched_on() else: is_active = self.is_active(value) # Set the actual used mode html.hidden_field(self.tp_current_mode, "%d" % is_active) vars_copy[self.tp_toggle_var] = "%d" % (not is_active) url_vars: HTTPVariables = [] url_vars += vars_copy.items() toggle_url = makeuri(request, url_vars) if is_active: value = self._get_timeperiod_value(value) self._get_timeperiod_valuespec().render_input(varprefix, value) html.buttonlink( toggle_url, _("Disable timespecific parameters"), class_=["toggle_timespecific_parameter"], ) else: value = self._get_timeless_value(value) self._enclosed_valuespec.render_input(varprefix, value) html.buttonlink( toggle_url, _("Enable timespecific parameters"), class_=["toggle_timespecific_parameter"], )
def action(self) -> ActionResult: if html.form_submitted("search"): return None alias = request.get_str_input_mandatory("alias") unique, info = groups.is_alias_used("roles", self._role_id, alias) if not unique: assert info is not None raise MKUserError("alias", info) new_id = request.get_ascii_input_mandatory("id") if not new_id: raise MKUserError("id", "You have to provide a ID.") if not re.match("^[-a-z0-9A-Z_]*$", new_id): raise MKUserError( "id", _("Invalid role ID. Only the characters a-z, A-Z, 0-9, _ and - are allowed.") ) if new_id != self._role_id: if new_id in self._roles: raise MKUserError("id", _("The ID is already used by another role")) self._role["alias"] = alias # based on if not self._role.get("builtin"): basedon = request.get_ascii_input_mandatory("basedon") if basedon not in builtin_role_ids: raise MKUserError( "basedon", _("Invalid valid for based on. Must be id of builtin rule.") ) self._role["basedon"] = basedon # Permissions permissions = self._role["permissions"] for var_name, value in request.itervars(prefix="perm_"): try: perm = permission_registry[var_name[5:]] except KeyError: continue if value == "yes": permissions[perm.name] = True elif value == "no": permissions[perm.name] = False elif value == "default": try: del permissions[perm.name] except KeyError: pass # Already at defaults if self._role_id != new_id: self._roles[new_id] = self._role del self._roles[self._role_id] self._rename_user_role(self._role_id, new_id) self._save_roles() _changes.add_change( "edit-roles", _("Modified user role '%s'") % new_id, sites=get_login_sites() ) return redirect(mode_url("roles"))