def update_attribute(attr_id=None): with argument_required("attr_name"): attr_name = request.values.get("attr_name") attr_alias = request.values.get("attr_alias", attr_name) choice_value = request.values.get("choice_value") is_multivalue = request.values.get("is_multivalue", False) is_uniq = request.values.get("is_uniq", False) value_type = request.values.get("value_type", "text") try: is_multivalue = int(is_multivalue) is_uniq = int(is_uniq) except ValueError: raise InvalidUsageError("argument format is error") attr_manager = AttributeManager() kwargs = { "choice_value": choice_value, "is_multivalue": is_multivalue, "is_uniq": is_uniq, "value_type": value_type } ret, res = attr_manager.update(attr_id, attr_name, attr_alias, **kwargs) if not ret: return abort(500, res) return jsonify(attr_id=res)
def get_attributes_by_type_id(self, type_id): attrs = CITypeAttributeCache.get(type_id) attr_manager = AttributeManager() result = list() for attr in attrs: attr_dict = attr_manager.get_attribute_by_id(attr.attr_id) attr_dict["is_required"] = attr.is_required result.append(attr_dict) return result
def get_attribute(attr_name=None, attr_id=None): attr_manager = AttributeManager() attr_dict = None if attr_name is not None: attr_dict = attr_manager.get_attribute_by_name(attr_name) if attr_dict is None: attr_dict = attr_manager.get_attribute_by_alias(attr_name) elif attr_id is not None: attr_dict = attr_manager.get_attribute_by_id(attr_id) if attr_dict is not None: return jsonify(attribute=attr_dict) abort(404, "attribute not found")
def _validate(self, attr, value, table, ci_id): converter = type_map.get("converter").get(attr.value_type) try: v = converter(value) except ValueError: return False, "attribute value {0} converter fail".format(value) if attr.is_choice: choice_list = AttributeManager()._get_choice_value( attr.attr_id, attr.value_type) if v not in choice_list: return False, "{0} is not existed in choice values".format( value) elif attr.is_uniq: old_value = db.session.query( table.attr_id).filter(table.attr_id == attr.attr_id).filter( table.value == v).filter(table.ci_id != ci_id).first() if old_value is not None: return False, "attribute {0} value {1} must be unique".format( attr.attr_name, value) return True, v
def delete_attribute(attr_id=None): attr_manager = AttributeManager() res = attr_manager.delete(attr_id) return jsonify(message="attribute {0} deleted".format(res))
def get_attributes(): q = request.values.get("q") attrs = AttributeManager().get_attributes(name=q) count = len(attrs) return jsonify(numfound=count, attributes=attrs)