def _delete_aux_tag(self) -> ActionResult: del_id = request.get_item_input( "_del_aux", dict(self._tag_config.aux_tag_list.get_choices()))[1] # Make sure that this aux tag is not begin used by any tag group for group in self._tag_config.tag_groups: for grouped_tag in group.tags: if del_id in grouped_tag.aux_tag_ids: raise MKUserError( None, _("You cannot delete this auxiliary tag. " "It is being used in the tag group <b>%s</b>.") % group.title, ) message = _rename_tags_after_confirmation( self.breadcrumb(), OperationRemoveAuxTag(del_id)) if message is False: return FinalizeRequest(code=200) if message: self._tag_config.aux_tag_list.remove(del_id) try: self._tag_config.validate_config() except MKGeneralException as e: raise MKUserError(None, "%s" % e) self._save_tags_and_update_hosts( self._tag_config.get_dict_format()) add_change("edit-tags", _("Removed auxiliary tag %s (%s)") % (message, del_id)) if isinstance(message, str): flash(message) return redirect(mode_url("tags"))
def _delete_tag_group(self) -> ActionResult: del_id = request.get_item_input( "_delete", dict(self._tag_config.get_tag_group_choices()))[1] if not request.has_var( "_repair") and self._is_cleaning_up_user_tag_group_to_builtin( del_id): message: Union[bool, str] = ( _('Transformed the user tag group "%s" to builtin.') % del_id) else: message = _rename_tags_after_confirmation( self.breadcrumb(), OperationRemoveTagGroup(del_id)) if message is False: return FinalizeRequest(code=200) if message: self._tag_config.remove_tag_group(del_id) try: self._tag_config.validate_config() except MKGeneralException as e: raise MKUserError(None, "%s" % e) self._save_tags_and_update_hosts( self._tag_config.get_dict_format()) add_change("edit-tags", _("Removed tag group %s (%s)") % (message, del_id)) if isinstance(message, str): flash(message) return redirect(mode_url("tags"))
def action(self) -> ActionResult: if not transactions.check_transaction(): return redirect(mode_url("tags")) vs = self._valuespec() tag_group_spec = vs.from_html_vars("tag_group") vs.validate_value(tag_group_spec, "tag_group") # Create new object with existing host tags changed_hosttags_config = cmk.utils.tags.TagConfig.from_config( self._tag_config_file.load_for_modification()) changed_tag_group = cmk.utils.tags.TagGroup.from_config(tag_group_spec) self._tag_group = changed_tag_group if self._new: # Inserts and verifies changed tag group changed_hosttags_config.insert_tag_group(changed_tag_group) try: changed_hosttags_config.validate_config() except MKGeneralException as e: raise MKUserError(None, "%s" % e) self._save_tags_and_update_hosts( changed_hosttags_config.get_dict_format()) add_change( "edit-hosttags", _("Created new host tag group '%s'") % changed_tag_group.id) flash( _("Created new host tag group '%s'") % changed_tag_group.title) return redirect(mode_url("tags")) # Updates and verifies changed tag group changed_hosttags_config.update_tag_group(changed_tag_group) try: changed_hosttags_config.validate_config() except MKGeneralException as e: raise MKUserError(None, "%s" % e) remove_tag_ids, replace_tag_ids = identify_modified_tags( changed_tag_group, self._untainted_tag_group) tg_id = self._tag_group.id if tg_id is None: raise Exception("tag group ID not set") operation = OperationReplaceGroupedTags(tg_id, remove_tag_ids, replace_tag_ids) # Now check, if any folders, hosts or rules are affected message = _rename_tags_after_confirmation(self.breadcrumb(), operation) if message is False: return FinalizeRequest(code=200) self._save_tags_and_update_hosts( changed_hosttags_config.get_dict_format()) add_change("edit-hosttags", _("Edited host tag group %s (%s)") % (message, self._id)) if isinstance(message, str): flash(message) return redirect(mode_url("tags"))
def action(self) -> ActionResult: if not transactions.check_transaction(): return redirect(self.mode_url()) if not request.var("_delete"): return redirect(self.mode_url()) delname = request.var("_delete") for index, attr in enumerate(self._attrs): if attr["name"] == delname: self._attrs.pop(index) save_custom_attrs_to_mk_file(self._all_attrs) self._update_config() add_change("edit-%sattrs" % self._type, _("Deleted attribute %s") % (delname)) return redirect(self.mode_url())
def _add_change(self, action_name, text): add_change(action_name, text, domains=[watolib.ConfigDomainGUI], sites=get_login_sites())
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) add_change( "edit-%sattr" % self._type, _("Create new %s attribute %s") % (self._type, self._name), ) else: 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"))