Пример #1
0
    def add(self, type_id, attr_ids=None, is_required=False):
        """
        add attributes to CIType, attr_ids are list
        """
        if not attr_ids or not isinstance(attr_ids, list):
            return abort(500, "attr_ids must be required")
        ci_type = CITypeCache.get(type_id)
        if ci_type is None:
            return abort(404, "CIType ID({0}) is not existed".format(type_id))
        for attr_id in attr_ids:
            attr = CIAttributeCache.get(attr_id)
            if attr is None:
                return abort(404,
                             "attribute id {0} is not existed".format(attr_id))
            existed = db.session.query(CITypeAttribute.attr_id).filter_by(
                type_id=type_id).filter_by(attr_id=attr_id).first()
            if existed is not None:
                continue
            current_app.logger.debug(attr_id)
            db.session.add(CITypeAttribute(
                type_id=type_id, attr_id=attr_id, is_required=is_required))
        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(
                "add attribute to CIType is error, {0}".format(str(e)))
            return abort(
                500, "add attribute to CIType is error, maybe duplicate entry")

        CITypeAttributeCache.clean(type_id)
        return True
Пример #2
0
 def delete(self, type_id, attr_ids=None):
     """
     delete attributes at CIType, attr_ids are list
     """
     if not attr_ids or not isinstance(attr_ids, list):
         return abort(
             500, "delete attribute of CIType, attr_ids must be required")
     ci_type = CITypeCache.get(type_id)
     if ci_type is None:
         return abort(
             404, "CIType ID({0}) is not existed".format(type_id))
     for attr_id in attr_ids:
         attr = CIAttributeCache.get(attr_id)
         if attr is None:
             return abort(
                 404, "attribute id {0} is not existed".format(attr_id))
         db.session.query(CITypeAttribute).filter_by(
             type_id=type_id).filter_by(attr_id=attr_id).delete()
     try:
         db.session.commit()
     except Exception as e:
         db.session.rollback()
         current_app.logger.error(
             "delete attributes of CIType is error, {0}".format(str(e)))
         return abort(500, "delete attributes of CIType is error")
     CITypeAttributeCache.clean(type_id)
     return True
Пример #3
0
def get_attributes_by_type(type_id=None, type_name=None):
    manager = CITypeAttributeManager()
    from models.attribute import CIAttributeCache
    from models.ci_type import CITypeCache
    from models.ci_type import CITypeAttributeCache

    t = CITypeCache.get(type_id)
    if not t:
        t = CITypeCache.get(type_name)
        if not t:
            return abort(400, "CIType {0} is not existed".format(type_id))
        type_id = t.type_id
    uniq_id = t.uniq_id
    CITypeAttributeCache.clean(type_id)
    unique = CIAttributeCache.get(uniq_id).attr_name
    return jsonify(attributes=manager.get_attributes_by_type_id(type_id),
                   type_id=type_id, uniq_id=uniq_id, unique=unique)
Пример #4
0
 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