예제 #1
0
파일: utils.py 프로젝트: qlqwl/Niflheim
def init_option():
    mem = memcache.get('SITE_OPTIONS')

    if mem is None:
        cache_ops = {}
    else:
        cache_ops = json.loads(mem, encoding="utf-8")

    if set([opt["option"] for opt in DEFAULT_OPTION]) == set(
        [key.encode("utf-8") for key, value in cache_ops.iteritems()]):
        for opt_name, opt_value in cache_ops.iteritems():
            app.config[opt_name.encode("utf-8")] = opt_value
        logging.info("load site option form memcache:{}".format(cache_ops))
    else:

        db_ops = {opt.key.id(): opt.value for opt in Option.query().fetch()}
        logging.info("load site option form db:{}".format(db_ops))
        cur_ops = {}
        for opt in DEFAULT_OPTION:
            opt_name = opt["option"]
            opt_default = opt["default_value"]
            opt_value = db_ops.get(opt_name, opt_default)
            app.config[opt_name] = opt_value
            cur_ops[opt_name] = opt_value
        memcache.add(key="SITE_OPTIONS",
                     value=json.dumps(cur_ops, encoding="utf-8"),
                     time=60 * 60)
    app.config["LATEST"] = datetime.now()