def add(cls, **kwargs): choice_value = kwargs.pop("choice_value", []) kwargs.pop("is_choice", None) is_choice = True if choice_value else False name = kwargs.pop("name") alias = kwargs.pop("alias", "") alias = name if not alias else alias Attribute.get_by(name=name, first=True) and abort(400, "attribute {0} is already existed".format(name)) attr = Attribute.create(flush=True, name=name, alias=alias, is_choice=is_choice, **kwargs) if choice_value: cls._add_choice_values(attr.id, attr.value_type, choice_value) try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error("add attribute error, {0}".format(str(e))) return abort(400, "add attribute <{0}> failed".format(name)) AttributeCache.clean(attr) return attr.id
def init_attributes(num=1): attrs = [] for i in range(num): attrs.append( Attribute.create(name=uuid.uuid4().hex[:8], alias=uuid.uuid4().hex[:8], value_type=str(random.randint(0, 100) % 3))) return attrs
def add(cls, **kwargs): choice_value = kwargs.pop("choice_value", []) kwargs.pop("is_choice", None) is_choice = True if choice_value else False name = kwargs.pop("name") alias = kwargs.pop("alias", "") alias = name if not alias else alias Attribute.get_by(name=name, first=True) and abort( 400, "attribute name <{0}> is duplicated".format(name)) Attribute.get_by(alias=alias, first=True) and abort( 400, "attribute alias <{0}> is duplicated".format(name)) attr = Attribute.create(flush=True, name=name, alias=alias, is_choice=is_choice, **kwargs) if choice_value: cls._add_choice_values(attr.id, attr.value_type, choice_value) try: db.session.commit() except Exception as e: db.session.rollback() current_app.logger.error("add attribute error, {0}".format(str(e))) return abort(400, "add attribute <{0}> failed".format(name)) AttributeCache.clean(attr) if current_app.config.get("USE_ES"): from api.extensions import es other = dict() other['index'] = True if attr.is_index else False if attr.value_type == ValueTypeEnum.TEXT: other['analyzer'] = 'ik_max_word' other['search_analyzer'] = 'ik_smart' if attr.is_index: other["fields"] = { "keyword": { "type": "keyword", "ignore_above": 256 } } es.update_mapping(name, ValueTypeMap.es_type[attr.value_type], other) return attr.id