예제 #1
0
파일: api.py 프로젝트: archlevel/knowing
def solr_delete(sakuya_db, id):

    row = sakuya_db.query(Charts).filter_by(id=id, cate_id=SOLR_CATE_ID).first()
    if row is None:
        return util.output('error', msg='Chart not found.')

    sakuya_db.delete(row)
    sakuya_db.commit()

    storm.delete_rule(id)

    return util.output('ok')
예제 #2
0
파일: api.py 프로젝트: archlevel/knowing
def solr_delete(sakuya_db, id):

    row = sakuya_db.query(Charts).filter_by(id=id,
                                            cate_id=SOLR_CATE_ID).first()
    if row is None:
        return util.output('error', msg='Chart not found.')

    sakuya_db.delete(row)
    sakuya_db.commit()

    storm.delete_rule(id)

    return util.output('ok')
예제 #3
0
파일: chart.py 프로젝트: archlevel/knowing
def delete(sakuya_db, id):

    row = sakuya_db.query(Charts).get(id)
    if row is None:
        abort(404)

    if haopan.is_haopan(row.cate_id):
        abort(404)

    cid = row.cate_id
    sakuya_db.delete(row)
    sakuya_db.query(Follows).filter_by(cid=id).delete()
    sakuya_db.commit()

    process_warn_rules(sakuya_db, id, [])

    storm.delete_rule(id)

    return redirect('/admin/category/%d' % cid)
예제 #4
0
파일: chart.py 프로젝트: archlevel/knowing
def delete(sakuya_db, id):

    row = sakuya_db.query(Charts).get(id)
    if row is None:
        abort(404)

    if haopan.is_haopan(row.cate_id):
        abort(404)

    cid = row.cate_id
    sakuya_db.delete(row)
    sakuya_db.query(Follows).filter_by(cid=id).delete()
    sakuya_db.commit()

    process_warn_rules(sakuya_db, id, [])

    storm.delete_rule(id)

    return redirect('/admin/category/%d' % cid)
예제 #5
0
파일: chart.py 프로젝트: archlevel/knowing
def update(sakuya_db, id):

    row = sakuya_db.query(Charts).get(id)
    if row is None:
        abort(404)

    is_haopan = haopan.is_haopan(row.cate_id)

    if auth.is_role('sa'):
        warn_only = False
    elif auth.is_role('haopan') and is_haopan:
        warn_only = True
    elif auth.get_user()['username'] == row.owner:
        warn_only = True
    else:
        abort(403)

    rule = None
    warn = None

    def error(msg):
        rt = _rt(sakuya_db)
        rt['id'] = id
        rt['editing'] = True
        rt['warn_only'] = warn_only
        rt['is_haopan'] = is_haopan
        rt['forms'] = request.forms
        rt['forms']['rule'] = dump_rule(rule)
        rt['forms']['warn'] = dump_warn(warn)
        rt['error_msg'] = msg
        return rt

    try:

        alert_enable = request.forms['alert_enable'] == '1'
        warn = make_warn()
        if alert_enable and not warn:
            raise ValueError

        if not warn_only and not is_haopan:

            use_storm = request.forms['use_storm'] == '1'
            if use_storm:
                rule = make_rule()
                if not rule['datasource'] or not rule['filters']:
                    raise ValueError

            name = request.forms['name']
            if not name:
                raise ValueError

            cid = int(request.forms['cid'])
            if not sakuya_db.\
                   query(Categories).\
                   filter_by(id=cid, is_parent=False).\
                   count():
                raise ValueError

            owner = request.forms['owner']
            if not sakuya_db.\
                   query(Users).\
                   filter_by(username=owner).\
                   count():
                raise ValueError

    except Exception:
        return error('参数错误。')

    try:
        row.alert_enable = int(alert_enable)
        row.warnings = 0
        row.criticals = 0
        process_warn_rules(sakuya_db, id, warn)

        if not warn_only and not is_haopan:
            row.name = name
            row.cate_id = cid
            row.root_category = get_parent_category(sakuya_db, cid)
            row.owner = owner

            ext = row.get_ext()
            if use_storm:
                ext['rule'] = rule
                storm.set_rule(id, json.dumps(rule))
            else:
                ext.pop('rule', None)
                storm.delete_rule(id)
            row.ext_info = json.dumps(ext)

        sakuya_db.commit()

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return error('图表更新失败。')

    if warn_only or is_haopan:
        redirect('/chart/%d' % id)
    redirect('/admin/category/%d' % cid)
예제 #6
0
파일: chart.py 프로젝트: archlevel/knowing
def update(sakuya_db, id):

    row = sakuya_db.query(Charts).get(id)
    if row is None:
        abort(404)

    is_haopan = haopan.is_haopan(row.cate_id)

    if auth.is_role('sa'):
        warn_only = False
    elif auth.is_role('haopan') and is_haopan:
        warn_only = True
    elif auth.get_user()['username'] == row.owner:
        warn_only = True
    else:
        abort(403)

    rule = None
    warn = None

    def error(msg):
        rt = _rt(sakuya_db)
        rt['id'] = id
        rt['editing'] = True
        rt['warn_only'] = warn_only
        rt['is_haopan'] = is_haopan
        rt['forms'] = request.forms
        rt['forms']['rule'] = dump_rule(rule)
        rt['forms']['warn'] = dump_warn(warn)
        rt['error_msg'] = msg
        return rt

    try:

        alert_enable = request.forms['alert_enable'] == '1'
        warn = make_warn()
        if alert_enable and not warn:
            raise ValueError

        if not warn_only and not is_haopan:

            use_storm = request.forms['use_storm'] == '1'
            if use_storm:
                rule = make_rule()
                if not rule['datasource'] or not rule['filters']:
                    raise ValueError

            name = request.forms['name']
            if not name:
                raise ValueError

            cid = int(request.forms['cid'])
            if not sakuya_db.\
                   query(Categories).\
                   filter_by(id=cid, is_parent=False).\
                   count():
                raise ValueError

            owner = request.forms['owner']
            if not sakuya_db.\
                   query(Users).\
                   filter_by(username=owner).\
                   count():
                raise ValueError

    except Exception:
        return error('参数错误。')

    try:
        row.alert_enable = int(alert_enable)
        row.warnings = 0
        row.criticals = 0
        process_warn_rules(sakuya_db, id, warn)

        if not warn_only and not is_haopan:
            row.name = name
            row.cate_id = cid
            row.root_category = get_parent_category(sakuya_db, cid)
            row.owner = owner

            ext = row.get_ext()
            if use_storm:
                ext['rule'] = rule
                storm.set_rule(id, json.dumps(rule))
            else:
                ext.pop('rule', None)
                storm.delete_rule(id)
            row.ext_info = json.dumps(ext)

        sakuya_db.commit()

    except Exception:
        traceback.print_exc()
        sakuya_db.rollback()
        return error('图表更新失败。')

    if warn_only or is_haopan:
        redirect('/chart/%d' % id)
    redirect('/admin/category/%d' % cid)