コード例 #1
0
ファイル: verify_code.py プロジェクト: lianglizhe/flask_first
def get_image_code(image_code_id):
    """
    获取图形验证码
    :param image_code_id:图片的编号
    :return:验证码 , 验证码图像
    """
    #验证参数
    #业务逻辑   生成验证码图片
    text, image_data = captcha.generate_captcha()
    #保存图形验证码
    #reids_store.set()
    #redis_store.expire()
    #相当于
    try:
        redis_store.setex("image_code_%s" % image_code_id,
                          constants.IMAGE_CODE_REDISE_EXPIRE, text)
    except Exception as e:
        logging.error(e)
        #返回json字符串
        return jsonify(errno=RET.DBERR, errmsg="验证码图片保存失败")

    #返回值
    response = make_response(image_data)
    response.headers['Content-Type'] = 'image/jpg'
    return response
コード例 #2
0
def get_house_index():
    """
    获取首页房屋信息
    :return: 排序后房屋信息
    """
    # 先查询缓存
    try:
        # print("wrwerwerwer")
        result = redis_store.get("home_page_data")
    except Exception as e:
        logging.error(e)
        result = None

    if result:
        return result.decode(), 200, {"Content-Type": "pplication/json"}
    else:
        # 查询数据库,房屋订单最多的5条
        try:
            houses = House.query.order_by(House.order_count.desc()).limit(
                contants.HOME_PAGE_MAX_NUMX).all()
        except Exception as e:
            logging.error(e)
            return jsonify(errno=RET.DBERR, errmsg='查询数据库失败')

        if not houses:
            return jsonify(errno=RET.NODATA, errmsg='没有数据')

        # print(houses)
        # print(type(houses))

        houses_list = []
        for house in houses:
            houses_list.append(house.to_basic_dict())

        house_dict = dict(errno=RET.OK, errmsg="ok", data=houses_list)
        json_houses = json.dumps(house_dict)
        try:
            redis_store.setex("home_page_data",
                              contants.HOME_PAGE_DATE_REDIS_EXPIRES,
                              json_houses)
        except Exception as e:
            logging.error(e)

        return json_houses, 200, {"Content-Type": "pplication/json"}
コード例 #3
0
ファイル: houses.py プロジェクト: lianglizhe/flask_first
def get_house_datail(house_id):
    """
    获取房屋详情   house_id  房屋信息
    :return:
    """
    #当前用户    #g对象中
    user_id = session.get("user_id", "-1")  #取不到值显示-1 (未登录)

    #校验参数
    if not house_id:
        return jsonify(errno=RET.PARAMERR, errmsg="参数错误")

    #先从缓存中查询数据
    try:
        result = redis_store.get("house_info_%s" % house_id)
    except Exception as e:
        logging.error(e)
        result = None
    if result:
        return '{"errrno":%s, "errmsg":"ok", data:{"house": %s, "user_id": %s}}'%(RET.OK, result.decode(), user_id), 200, {"Content-Type":"application/json"}


    #查询数据库
    try:
        house = House.query.get(house_id)
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg="查询数据库失败")

    if not house:
        return jsonify(errno=RET.NODATA, errmsg="房屋不存在")

    house_data = house.to_full_dict()
    #存入redis
    json_house = json.dumps(house_data)
    try:
        redis_store.setex("house_info_%s" % house_id, constants.HOUSE_DETAIL_REDIS_EXPIRES, json_house)
    except Exception as e:
        logging.error(e)


    return jsonify(errno=RET.OK, errmsg="okk", data={"house":house_data, "user_id":user_id})
コード例 #4
0
ファイル: houses.py プロジェクト: lianglizhe/flask_first
def get_area_info():

    #用redis中读取数据
    try:
        response_json = redis_store.get("area_info")
    except Exception as e:
        logging.error(e)
    else:
        # redis有缓存数据
        if response_json is not None:
            response_json = json.loads(response_json)
            # print(response_json)
            logging.info('redis cache')
            #return response_json, 200, {"Content-Type": "application/json"}
            return jsonify(errno=RET.OK, errmsg='OK', data=response_json['data'])


    #查询数据库,读取城区信息
    try:
        area_li = Area.query.all()
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')

    area_dict_li = []
    #
    for area in area_li:
        area_dict_li.append(area.to_dict())

    #将数据转成json字符串
    #response_dict = dict(errno=RET.OK, errmsg="ok", data=area_dict_li)
    response_dict = dict(data=area_dict_li)
    response_json = json.dumps(response_dict)

    try:
        redis_store.setex("area_info", constants.AREA_INFO_REDIS_CACHE_EXPIRES, response_json)
    except Exception as e:
        logging.error(e)

    #return response_json, 200, {"Content-Type": "application/json"}
    return jsonify(errno=RET.OK, errmsg="ok", data=area_dict_li)
コード例 #5
0
def get_area_info():
    """
    获取城区信息
    :return:
    """
    # 从redis里读取数据
    try:
        response_json = redis_store.get("area_info")
    except Exception as e:
        logging.error(e)
    else:
        # redis 有缓存数据
        if response_json is not None:
            response_json = json.loads(response_json)
            # print(response_json)
            # print("--",type(response_json['data']))
            return jsonify(errno=RET.OK,
                           errmsg='OK',
                           data=response_json['data'])

    # 查询数据库 获取城区信息
    try:
        area_li = Area.query.all()
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg='数据库异常')
    area_dict_li = []
    for area in area_li:
        area_dict_li.append(area.to_dict())
    # 将数据转成json字符串
    response_dict = dict(data=area_dict_li)
    # response_dict = dict(area_dict_li)
    response_json = json.dumps(response_dict)
    try:
        redis_store.setex("area_info", contants.AREA_INFO_REDIS_CACHE_EXPIRES,
                          response_json)
    except Exception as e:
        logging.error(e)
    # print("--",type(area_dict_li))
    return jsonify(errno=RET.OK, errmsg='ok', data=area_dict_li)
コード例 #6
0
def get_image_code(image_code_id):
    """
    获取图片验证码
    :param image_code_id: 图片的编号
    :return: 验证码  图像
    """
    # 验证参数
    # 业务逻辑处理
    # 生成验证码图片
    text, image_data = captcha.generate_captcha()
    # 保存验证码 redis
    # redis_store.set()
    # redis_store.expire()
    try:
        redis_store.setex('image_code_%s' % image_code_id,
                          contants.IMAGE_CODE_REDIS_EXPIRES, text)
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.OK, errmsg='保存图片验证码失败')
    # 返回值
    reponse = make_response(image_data)
    reponse.headers['Content-Type'] = 'image/jpg'
    return reponse
コード例 #7
0
def get_house_detail(house_id):
    """
    获取房屋的详情
    :param house_id: 房屋id
    :return:  房屋的详细信息
    """
    # 当前用户
    # g对象
    user_id = session.get("user_id", "-1")

    # 校验参数
    if not house_id:
        return jsonify(errno=RET.PARAMERR, errmsg='参数错误')

    # 查询缓存
    try:
        result = redis_store.get("house_info_%s" % house_id)
    except Exception as e:
        logging.error(e)
        # return jsonify(errno=RET.PARAMERR, errmsg='参数错误')
        result = None
    if result:
        # print("result---2:", result)
        # print("result---2:", type(result))
        # print("result---2:", result)
        print("result---3:", result.decode())
        return '{"errno":%s, "errmsg":"OK", "data":{"house": %s, "user_id": %s }}' % (
            RET.OK, result.decode(), user_id), 200, {
                "Content-Type": "pplication/json"
            }

    # 查询数据库
    try:
        house = House.query.get(house_id)
    except Exception as e:
        logging.error(e)
        return jsonify(errno=RET.DBERR, errmsg='查询数据库失败')

    if not house:
        return jsonify(errno=RET.NODATA, errmsg='房屋不存在')

    # print(house)

    house_data = house.to_full_dict()

    # 保存redis
    json_data = json.dumps(house_data)
    print("json:--", json_data)
    try:
        redis_store.setex("house_info_%s" % house_id,
                          contants.HOUSE_DETAIL_REDIS_EXPIRES, json_data)
    except Exception as e:
        logging.error(e)
    # print("house_data---1:", house_data)
    # print("house_data---1:", type(house_data))
    print(house)
    return jsonify(errno=RET.OK,
                   errmsg='OK',
                   data={
                       "house": house_data,
                       "user_id": user_id
                   })