Пример #1
0
    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
Пример #2
0
    def update(self, _id, **kwargs):
        attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id))

        if kwargs.get("name"):
            other = Attribute.get_by(name=kwargs['name'], first=True, to_dict=False)
            if other and other.id != attr.id:
                return abort(400, "Attribute name <{0}> cannot be duplicate!".format(kwargs['name']))
        if kwargs.get("alias"):
            other = Attribute.get_by(alias=kwargs['alias'], first=True, to_dict=False)
            if other and other.id != attr.id:
                return abort(400, "Attribute alias <{0}> cannot be duplicate!".format(kwargs['alias']))

        choice_value = kwargs.pop("choice_value", False)
        is_choice = True if choice_value else False
        kwargs['is_choice'] = is_choice

        attr.update(flush=True, **kwargs)

        if is_choice:
            self._add_choice_values(attr.id, attr.value_type, choice_value)
        else:
            self._del_choice_values(attr.id, attr.value_type)

        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error("update attribute error, {0}".format(str(e)))
            return abort(400, "update attribute <{0}> failed".format(_id))

        AttributeCache.clean(attr)

        return attr.id
Пример #3
0
    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
Пример #4
0
    def delete(_id):
        attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id))
        name = attr.name

        if attr.is_choice:
            choice_table = type_map["choice"].get(attr.value_type)
            db.session.query(choice_table).filter(choice_table.attr_id == _id).delete()  # FIXME: session conflict
            db.session.flush()

        AttributeCache.clean(attr)

        attr.soft_delete()

        for i in CITypeAttribute.get_by(attr_id=_id, to_dict=False):
            i.soft_delete()

        for i in PreferenceShowAttributes.get_by(attr_id=_id, to_dict=False):
            i.soft_delete()

        return name
Пример #5
0
    def update(self, _id, **kwargs):
        attr = Attribute.get_by_id(_id) or abort(404, "Attribute <{0}> does not exist".format(_id))

        choice_value = kwargs.pop("choice_value", False)
        is_choice = True if choice_value else False

        attr.update(flush=True, **kwargs)

        if is_choice:
            self._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("update attribute error, {0}".format(str(e)))
            return abort(400, "update attribute <{0}> failed".format(_id))

        AttributeCache.clean(attr)

        return attr.id