Ejemplo n.º 1
0
def jsonify(data):
    try:
        data = json_dumps(data, indent=4)
    except:
        data = json_dumps(dict(error='json_error'))
    response = Response(data, mimetype='application/json')
    return response
Ejemplo n.º 2
0
def extend_bucket_expired_date_yearly_by_alipay(bucket, try_price2=False):
    title = "FarBox"
    if bucket:
        title = "FarBox-%s" % bucket
    price = BUCKET_PRICE
    if try_price2:
        price = BUCKET_PRICE2
    if price <= 0:
        price = 128
    if request.method == 'POST':
        # # notify 过来的,要告诉 alipay 已经成功了
        cache_client = get_cache_client()
        cache_client.set('alipay_notify',
                         json_dumps(alipay.payment_doc),
                         zipped=True,
                         expiration=24 * 60 * 60)
        set_response_in_request(alipay.success_response)
    current_url = request.url.split('?')[0]  # 不处理 GET 参数
    if bucket and not is_valid_bucket_name(bucket):
        return 'not a valid bucket name'
    payment_doc = alipay.payment_doc
    if not payment_doc:
        alipay_url_to_redirect = alipay.pay(
            price=price,
            title=title,
            content=bucket,
            notify_url=current_url,
            callback_url=current_url,
        )
        force_redirect(alipay_url_to_redirect)
    else:
        # bucket 增加了一年的有效期
        bucket = payment_doc.get('body') or bucket
        bucket_service_info = get_bucket_service_info(bucket)
        order_id = payment_doc.get("order_id")
        price = payment_doc.get("total_fee")
        to_handle = True
        order_id_for_db = None
        if order_id:
            order_ids_for_db = bucket_service_info.get("order_id_list")
            order_id_for_db = "%s-%s" % (order_id, price)
            if isinstance(
                    order_ids_for_db,
                (list, tuple)) and order_id_for_db in order_ids_for_db:
                # 已经处理过了
                to_handle = False
        if to_handle:
            change_bucket_expired_date(
                bucket,
                days=367,
                order_id=order_id_for_db,
            )

        if request.method == "POST":
            return force_response(alipay.success_response)

        return to_handle
Ejemplo n.º 3
0
def py_data_to_ssdb_data(py_data):
    # just string
    if isinstance(py_data, string_types):
        return py_data
    else:
        try:
            ssdb_data = json_dumps(py_data)
        except:
            ssdb_data = ''
        return ssdb_data
Ejemplo n.º 4
0
def set_bucket_private_configs(bucket, configs):
    if not isinstance(configs, dict):
        return
    if not has_bucket(bucket):
        return
    configs_data = json_dumps(configs)
    if len(configs_data) > 500 * 1024:  # 不能大于 500k
        return
    else:
        hset("_bucket_private_configs", bucket, configs_data)
        return True
Ejemplo n.º 5
0
def get_record_data_error_info(data):
    # record_data 必须是可以 dumps 的 dict 或者 字符串
    if isinstance(data, dict):
        try:
            data = json_dumps(data)
        except:
            if sentry_client:
                sentry_client.captureException()
            return 'py-data format error'
    if not isinstance(data, string_types):
        return 'data format error'
Ejemplo n.º 6
0
def get_markdown_post_compiled_info(md_filepath, content_times = 1):
    with open(md_filepath, "rb") as f:
        raw_content = f.read()
    content = raw_content * content_times
    if len(content) > MAX_RECORD_SIZE:
        print("content size too big")
    filename = os.path.split(md_filepath)[-1]
    compiled_data = get_compiler_data_directly(filename, content=content)
    record_size = len(json_dumps(compiled_data))
    print("file_type: %s, zipped:%s, file_size:%s, record_size:%s" %(compiled_data.get("type"),
                                                       compiled_data.get("_zipped"),
                                                       compiled_data.get("size"),
                                                       record_size))
Ejemplo n.º 7
0
Archivo: my.py Proyecto: zhiiker/FarBox
def setting_settings_view():
    bucket = get_logined_bucket(check=True)
    if not bucket:
        return abort(410)
    data_obj = get_json_content_by_path(bucket, 'settings.json') or {}
    if request.method == "POST":
        keys = request.values.keys()
        data_obj = get_data_obj_from_POST(keys)
        sync_file_by_server_side(bucket,
                                 "settings.json",
                                 content=json_dumps(data_obj, indent=4))

    return render_api_template_as_response("page_user_site_settings.jade",
                                           data_obj=data_obj)
Ejemplo n.º 8
0
def json(obj):
    """
    unicode
    {"is_property": true}
    """
    if isinstance(obj, (tuple, list, dict, str, unicode)):
        try:
            return json_dumps(obj, indent=4)
        except:
            return obj
        # todo 缓存策略
        # return cache_result(cache_key)(json_dumps)(obj)
    else:
        return obj
Ejemplo n.º 9
0
def show_bucket_pages_configs_by_web_api(bucket):
    sign = request.values.get("sign")
    if not check_signature_for_bucket(bucket, sign):
        pages_config = {"error": "signature is not matched"}
    else:
        pages_config = get_bucket_pages_configs(bucket) or {}
        if not pages_config.get("can_copy", True):
            # 比如从别人那里 copy 过来的,是不允许再 copy 的
            pages_config = {
                "error":
                "copied from another bucket, not allowed to copy it again."
            }
    data = json_dumps(pages_config)
    response = Response(data, mimetype='application/json')
    return response
Ejemplo n.º 10
0
def get_server_status_bucket_configs():
    global server_status_bucket_configs
    if not server_status_bucket_configs:
        config_filepath = os.path.join(config_folder, 'server_status.json')
        if not os.path.isfile(config_filepath):
            private_key, public_key = create_private_public_keys()
            bucket = get_bucket_by_public_key(public_key)
            configs = dict(bucket=bucket,
                           private_key=private_key,
                           public_key=public_key,
                           created_at=time.time())
            write_file(config_filepath, json_dumps(configs, indent=4))
        else:
            configs = load_json_file(config_filepath)
        server_status_bucket_configs = configs
    return server_status_bucket_configs
Ejemplo n.º 11
0
def create_bucket_by_public_key(public_key,
                                init_configs=None,
                                force_to_create=False):
    # 本质上是创建一个 创世configs
    if not force_to_create:
        # todo 需要校验邀请码?
        # 非强制需要创建的,需要被邀请才能创建 bucket
        return False
    bucket = get_bucket_by_public_key(public_key)
    if not bucket:
        return False
    if has_bucket(bucket):  # 已经存在了,不允许重新创建, 认为已经创建成功了
        return True
    now = int(time.time())
    now_date = datetime.datetime.utcfromtimestamp(now)
    now_date_string = now_date.strftime('%Y-%m-%d %H:%M:%S UTC')
    bucket_info = dict(
        public_key=public_key,
        created_at=now,
        created_date=now_date_string,
    )
    if init_configs and isinstance(init_configs, dict):
        allowed_to_update = True
        init_configs_bytes = json_dumps(init_configs)
        if len(init_configs_bytes) > 10 * 1024:
            allowed_to_update = False
        if allowed_to_update:
            init_configs.update(bucket_info)
            bucket_info = init_configs

    # 创建 init config, 这是不可修改的
    hset(bucket, zero_id, bucket_info, ignore_if_exists=True)

    # 创建的时候,也进行这个操作,方便知道 buckets 的总数相关数据
    set_bucket_into_buckets(bucket)

    # 创建的时候,给 30 days 的有效期
    change_bucket_expired_date(bucket)

    return True
Ejemplo n.º 12
0
    def ajax(dom_id, url, method='get', data='', callback=''):
        # 能够在模板中直接生成 ajax 的代码逻辑
        # 向 url 发送 method 的请求,data 为 data, callback是一个本地的 js 函数名称
        # 即使在一个 list 中,我们也认为 dom_id 是可以界定的,即使最终生成的代码看起来累赘,实际并没有什么性能问题
        url = smart_unicode(url)
        if "'" in url:
            return "' is not allowed be included in a url"

        dom_id = smart_unicode(dom_id)

        method = smart_unicode(method).lower()
        if method not in ['get', 'post', 'delete', 'update']:
            method = 'get'

        if data:
            data = json_dumps(data)
        html_content = render_api_template('ajax.jade',
                                           url=url,
                                           method=method,
                                           data=data,
                                           callback=callback,
                                           dom_id=dom_id)
        return html_content
Ejemplo n.º 13
0
 def json_dumps(self, data):
     self.pre_data_for_sync(data)
     return json_dumps(data)
Ejemplo n.º 14
0
 def store_files_info(self):
     files_info_content = json_dumps(self.files_info, indent=4)
     write_file(self.files_info_filepath, files_info_content)
Ejemplo n.º 15
0
def sync_site_folder_simply(
    node,
    root,
    private_key,
    should_encrypt_file=False,
    app_name_for_sync=None,
    print_log=True,
    exclude_rpath_func=None,
):
    if not node or not root or not private_key:
        return  # ignore
    if not os.path.isdir(root):
        return  # ignore
    if not is_valid_private_key(private_key):
        return  # ignore
    now = time.time()
    app_name_for_sync = app_name_for_sync or 'farbox_bucket'
    site_folder_status_config_filepath = join(
        root, '.%s_site_folder_status.json' % app_name_for_sync)
    site_folder_status = load_json_file(
        site_folder_status_config_filepath) or {}
    bucket = get_bucket_by_private_key(private_key)
    old_bucket = site_folder_status.get('bucket')
    old_node = site_folder_status.get('node')
    if bucket != old_bucket or node != old_node:
        # bucket or node changed, reset the sync
        clear_sync_meta_data(root=root, app_name=app_name_for_sync)
        site_folder_status['bucket'] = bucket
        site_folder_status['node'] = node
        # configs 的逻辑也调整下
        for key in site_folder_status:
            if key.endswith('_md5'):
                site_folder_status.pop('key', None)

    # dump_template first
    template_folder = get_path_with_dot_allowed(root, 'template')
    if os.path.isdir(template_folder):
        pages_data = get_pages_data(template_folder)
        current_pages_md5 = get_md5(json_dumps(pages_data, indent=4))
        old_pages_md5 = site_folder_status.get('pages_md5')
        if current_pages_md5 != old_pages_md5:  # 模板发生变化
            old_pages_data = site_folder_status.get('pages') or {}
            sync_status = dump_pages(
                node=node,
                private_key=private_key,
                pages_dir=template_folder,
                old_pages_data=old_pages_data,
            )
            sync_status_code = sync_status.get('code')
            if sync_status_code != 200:
                if print_log:
                    print(sync_status.get('message'))
                return
            else:
                # update pages_md5
                site_folder_status['pages_md5'] = current_pages_md5
                site_folder_status['pages'] = pages_data
                if print_log:
                    print('template is changed and synced')

    # update files first
    files_changed = sync_folder_simply(node=node,
                                       root=root,
                                       private_key=private_key,
                                       should_encrypt_file=should_encrypt_file,
                                       app_name_for_sync=app_name_for_sync,
                                       exclude_rpath_func=exclude_rpath_func)

    # update configs
    for config_type in allowed_bucket_config_types:
        sync_bucket_config(site_folder_status,
                           root=root,
                           node=node,
                           private_key=private_key,
                           config_type=config_type,
                           print_log=print_log)

    # store the site_folder_status
    dump_json_file(filepath=site_folder_status_config_filepath,
                   data=site_folder_status)
Ejemplo n.º 16
0
def encrypt_configs_for_bucket(configs, private_key_md5):
    configs = dict(
        data=simple_encrypt(json_dumps(configs), password=private_key_md5))
    return configs