Ejemplo n.º 1
0
def delete_article():
    entry_id = util.extract_arg_from_req(request, 'id')
    if entry_id == '':
        app.logger.warning('failed to delete_article, lack of parameter: id',
                           extra=util.req_as_dict(request))
        return util.make_err_json_response(const.ERRCODE_LACK_OF_PARAMETER,
                                           {"param_name": "id"})

    conn = db_util.getConnection(config)
    try:
        errcode = article_srv.delete_article(conn, entry_id)
        if errcode != const.ERRCODE_SUCC:
            app.logger.warning('failed to delete_article, errcode: %d',
                               errcode,
                               extra=util.req_as_dict(request))
            return util.make_err_json_response(errcode)

        conn.commit()
        app.logger.info('delete_article', extra=util.req_as_dict(request))
        resp = {'errcode': const.ERRCODE_SUCC}
        return json.dumps(resp,
                          default=util.defaultSerializer,
                          ensure_ascii=False)

    except Exception, e:
        app.logger.warning('failed to delete_article, exception:%s',
                           str(e),
                           extra=util.req_as_dict(request))
        return util.make_err_json_response(const.ERRCODE_INTERNAL_ERR)
Ejemplo n.º 2
0
def article():
    entry_id = util.extract_arg_from_req(request, 'id')
    if entry_id == '':
        app.logger.warning(
            'failed to get_article_entry, lack of parameter: id',
            extra=util.req_as_dict(request))
        return util.make_err_json_response(const.ERRCODE_LACK_OF_PARAMETER,
                                           {"param_name": "id"})

    conn = db_util.getConnection(config)
    try:
        entry = article_srv.get_article(conn, entry_id)
        if entry == None:
            app.logger.warning(
                'failed to get_article_entry, no such entry: %s',
                entry_id,
                extra=util.req_as_dict(request))
            return util.make_err_json_response(const.ERRCODE_NO_SUCH_ENTRY)

        app.logger.info('get_article_entry', extra=util.req_as_dict(request))
        resp = {'errcode': const.ERRCODE_SUCC, 'result': entry}
        return json.dumps(resp,
                          default=util.defaultSerializer,
                          ensure_ascii=False)

    except Exception, e:
        app.logger.warning('failed to get_article_entry, exception:%s',
                           str(e),
                           extra=util.req_as_dict(request))
        return util.make_err_json_response(const.ERRCODE_INTERNAL_ERR)
Ejemplo n.º 3
0
def load_user(user_id):
    conn = db_util.getConnection(config)
    admin_info = admin_user_svc.getAdminUserInfo(conn, user_id)
    if admin_info == None:
        return None
    login_data = LoginData(user_id, admin_info.get('login_name', ''),
                           admin_info.get('name'))
    return login_data
Ejemplo n.º 4
0
def add_article():
    allowed_param_list = [
        'category_id', 'title', 'image', 'keywords', 'description', 'content'
    ]
    required_param_list = ['title', 'content']

    param_dict = {}
    for p in allowed_param_list:
        value = util.extract_arg_from_req(request, p)
        if value == '':
            if p in required_param_list:
                app.logger.warning(
                    'failed to add_article, lack of parameter: %s',
                    p,
                    extra=util.req_as_dict(request))
                return util.make_err_json_response(
                    const.ERRCODE_LACK_OF_PARAMETER, {"param_name": p})
        else:
            param_dict[p] = value

    conn = db_util.getConnection(config)
    try:
        errcode, new_id = article_srv.add_article(conn, param_dict)
        if errcode != const.ERRCODE_SUCC:
            app.logger.warning('failed to add_article, errcode=%d',
                               errcode,
                               extra=util.req_as_dict(request))
            return util.make_err_json_response(errcode)
        # 提交到数据库执行
        conn.commit()

        app.logger.info('add_article', extra=util.req_as_dict(request))
        resp = {'errcode': const.ERRCODE_SUCC, 'result': {'id': new_id}}
        return json.dumps(resp,
                          default=util.defaultSerializer,
                          ensure_ascii=False)
    except Exception, e:
        # 发生错误时回滚
        conn.rollback()

        app.logger.warning('failed to add_article, exception:%s',
                           str(e),
                           extra=util.req_as_dict(request))
        return util.make_err_json_response(const.ERRCODE_INTERNAL_ERR)
Ejemplo n.º 5
0
def login():
    login_name = util.extract_arg_from_req(request, 'login_name')
    if login_name == '':
        app.logger.warning('failed to login, lack of parameter: login_name',
                           extra=util.req_as_dict(request))
        resp = {'errcode': const.ERRCODE_LACK_OF_PARAMETER,
                'errmsg': const.strerr(const.ERRCODE_LACK_OF_PARAMETER, {"param_name":"login_name"})}
        return json.dumps(resp, ensure_ascii=False)
    
    password = util.extract_arg_from_req(request, 'password')
    if password == '':
        app.logger.warning('failed to login, lack of parameter: password',
                           extra=util.req_as_dict(request))
        resp = {'errcode': const.ERRCODE_LACK_OF_PARAMETER,
                'errmsg': const.strerr(const.ERRCODE_LACK_OF_PARAMETER, {"param_name":"password"})}
        return json.dumps(resp, ensure_ascii=False)
    
    conn = db_util.getConnection(config)
    try:
        errcode, login_data = admin_user_svc.verifyLogin(conn, login_name, password)
        app.logger.debug('%s login', login_name, extra=util.req_as_dict(request))
            
        if errcode != const.ERRCODE_SUCC:
            app.logger.warning('failed to login, errcode=%d', errcode, extra=util.req_as_dict(request))
            resp = {'errcode': errcode,
                    'errmsg': const.strerr(errcode)}
            return json.dumps(resp, ensure_ascii=False)
        
        conn.commit()
        login_user(login_data)
        app.logger.info('login', extra=util.req_as_dict(request))
        resp = {'errcode': const.ERRCODE_SUCC}
        return json.dumps(resp, ensure_ascii=False)
    
    except Exception, e:
        app.logger.warning('failed to login, exception:%s, login_name:%s',
                           str(e), login_name, extra=util.req_as_dict(request))
        resp = {'errcode': const.ERRCODE_INTERNAL_ERR,
                'errmsg': const.strerr(const.ERRCODE_INTERNAL_ERR)}
        return json.dumps(resp, ensure_ascii=False)
Ejemplo n.º 6
0
def modify_article():
    entry_id = util.extract_arg_from_req(request, 'id')
    if entry_id == '':
        app.logger.warning('failed to modify_article, lack of parameter: id',
                           extra=util.req_as_dict(request))
        return util.make_err_json_response(const.ERRCODE_LACK_OF_PARAMETER,
                                           {"param_name": "id"})

    allowed_param_list = [
        'start_date', 'end_date', 'institution_name', 'degree_id', 'major',
        'memo'
    ]
    param_dict = {}
    for p in allowed_param_list:
        value = util.extract_arg_from_req(request, p)
        if value != '':
            param_dict[p] = value

    conn = db_util.getConnection(config)
    try:
        errcode = article_srv.modify_article(conn, entry_id, param_dict)
        if errcode != const.ERRCODE_SUCC:
            app.logger.warning('failed to modify_article, errcode: %d',
                               errcode,
                               extra=util.req_as_dict(request))
            return util.make_err_json_response(errcode)

        conn.commit()
        app.logger.info('modify_article', extra=util.req_as_dict(request))
        resp = {'errcode': const.ERRCODE_SUCC}
        return json.dumps(resp,
                          default=util.defaultSerializer,
                          ensure_ascii=False)

    except Exception, e:
        app.logger.warning('failed to modify_article, exception:%s',
                           str(e),
                           extra=util.req_as_dict(request))
        return util.make_err_json_response(const.ERRCODE_INTERNAL_ERR)
Ejemplo n.º 7
0
def list_article():
    #可选和必选的参数
    allowed_param_list = ['category_id']
    required_param_list = []
    param_dict = {}
    for p in allowed_param_list:
        value = util.extract_arg_from_req(request, p)
        if value == '':
            if p in required_param_list:
                app.logger.warning(
                    'failed to get_education_history, lack of parameter: %s',
                    p,
                    extra=util.req_as_dict(request))
                return util.make_err_json_response(
                    const.ERRCODE_LACK_OF_PARAMETER, {"param_name": p})
        else:
            param_dict[p] = value
    ## page-显示第几页, rows-每页显示多少行
    page, rows = util.extract_page_arg_from_req(request)

    conn = db_util.getConnection(config)
    try:
        total, entry_list = article_srv.list_article(conn, param_dict, page,
                                                     rows)

        app.logger.info('list_article', extra=util.req_as_dict(request))
        resp = {'errcode': 0, 'total_rows': total, 'result': entry_list}
        return json.dumps(resp,
                          default=util.defaultSerializer,
                          ensure_ascii=False)

    except Exception, e:
        app.logger.warning('failed to list_article, exception:%s',
                           str(e),
                           extra=util.req_as_dict(request))
        return util.make_err_json_response(const.ERRCODE_INTERNAL_ERR)
Ejemplo n.º 8
0
def list_category():
    parent_id = util.extract_arg_from_req(request, "parent_id")
    if parent_id == '':
        parent_id = '0'

    conn = db_util.getConnection(config)
    try:
        list_data = category_srv.getCategoryList(conn, parent_id)

        conn.commit()
        app.logger.info('login', extra=util.req_as_dict(request))
        resp = {'errcode': const.ERRCODE_SUCC, 'result': list_data}
        return json.dumps(resp, ensure_ascii=False)
    except Exception, e:
        app.logger.warning(
            'failed to list_category, exception:%s, parent_id:%s',
            str(e),
            parent_id,
            extra=util.req_as_dict(request))
        resp = {
            'errcode': const.ERRCODE_INTERNAL_ERR,
            'errmsg': const.strerr(const.ERRCODE_INTERNAL_ERR)
        }
        return json.dumps(resp, ensure_ascii=False)