How to use the extensions.db.session function in extensions

To help you get started, we’ve selected a few extensions examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github dlapiduz / govcode.org / gh_import.py View on Github external
def get_or_create_user(self, user):
        u = User.query.filter_by(gh_id=user.id).first()
        if u is None:
            u = User()
            u.gh_id = user.id
            u.login = user.login
            u.avatar_url = user.avatar_url
            db.session.add(u)
            db.session.commit()
        return u
github pycook / cmdb / lib / ci_type.py View on Github external
CITypeAttribute.attr_id == uniq_key.attr_id).first()
            if citype_attr is None:
                citype_attr = CITypeAttribute()
                citype_attr.attr_id = uniq_key.attr_id
                citype_attr.type_id = type_id
            citype_attr.is_required = True
            db.session.add(citype_attr)
        if type_name:
            citype.type_name = type_name
        if type_alias:
            citype.type_alias = type_alias
        if icon_url:
            citype.icon_url = icon_url
        if enabled is not None:
            citype.enabled = enabled
        db.session.add(citype)
        try:
            db.session.commit()
        except Exception as e:
            db.session.rollback()
            current_app.logger.error("add CIType is error, {0}".format(str(e)))
            return False, str(e)
        CITypeCache.clean(type_id)
        return True, type_id
github pycook / cmdb / lib / ci.py View on Github external
def delete(self, cr_id):
        cr = db.session.query(CIRelation).filter(
            CIRelation.cr_id == cr_id).first()
        cr_id = cr.cr_id
        first_ci = cr.first_ci_id
        second_ci = cr.second_ci_id
        if cr is not None:
            db.session.delete(cr)
            try:
                db.session.commit()
            except Exception as e:
                db.session.rollback()
                current_app.logger.error(
                    "delete CIRelation is error, {0}".format(str(e)))
                return abort(
                    400, "delete CIRelation is error, {0}".format(str(e)))
            his_manager = CIRelationHistoryManager()
            his_manager.add(cr_id, first_ci, second_ci,
                            cr.relation_type, operate_type="delete")
            return True
        return abort(404, "CI relation is not existed")
github dlapiduz / govcode.org / gh_import.py View on Github external
for commit in commit_pages.iterator():
            if last_commit and commit.sha == last_commit.sha:
                print 'Next'
                break
            c = Commit.query.filter_by(sha=commit.sha).first()
            if c is None:
                c = Commit()
                c.sha = commit.sha
                c.message = commit.commit.message
                c.repository = repo
                c.date = commit.commit.author.date
                if commit.author:
                    c.user = self.get_or_create_user(commit.author)
                print 'New commit: ' + c.sha
                db.session.add(c)
                db.session.commit()

        repo.last_commit_date = repo.last_commit.date
        db.session.commit()
github pycook / cmdb / models / attribute.py View on Github external
def get(cls, key):
        if key is None:
            return
        attr = cache.get('Field::Name::%s' % key) or \
               cache.get('Field::ID::%s' % key) or \
               cache.get('Field::Alias::%s' % key)
        if attr is None:
            attr = db.session.query(CIAttribute).filter_by(
                attr_name=key).first() or \
                   db.session.query(CIAttribute).filter(
                       CIAttribute.attr_id == key).first() or \
                   db.session.query(CIAttribute).filter(
                       CIAttribute.attr_alias == key).first()
            db.session.close()
            if attr is not None:
                CIAttributeCache.set(attr)
        return attr
github pycook / cmdb / lib / ci.py View on Github external
for field in fields:
            attr = CIAttributeCache.get(field)
            if attr is not None:
                _fields.append(str(attr.attr_id))
        _fields = "WHERE A.attr_id in ({0})".format(",".join(_fields))
    ci_ids = ",".join(ci_ids)
    if value_tables is None:
        value_tables = type_map["table_name"].values()
    current_app.logger.debug(value_tables)
    value_sql = " UNION ".join([QUERY_CIS_BY_VALUE_TABLE.format(value_table,
                                                                ci_ids)
                                for value_table in value_tables])
    query_sql = QUERY_CIS_BY_IDS.format(ci_ids, _fields, value_sql)
    current_app.logger.debug(query_sql)
    start = time.time()
    hosts = db.session.execute(query_sql).fetchall()
    current_app.logger.info("get cis time is: {0}".format(
        time.time() - start))

    ci_list = set()
    res = list()
    ci_dict = dict()
    start = time.time()
    for ci_id, type_id, attr_id, attr_name, \
            attr_alias, value, value_type, is_multivalue in hosts:
        if ci_id not in ci_list:
            ci_dict = dict()
            ci_type = CITypeSpecCache.get(type_id)
            ci_dict["_id"] = ci_id
            ci_dict["_type"] = type_id
            ci_dict["ci_type"] = ci_type.type_name
            ci_dict["ci_type_alias"] = ci_type.type_alias
github pycook / cmdb / models / attribute.py View on Github external
def get(cls, key):
        if key is None:
            return
        attr = cache.get('Field::Name::%s' % key) or \
               cache.get('Field::ID::%s' % key) or \
               cache.get('Field::Alias::%s' % key)
        if attr is None:
            attr = db.session.query(CIAttribute).filter_by(
                attr_name=key).first() or \
                   db.session.query(CIAttribute).filter(
                       CIAttribute.attr_id == key).first() or \
                   db.session.query(CIAttribute).filter(
                       CIAttribute.attr_alias == key).first()
            db.session.close()
            if attr is not None:
                CIAttributeCache.set(attr)
        return attr