Пример #1
0
def cgi_deleteUserGroup(para, res):
    # 1. prepare a logger
    logger = global_tool.get_logger(sys._getframe().f_code.co_name)

    # 2. get parameters
    for key in ["loginUin", "ownerUin", "groupIdList"]:
        if key not in para:
            res["returnCode"] = conf_code.CONF_CODE["para_miss"][0]
            res["returnMessage"] = "miss parameter(%s)" % (key)
            return

    loginUin = int(para["loginUin"])
    ownerUin = int(para["ownerUin"])

    groupIdList = para["groupIdList"]

    if not isinstance(groupIdList, list):
        groupIdList = [groupIdList]

    n = len(groupIdList)
    i = 0
    while i < n:
        groupIdList[i] = int(groupIdList[i])
        i += 1

    # 3. logic work
    session_mark = False
    try:
        session_auth = global_db.DB_SESSION_AUTH
        session_mark = True

        if session_auth.query(TableAccountUser).filter(
                TableAccountUser.userUin == ownerUin).first() is None:
            res["returnCode"] = conf_code.CONF_CODE["invalid_owner_uin"][0]
            res["returnMessage"] = conf_code.CONF_CODE["invalid_owner_uin"][1]
        else:
            res["data"]["batchRes"] = []

            for groupId in groupIdList:
                res["data"]["batchRes"].append(
                    _do_delete_group(session_auth, ownerUin, groupId))

        session_auth.commit()

    except Exception, e:
        if session_mark is True:
            session_auth.rollback()

        traceback_str = traceback.format_exc()
        res["returnCode"] = conf_code.CONF_CODE["sql_error"][0]
        res["returnMessage"] = traceback_str

        logger.error(traceback_str)
Пример #2
0
def cgi_createUserGroup(para, res):
    # 1. prepare a logger
    logger = global_tool.get_logger(sys._getframe().f_code.co_name)

    # 2. get parameters
    for key in ["loginUin", "ownerUin", "groupName", "groupRemark"]:
        if key not in para:
            res["returnCode"] = conf_code.CONF_CODE["para_miss"][0]
            res["returnMessage"] = "miss parameter(%s)" % (key)
            return

    loginUin = int(para["loginUin"])
    ownerUin = int(para["ownerUin"])
    groupName = global_tool.deal_post_str(para["groupName"])
    groupRemark = global_tool.deal_post_str(para["groupRemark"])

    # 3. logic work
    session_mark = False
    try:
        session_auth = global_db.DB_SESSION_AUTH
        session_mark = True

        if session_auth.query(TableAccountUser).filter(
                TableAccountUser.userUin == ownerUin).first() is None:
            res["returnCode"] = conf_code.CONF_CODE["invalid_owner_uin"][0]
            res["returnMessage"] = conf_code.CONF_CODE["invalid_owner_uin"][1]
        elif session_auth.query(TableAccountGroupInfo).filter(
                TableAccountGroupInfo.ownerUin == ownerUin,
                TableAccountGroupInfo.groupName ==
                groupName).first() is not None:
            res["returnCode"] = conf_code.CONF_CODE["invalid_group_name"][0]
            res["returnMessage"] = conf_code.CONF_CODE["invalid_group_name"][1]
        else:
            time_now = datetime.datetime.now()
            obj_group = TableAccountGroupInfo(ownerUin, groupName, groupRemark,
                                              time_now, time_now)

            session_auth.add(obj_group)
            session_auth.flush()

            res["data"]["userGroupDetail"] = obj_group.to_json()

        session_auth.commit()

    except Exception, e:
        if session_mark is True:
            session_auth.rollback()

        traceback_str = traceback.format_exc()
        res["returnCode"] = conf_code.CONF_CODE["sql_error"][0]
        res["returnMessage"] = traceback_str

        logger.error(traceback_str)
Пример #3
0
def process_account_request():
    # 1. get request parameters
    para_str = flask.request.get_data()
    para_dict = json.loads(para_str)

    # 2. get logger
    if "interface" in para_dict and "interfaceName" in para_dict[
            "interface"] and para_dict["interface"][
                "interfaceName"] in conf_method.CONF_METHOD:
        logger_name = conf_method.CONF_METHOD[para_dict["interface"]
                                              ["interfaceName"]]
    else:
        logger_name = "error_method"

    logger = global_tool.get_logger(logger_name)
    logger.debug('-------------------- process beg --------------------')

    if conf_common.CONF_LOG["save_request"] is True:
        logger.debug("request : %s" % (para_str))

    # 3. init respond struct
    res = {
        "version": para_dict.get("version", "1.0"),
        "componentName": para_dict.get("componentName", "yapi.account"),
        "eventId": para_dict.get("eventId", global_tool.create_eventid()),
        "timestamp": para_dict.get("timestamp",
                                   global_tool.create_timestamp()),
        "returnCode": conf_code.CONF_CODE["success"][0],
        "returnMessage": conf_code.CONF_CODE["success"][1],
        "data": {}
    }

    # 4. process
    try:
        if logger_name == "error_method":
            res["returnCode"] = conf_code.CONF_CODE["invalid_method"][0]
            res["returnMessage"] = conf_code.CONF_CODE["invalid_method"][1]
        else:
            eval(logger_name)(para=para_dict.get("interface",
                                                 {}).get("para", {}),
                              res=res)

    except KeyError, e:
        res["returnCode"] = conf_code.CONF_CODE["para_error"][0]
        res["returnMessage"] = traceback.format_exc()
Пример #4
0
def cgi_getUserDetail(para, res):
    # 1. prepare a logger
    logger = global_tool.get_logger(sys._getframe().f_code.co_name)

    # 2. get parameters
    userUinList = para.get("userUinList", [])

    if not isinstance(userUinList, list):
        userUinList = [userUinList]

    n = len(userUinList)
    i = 0
    while i < n:
        userUinList[i] = int(userUinList[i])
        i += 1

    # 3. logic work
    session_mark = False
    try:
        session_auth = global_db.DB_SESSION_AUTH
        session_mark = True

        obj_user_list = session_auth.query(
            TableAccountUser, TableAccountAppId).join(
                TableAccountAppId, TableAccountUser.ownerUin ==
                TableAccountAppId.ownerUin).filter(
                    TableAccountUser.userUin.in_(tuple(userUinList))).all()

        for obj_user in obj_user_list:
            res["data"][str(obj_user[0].userUin)] = dict(
                obj_user[0].to_json(), **obj_user[1].to_json())

        session_auth.commit()

    except Exception, e:
        if session_mark is True:
            session_auth.rollback()

        traceback_str = traceback.format_exc()
        res["returnCode"] = conf_code.CONF_CODE["sql_error"][0]
        res["returnMessage"] = traceback_str

        logger.error(traceback_str)
Пример #5
0
def cgi_getSubAccountList(para, res):
    # 1. prepare a logger
    logger = global_tool.get_logger(sys._getframe().f_code.co_name)

    # 2. get parameters
    if "ownerUin" not in para:
        res["returnCode"] = conf_code.CONF_CODE["para_miss"][0]
        res["returnMessage"] = "miss parameter(ownerUin)"
        return

    ownerUin = int(para["ownerUin"])
    pageId = int(para.get("pageId", 1))
    pageSize = int(para.get("pageSize", 10))
    keyword = global_tool.deal_post_str(para.get("keyword", ""))

    # 3. logic work
    session_mark = False
    try:
        session_auth = global_db.DB_SESSION_AUTH
        session_mark = True

        if session_auth.query(TableAccountUser).filter(
                TableAccountUser.userUin == ownerUin).first() is None:
            res["returnCode"] = conf_code.CONF_CODE["invalid_owner_uin"][0]
            res["returnMessage"] = conf_code.CONF_CODE["invalid_owner_uin"][1]
        else:
            # create a sql
            sql = session_auth.query(TableAccountUser, TableAccountAppId).join(
                TableAccountAppId, TableAccountUser.ownerUin ==
                TableAccountAppId.ownerUin).filter(
                    TableAccountUser.ownerUin == ownerUin,
                    TableAccountUser.userUin != ownerUin)

            # add condition : keyword
            if len(keyword) != 0:
                sql = sql.filter(
                    or_(TableAccountUser.userName.like("%" + keyword + "%"),
                        TableAccountUser.userRemark.like("%" + keyword + "%")))

            # get total number
            res["data"]["totalNum"] = sql.count()

            # page query
            obj_user_list = sql.limit(pageSize).offset(
                (pageId - 1) * pageSize).all()

            res["data"]["userList"] = []
            for obj_user in obj_user_list:
                res["data"]["userList"].append(
                    dict(obj_user[0].to_json(), **obj_user[1].to_json()))

        session_auth.commit()

    except Exception, e:
        if session_mark is True:
            session_auth.rollback()

        traceback_str = traceback.format_exc()
        res["returnCode"] = conf_code.CONF_CODE["sql_error"][0]
        res["returnMessage"] = traceback_str

        logger.error(traceback_str)
Пример #6
0
def cgi_getGroupMemberList(para, res):
    # 1. prepare a logger
    logger = global_tool.get_logger(sys._getframe().f_code.co_name)

    # 2. get parameters
    if "groupId" not in para:
        res["returnCode"] = conf_code.CONF_CODE["para_miss"][0]
        res["returnMessage"] = "miss parameter(groupId)"
        return

    groupId = int(para["groupId"])

    # 3. logic work
    session_mark = False
    try:
        session_auth = global_db.DB_SESSION_AUTH
        session_mark = True

        if session_auth.query(TableAccountGroupInfo).filter(
                TableAccountGroupInfo.groupId == groupId).first() is None:
            res["returnCode"] = conf_code.CONF_CODE["invalid_group_id"][0]
            res["returnMessage"] = conf_code.CONF_CODE["invalid_group_id"][1]
        else:
            # S1. init a sql
            sql = session_auth.query(
                TableAccountGroupMember, TableAccountUser,
                TableAccountAppId).join(
                    TableAccountUser, TableAccountGroupMember.userUin ==
                    TableAccountUser.userUin).join(
                        TableAccountAppId, TableAccountUser.ownerUin ==
                        TableAccountAppId.ownerUin).filter(
                            TableAccountGroupMember.groupId == groupId)

            # S2. get total count
            res["data"]["totalNum"] = sql.count()

            # S3. page query
            if  "pageId"   in para and int(para["pageId"])   > 0 and \
                "pageSize" in para and int(para["pageSize"]) > 0:
                pageId = int(para["pageId"])
                pageSize = int(para["pageSize"])
                sql = sql.limit(pageSize).offset((pageId - 1) * pageSize)

            user_list = sql.all()

            res["data"]["userList"] = []
            for item in user_list:
                res["data"]["userList"].append(
                    dict(item[1].to_json(), **item[2].to_json()))

        session_auth.commit()

    except Exception, e:
        if session_mark is True:
            session_auth.rollback()

        traceback_str = traceback.format_exc()
        res["returnCode"] = conf_code.CONF_CODE["sql_error"][0]
        res["returnMessage"] = traceback_str

        logger.error(traceback_str)
Пример #7
0
def cgi_getUserGroupList(para, res):
    # 1. prepare a logger
    logger = global_tool.get_logger(sys._getframe().f_code.co_name)

    # 2. get parameters
    for key in ["loginUin", "ownerUin"]:
        if key not in para:
            res["returnCode"] = conf_code.CONF_CODE["para_miss"][0]
            res["returnMessage"] = "miss parameter(%s)" % (key)
            return

    loginUin = int(para["loginUin"])
    ownerUin = int(para["ownerUin"])

    # 3. logic work
    session_mark = False
    try:
        session_auth = global_db.DB_SESSION_AUTH
        session_mark = True

        # S1. init a sql
        sql = session_auth.query(TableAccountGroupInfo)

        # S2. add condtion : userUin
        if "userUin" in para:
            sql = sql.join(
                TableAccountGroupMember, TableAccountGroupInfo.groupId ==
                TableAccountGroupMember.groupId).filter(
                    TableAccountGroupMember.userUin == int(para["userUin"]))

        # S3. add condtion : ownerUin
        sql = sql.filter(TableAccountGroupInfo.ownerUin == ownerUin)

        # S4. add condtion : keyword
        if "keyword" in para:
            keyword = global_tool.deal_post_str(para["keyword"])
            if len(keyword) != 0:
                sql = sql.filter(
                    or_(
                        TableAccountGroupInfo.groupName.like("%" + keyword +
                                                             "%"),
                        TableAccountGroupInfo.groupRemark.like("%" + keyword +
                                                               "%")))

        # S5. get total count
        res["data"]["totalNum"] = sql.count()

        # S6. page query
        if  "pageId"   in para and int(para["pageId"])   > 0 and \
            "pageSize" in para and int(para["pageSize"]) > 0:
            pageId = int(para["pageId"])
            pageSize = int(para["pageSize"])
            sql = sql.limit(pageSize).offset((pageId - 1) * pageSize)

        group_list = sql.all()

        res["data"]["userGroupList"] = []
        for obj_group in group_list:
            res["data"]["userGroupList"].append(obj_group.to_json())

        session_auth.commit()

    except Exception, e:
        if session_mark is True:
            session_auth.rollback()

        traceback_str = traceback.format_exc()
        res["returnCode"] = conf_code.CONF_CODE["sql_error"][0]
        res["returnMessage"] = traceback_str

        logger.error(traceback_str)
Пример #8
0
def cgi_bindUserGroup(para, res):
    # 1. prepare a logger
    logger = global_tool.get_logger(sys._getframe().f_code.co_name)

    # 2. get parameters
    for key in ["loginUin", "ownerUin", "opMode", "opList"]:
        if key not in para:
            res["returnCode"] = conf_code.CONF_CODE["para_miss"][0]
            res["returnMessage"] = "miss parameter(%s)" % (key)
            return

    loginUin = int(para["loginUin"])
    ownerUin = int(para["ownerUin"])
    opMode = int(para["opMode"])

    if opMode != USER_GROUP_BIND and opMode != USER_GROUP_UNBIND:
        res["returnCode"] = conf_code.CONF_CODE["invalid_bind_mode"][0]
        res["returnMessage"] = conf_code.CONF_CODE["invalid_bind_mode"][1]
        return

    opList = para["opList"]

    if not isinstance(opList, list):
        opList = [opList]

    n = len(opList)
    i = 0
    while i < n:
        opList[i]["groupId"] = int(opList[i]["groupId"])
        opList[i]["userUin"] = int(opList[i]["userUin"])
        i += 1

    # 3. logic work
    session_mark = False
    try:
        session_auth = global_db.DB_SESSION_AUTH
        session_mark = True

        if session_auth.query(TableAccountUser).filter(
                TableAccountUser.userUin == ownerUin).first() is None:
            res["returnCode"] = conf_code.CONF_CODE["invalid_owner_uin"][0]
            res["returnMessage"] = conf_code.CONF_CODE["invalid_owner_uin"][1]
        else:
            res["data"]["batchRes"] = []

            for item in opList:
                if opMode == USER_GROUP_BIND:
                    opFunc = _do_user_group_bind
                else:
                    opFunc = _do_user_group_unbind

                res["data"]["batchRes"].append(
                    opFunc(session_auth, ownerUin, item["groupId"],
                           item["userUin"]))

        session_auth.commit()

    except Exception, e:
        if session_mark is True:
            session_auth.rollback()

        traceback_str = traceback.format_exc()
        res["returnCode"] = conf_code.CONF_CODE["sql_error"][0]
        res["returnMessage"] = traceback_str

        logger.error(traceback_str)