def _bulk_collect_cleaned_attributes(self): to_clean = [] for attr in host_attribute_registry.attributes(): attrname = attr.name() if html.get_checkbox("_clean_" + attrname) is True: to_clean.append(attrname) return to_clean
def remove_unused_vars( form_prefix: str, is_var_to_delete: Callable[[str, str, str], bool] = lambda prefix, varname, value: True, ) -> None: """Delete all variables for a form with prefix "form_prefix" that are not activated by a "varname_USE" entry. * simple example: 'search_p_fulltext_USE':'on' 'search_p_fulltext':'my search text' * is_var_to_delete: determines variables to keep """ checkboxes, variables = set(), {} for varname, value in html.request.itervars(form_prefix): if varname.endswith("_USE"): checkboxes.add(varname) continue variables[varname] = value active_prefixes = { active_checkbox.rsplit("_USE")[0] for active_checkbox in checkboxes if html.get_checkbox(active_checkbox) } for varname, value in variables.items(): if not any(varname.startswith(p) for p in active_prefixes) or is_var_to_delete( form_prefix, varname, value ): html.request.del_var(varname)
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 from_html_vars(self, varprefix: str) -> dict[str, Any]: self.load_data() cgs = self._vs_contactgroups().from_html_vars(varprefix + self.name()) return { "groups": cgs, "recurse_perms": html.get_checkbox(varprefix + self.name() + "_recurse_perms"), "use": html.get_checkbox(varprefix + self.name() + "_use"), "use_for_services": html.get_checkbox(varprefix + self.name() + "_use_for_services"), "recurse_use": html.get_checkbox(varprefix + self.name() + "_recurse_use"), }
def _from_vars(self): self._start = bool(request.var("_start")) # 'all' not set -> only scan checked hosts in current folder, no recursion # otherwise: all host in this folder, maybe recursively self._all = bool(request.var("all")) self._complete_folder = self._all # Ignored during initial form display self._settings = ParentScanSettings( where=request.get_ascii_input_mandatory("where", "subfolder"), alias=request.get_str_input_mandatory("alias", "").strip(), recurse=html.get_checkbox("recurse") or False, select=request.get_ascii_input_mandatory("select", "noexplicit"), timeout=request.get_integer_input_mandatory("timeout", 8), probes=request.get_integer_input_mandatory("probes", 2), max_ttl=request.get_integer_input_mandatory("max_ttl", 10), force_explicit=html.get_checkbox("force_explicit") or False, ping_probes=request.get_integer_input_mandatory("ping_probes", 5), ) self._job = ParentScanBackgroundJob()
def _add_extra_attrs_from_html_vars(self): self._attr["user_editable"] = html.get_checkbox("user_editable")
def action(self) -> ActionResult: # TODO: remove subclass specific things specifict things (everything with _type == 'user') if not transactions.check_transaction(): return None title = request.get_str_input_mandatory("title").strip() if not title: raise MKUserError("title", _("Please specify a title.")) for this_attr in self._attrs: if title == this_attr["title"] and self._name != this_attr["name"]: raise MKUserError( "alias", _("This alias is already used by the attribute %s.") % this_attr["name"], ) topic = request.get_str_input_mandatory("topic", "").strip() help_txt = request.get_str_input_mandatory("help", "").strip() show_in_table = html.get_checkbox("show_in_table") add_custom_macro = html.get_checkbox("add_custom_macro") if self._new: self._name = request.get_ascii_input_mandatory("name", "").strip() if not self._name: raise MKUserError("name", _("Please specify a name for the new attribute.")) if " " in self._name: raise MKUserError("name", _("Sorry, spaces are not allowed in attribute names.")) if not re.match("^[-a-z0-9A-Z_]*$", self._name): raise MKUserError( "name", _( "Invalid attribute name. Only the characters a-z, A-Z, 0-9, _ and - are allowed." ), ) if [a for a in self._attrs if a["name"] == self._name]: raise MKUserError("name", _("Sorry, there is already an attribute with that name.")) ty = request.get_ascii_input_mandatory("type", "").strip() if ty not in [t[0] for t in custom_attr_types()]: raise MKUserError("type", _("The choosen attribute type is invalid.")) self._attr = { "name": self._name, "type": ty, } self._attrs.append(self._attr) _changes.add_change( "edit-%sattr" % self._type, _("Create new %s attribute %s") % (self._type, self._name), ) else: _changes.add_change( "edit-%sattr" % self._type, _("Modified %s attribute %s") % (self._type, self._name) ) self._attr.update( { "title": title, "topic": topic, "help": help_txt, "show_in_table": show_in_table, "add_custom_macro": add_custom_macro, } ) self._add_extra_attrs_from_html_vars() save_custom_attrs_to_mk_file(self._all_attrs) self._update_config() return redirect(mode_url(self._type + "_attrs"))