Beispiel #1
0
def user_delete(username):
    admin_user = request.json.get("admin_user", "").strip()  # 当前登录的管理员用户
    token = request.json.get("token", "").strip()  # token口令
    if admin_user and token:
        redis_token = redis_db.handle_redis_token(admin_user)  # 从redis中取token
        if redis_token:
            if redis_token == token:  # 如果从redis中取到的token不为空,且等于请求body中的token
                sql1 = "SELECT role FROM user WHERE username = '******'".format(admin_user)
                res1 = db.select_db(sql1)
                print("根据用户名 【 {} 】 查询到用户类型 == >> {}".format(admin_user, res1))
                user_role = res1[0]["role"]
                if user_role == 0:  # 如果当前登录用户是管理员用户
                    sql2 = "SELECT * FROM user WHERE username = '******'".format(username)
                    res2 = db.select_db(sql2)
                    print(sql2)
                    print("根据用户名 【 {} 】 查询到用户信息 ==>> {}".format(username, res2))
                    if not res2:  # 如果要删除的用户不存在于数据库中,res2为空
                        return jsonify({"code": 3005, "msg": "删除的用户名不存在,无法进行删除,请检查!!!"})
                    elif res2[0]["role"] == 0: # 如果要删除的用户是管理员用户,则不允许删除
                        return jsonify({"code": 3006, "msg": "用户名:【 {} 】,该用户不允许删除!!!".format(username)})
                    else:
                        sql3 = "DELETE FROM user WHERE username = '******'".format(username)
                        db.execute_db(sql3)
                        print("删除用户信息SQL ==>> {}".format(sql3))
                        return jsonify({"code": 0, "msg": "恭喜,删除用户信息成功!"})
                else:
                    return jsonify({"code": 3004, "msg": "当前用户不是管理员用户,无法进行操作,请检查!!!"})
            else:
                return jsonify({"code": 3003, "msg": "token口令不正确,请检查!!!"})
        else:
            return jsonify({"code": 3002, "msg": "当前用户未登录,请检查!!!"})
    else:
        return jsonify({"code": 3001, "msg": "管理员用户/token口令不能为空,请检查!!!"})
Beispiel #2
0
def user_register():
    """注册用户"""
    username = request.json.get("username", "").strip()  # 用户名
    password = request.json.get("password", "").strip()  # 密码
    sex = request.json.get("sex", "0").strip()  # 性别,默认为0(男性)
    telephone = request.json.get("telephone", "").strip()  # 手机号
    address = request.json.get("address", "").strip()  # 地址,默认为空串
    if username and password and telephone: # 注意if条件中 "" 也是空, 按False处理
        sql1 = "SELECT username FROM user WHERE username = '******'".format(username)
        res1 = db.select_db(sql1)
        print("查询到用户名 ==>> {}".format(res1))
        sql2 = "SELECT telephone FROM user WHERE telephone = '{}'".format(telephone)
        res2 = db.select_db(sql2)
        print("查询到手机号 ==>> {}".format(res2))
        if res1:
            return jsonify({"code": 2002, "msg": "用户名已存在,注册失败!!!"})
        elif not (sex == "0" or sex == "1"):
            return jsonify({"code": 2003, "msg": "输入的性别只能是 0(男) 或 1(女)!!!"})
        elif not (len(telephone) == 11 and re.match("^1[3,5,7,8]\d{9}$", telephone)):
            return jsonify({"code": 2004, "msg": "手机号格式不正确!!!"})
        elif res2:
            return jsonify({"code": 2005, "msg": "手机号已被注册!!!"})
        else:
            password = get_md5(username, password) # 把传入的明文密码通过MD5加密变为密文,然后再进行注册
            sql3 = "INSERT INTO user(username, password, role, sex, telephone, address) " \
                  "VALUES('{}', '{}', '1', '{}', '{}', '{}')".format(username, password, sex, telephone, address)
            db.execute_db(sql3)
            print("新增用户信息SQL ==>> {}".format(sql3))
            return jsonify({"code": 0, "msg": "恭喜,注册成功!"})
    else:
        return jsonify({"code": 2001, "msg": "用户名/密码/手机号不能为空,请检查!!!"})
Beispiel #3
0
def update_user_telephone():
    """修改用户前,因为手机号唯一,为了使用例重复执行,每次需要先修改手机号,再执行用例"""
    update_sql = base_data["init_sql"]["update_user_telephone"]
    db.execute_db(update_sql)
    step_first()
    logger.info("修改用户操作:手工修改用户的手机号,以便用例重复执行")
    logger.info("执行SQL:{}".format(update_sql))
Beispiel #4
0
def delete_register_user():
    """注册用户前,先删除数据,用例执行之后,再次删除以清理数据"""
    del_sql = base_data["init_sql"]["delete_register_user"]
    db.execute_db(del_sql)
    step_first()
    logger.info("注册用户操作:清理用户--准备注册新用户")
    logger.info("执行前置SQL:{}".format(del_sql))
    yield
    db.execute_db(del_sql)
    step_last()
    logger.info("注册用户操作:删除注册的用户")
    logger.info("执行后置SQL:{}".format(del_sql))
Beispiel #5
0
def user_update(id): # id为准备修改的用户ID
    """修改用户信息"""
    admin_user = request.json.get("admin_user", "").strip() # 当前登录的管理员用户
    token = request.json.get("token", "").strip()  # token口令
    new_password = request.json.get("password", "").strip()  # 新的密码
    new_sex = request.json.get("sex", "0").strip()  # 新的性别,如果参数不传sex,那么默认为0(男性)
    new_telephone = request.json.get("telephone", "").strip()  # 新的手机号
    new_address = request.json.get("address", "").strip()  # 新的联系地址,默认为空串
    if admin_user and token and new_password and new_telephone: # 注意if条件中空串 "" 也是空, 按False处理
        if not (new_sex == "0" or new_sex == "1"):
            return jsonify({"code": 4007, "msg": "输入的性别只能是 0(男) 或 1(女)!!!"})
        elif not (len(new_telephone) == 11 and re.match("^1[3,5,7,8]\d{9}$", new_telephone)):
            return jsonify({"code": 4008, "msg": "手机号格式不正确!!!"})
        else:
            redis_token = redis_db.handle_redis_token(admin_user) # 从redis中取token
            if redis_token:
                if redis_token == token: # 如果从redis中取到的token不为空,且等于请求body中的token
                    sql1 = "SELECT role FROM user WHERE username = '******'".format(admin_user)
                    res1 = db.select_db(sql1)
                    print("根据用户名 【 {} 】 查询到用户类型 == >> {}".format(admin_user, res1))
                    user_role = res1[0]["role"]
                    if user_role == 0: # 如果当前登录用户是管理员用户
                        sql2 = "SELECT * FROM user WHERE id = '{}'".format(id)
                        res2 = db.select_db(sql2)
                        print("根据用户ID 【 {} 】 查询到用户信息 ==>> {}".format(id, res2))
                        sql3 = "SELECT telephone FROM user WHERE telephone = '{}'".format(new_telephone)
                        res3 = db.select_db(sql3)
                        print("返回结果:{}".format(res3))
                        print("查询到手机号 ==>> {}".format(res3))
                        if not res2: # 如果要修改的用户不存在于数据库中,res2为空
                            return jsonify({"code": 4005, "msg": "修改的用户ID不存在,无法进行修改,请检查!!!"})
                        elif res3: # 如果要修改的手机号已经存在于数据库中,res3非空
                            return jsonify({"code": 4006, "msg": "手机号已被注册,无法进行修改,请检查!!!"})
                        else:
                            # 如果请求参数不传address,那么address字段不会被修改,仍为原值
                            if not new_address:
                                new_address = res2[0]["address"]
                            # 把传入的明文密码通过MD5加密变为密文
                            new_password = get_md5(res2[0]["username"], new_password)
                            sql3 = "UPDATE user SET password = '******', sex = '{}', telephone = '{}', address = '{}' " \
                                   "WHERE id = {}".format(new_password, new_sex, new_telephone, new_address, id)
                            db.execute_db(sql3)
                            print("修改用户信息SQL ==>> {}".format(sql3))
                            return jsonify({"code": 0, "msg": "恭喜,修改用户信息成功!"})
                    else:
                        return jsonify({"code": 4004, "msg": "当前用户不是管理员用户,无法进行操作,请检查!!!"})
                else:
                    return jsonify({"code": 4003, "msg": "token口令不正确,请检查!!!"})
            else:
                return jsonify({"code": 4002, "msg": "当前用户未登录,请检查!!!"})
    else:
        return jsonify({"code": 4001, "msg": "管理员用户/token口令/密码/手机号不能为空,请检查!!!"})
Beispiel #6
0
def insert_delete_user():
    """删除用户前,先在数据库插入一条用户数据"""
    insert_sql = base_data["init_sql"]["insert_delete_user"][0]
    db.execute_db(insert_sql)
    step_first()
    logger.info("删除用户操作:插入新用户--准备用于删除用户")
    logger.info("执行前置SQL:{}".format(insert_sql))
    yield
    # 因为有些情况是不给删除管理员用户的,这种情况需要手动清理上面插入的数据
    del_sql = base_data["init_sql"]["insert_delete_user"][1]
    db.execute_db(del_sql)
    step_last()
    logger.info("删除用户操作:手工清理处理失败的数据")
    logger.info("执行后置SQL:{}".format(del_sql))
Beispiel #7
0
    def test007_read_notice(self):
        # 数据库插入消息
        create_time = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
        header_data = ''.join(
            random.sample([
                'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c',
                'b', 'a'
            ], 7))
        id = random.randint(100000000, 999999999)
        notice_id = random.randint(1000000000, 9999999999)
        db.execute_db(
            f"INSERT INTO yzf_crm_notice.notice (id,type,msg,`data`,creator,create_time) VALUES ( '{notice_id}','1', 'a_test_message','{header_data}','756574317326995456','{create_time}');"
        )
        db.execute_db(
            f"INSERT INTO yzf_crm_notice.user_notice (id,user_id,notice_id,read_flag,read_time,deleted) VALUES ( '{id}','756574317326995456','{notice_id}','0','{create_time}','0');"
        )
        # 将notice_id写入yaml的预期结果里
        func_name = sys._getframe().f_code.co_name
        data = {
            'id': str(notice_id),
            'type': 1,
            'msg': 'a_test_message',
            'data': header_data,
            'receiver': None,
            'readFlag': True,
            'creator': '756574317326995456'
        }
        expectresult = {'code': 0, 'msg': None, 'data': data}
        yaml_path = os.path.join(path_conf.BASE_DIR, "apidata",
                                 'personal_center.yaml')
        wd.write_yaml(yaml_path, func_name, 'expectresult', expectresult)

        csurl, method, headers, yamlvalue, yaml_path, mainkey = get_common_params(
            'personal_center.yaml', func_name)
        r = RestClient(rooturl)
        csurl = csurl + str(notice_id) + '/read-flag'
        res = r.request(csurl, method, headers=headers)
        check_codes_msg(res, yamlvalue, mainkey)
        check_datas(res, yamlvalue)
Beispiel #8
0
 def test008_set_notice_read(self):
     # 数据库插入消息
     create_time = str(time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
     data = ''.join(
         random.sample([
             'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c',
             'b', 'a'
         ], 7))
     id = random.randint(100000000, 999999999)
     notice_id = random.randint(1000000000, 9999999999)
     db.execute_db(
         f"INSERT INTO yzf_crm_notice.notice (id,type,msg,`data`,creator,create_time) VALUES ( '{notice_id}','1', 'a_test_message','{data}','756574317326995456','{create_time}');"
     )
     db.execute_db(
         f"INSERT INTO yzf_crm_notice.user_notice (id,user_id,notice_id,read_flag,read_time,deleted) VALUES ( '{id}','756574317326995456','{notice_id}','0','{create_time}','0');"
     )
     func_name = sys._getframe().f_code.co_name
     csurl, method, headers, yamlvalue, yaml_path, mainkey = get_common_params(
         'personal_center.yaml', func_name)
     r = RestClient(rooturl)
     res = r.request(csurl, method, headers=headers)
     check_codes_msg(res, yamlvalue, mainkey)
     check_datas(res, yamlvalue)
Beispiel #9
0
def pre_company_data():
    """前置插入企业看板(客户数量和员工价值)数据"""
    utc = arrow.now()
    lastnt = utc.shift(months=-1)  # 取上个月
    ym = int(lastnt.format("YYYYMM"))
    ntime = utc.format("YYYY-MM-DD HH:mm:ss")
    cus_datas = get_pre_data("pre_qy_datas.yaml", "customer_num")
    personel_data = get_pre_data("pre_qy_datas.yaml", "personel_value")
    sql_cus = cus_datas % (ym, ym, ym, ntime, ym, ntime, ntime)
    sql_per = personel_data % (ym, ym)
    sql_list_cus = sql_cus.split(';')
    sql_list_per = sql_per.split(';')
    all_sql_list = sql_list_cus + sql_list_per
    for sql in all_sql_list:
        if sql:
            db.execute_db(sql)
    yield
    delete_cus_datas = get_pre_data("pre_qy_datas.yaml", "delete_customer_num")
    delete_per_datas = get_pre_data("pre_qy_datas.yaml",
                                    "delete_personel_value")
    update_qz_datas = get_pre_data("pre_qy_datas.yaml", "update_qz")
    db.execute_db(delete_cus_datas)  # 删除预置客户数量数据
    db.execute_db(delete_per_datas)  # 删除预置员工价值数据
    db.execute_db(update_qz_datas)  # 还原权重比重为默认值