def system_configs_setup(): if not get_logined_admin_bucket(): abort(404, "not admin") data_obj = load_app_global_envs() info = "" if request.method == "POST": configs = request.form.to_dict() set_app_global_envs(configs) if request.method == "POST": new_data_obj = load_app_global_envs() if new_data_obj != data_obj: # 尝试重启整个 Web App data_obj = new_data_obj # update should_sync_buckets_in_backend = auto_type( data_obj.get("should_sync_buckets_in_backend")) restart_backend = False if should_sync_buckets_in_backend: restart_backend = True try_to_reload_web_app(restart_backend=restart_backend) for field in ["show_donation", "should_sync_buckets_in_backend"]: if field in data_obj: data_obj[field] = auto_type(data_obj[field]) html = render_api_template("page_admin_system_setup.jade", info=info, data_obj=data_obj) #print(time.time() - t1) return Response(html)
def get_data_obj_from_POST(keys=None): # 从 request.POST 中获得一个dict类型的数据对象 pure_keys = get_pure_form_keys(keys) data_obj = {} _data = request.form.to_dict() for k, v in _data.items(): if k.endswith('@list'): k = k[:-5] v = [row.strip() for row in v.split('\n') if row.strip()] elif k.endswith('@json'): k = k[:-5] try: v = json.loads(v) except: pass elif v in ['yes', 'no', 'true', 'false']: # bool 性质的 if v in ['yes', 'true']: v = True else: v = False else: v = auto_type(v) if 'title' in k: # 有 title 关键字的 key, 必须处理为字符串的形式 v = smart_unicode(v) if isinstance(v, int) and v >= 1024000: v = smart_unicode(v) if k not in pure_keys: # 防止多余的参数进来,特别如果是callback_func 直接是一个 db_doc 的 update 函数,会有漏洞 continue else: data_obj[k] = v return data_obj
def get_site_config(fields, type_required=None, default_value=None): if isinstance(type_required, list): type_required = tuple(type_required) bucket = get_bucket_in_request_context() if not bucket: if default_value is not None: return default_value else: return None site_configs = get_bucket_site_configs(bucket) if not isinstance(fields, (list, tuple)): fields = [fields] for field in fields: if not isinstance(field, string_types): continue field_value = get_value_from_data(site_configs, field) if field_value is not None: if type_required: if isinstance(field_value, type_required): return field_value else: return auto_type(field_value) if default_value is not None: return default_value else: return None
def login(): set_not_cache_current_request() bucket = get_logined_bucket(check=True) if bucket: email = get_bucket_owner_email(bucket) else: email = "" show_donation = auto_type(get_env("show_donation")) response = render_api_template_as_response( "page_user_admin.jade", email=email, show_donation=show_donation, is_wechat_server_valid=is_wechat_server_valid) return response
def get_auto_nav_items(bucket): # 自动生成的 pages_configs = get_bucket_pages_configs(bucket) nav_items = [] homepage_url = '/' if request.args.get('status') == 'loaded': homepage_url = '/?status=loaded' homepage_nav_item = dict(name='Home', url=homepage_url) nav_items.append(homepage_nav_item) site_configs = get_bucket_site_configs(bucket) albums_root = smart_unicode(site_configs.get("albums_root", "")).strip() if albums_root: nav_items.append(dict(name="Album", url="/album")) wiki_configs = get_json_content_by_path(bucket, "__wiki.json", force_dict=True) wiki_root = wiki_configs.get("wiki_root") enable_wiki_nodes = auto_type(wiki_configs.get("enable_wiki_nodes", True)) if wiki_root: nav_items.append(dict(name="Wiki", url="/wiki")) if enable_wiki_nodes: nav_items.append(dict(name="Wiki Nodes", url="/wiki_nodes")) if 'categories.jade' in pages_configs: # 有 categories.jade 的呈现 nav_items.append(dict(name='Categories', url='/categories')) if 'archive.jade' in pages_configs: # archive 页面 nav_items.append(dict(name='Archive', url='/archive')) if has_markdown_record_by_path_prefix(bucket, "links"): nav_items.append(dict(name='Links', url='/__page/links')) if has_markdown_record_by_path_prefix(bucket, "about"): nav_items.append(dict(name='About', url='/__page/about')) if has_markdown_record_by_path_prefix(bucket, "contact"): nav_items.append(dict(name='Contact', url='/__page/contact')) if 'feed.jade' in pages_configs: nav_items.append(dict(name='Feed', url='/feed')) return nav_items
def show_wiki_nodes_as_sub_site(): bucket = get_bucket_in_request_context() if not bucket: return request_path = get_request_path().strip("/") if not re.match("wiki_nodes(/|$)", request_path): return wiki_configs = get_json_content_by_path(bucket, "__wiki.json", force_dict=True) enable_wiki_nodes = auto_type(wiki_configs.get("enable_wiki_nodes", True)) if not enable_wiki_nodes: return wiki_root = smart_unicode(wiki_configs.get("wiki_root", "")) if not wiki_root: return wiki_root = wiki_root.strip("/") wiki_title = wiki_configs.get("wiki_title") or get_just_name(wiki_root, for_folder=True) path = request.values.get("path", "").strip("/") if request.values.get("type") == "data": # return json data wiki_root = wiki_root.lower() under = "%s/%s" % (wiki_root, path) posts_info = get_bucket_posts_info(bucket) data = filter_and_get_posts_link_points_info(posts_info, under=under) nodes = data.get("nodes") if nodes: for node in nodes: node_id = node.get("id") if node_id and isinstance(node_id, string_types): if node_id.startswith("#"): tag = node_id.lstrip("#") url = "/wiki/tag/%s" % tag node["url"] = url else: relative_path = get_relative_path(node_id.strip("/"), wiki_root, return_name_if_fail=False) if relative_path: url = "/wiki/post/%s" % relative_path node["url"] = url return force_response(data) else: return render_api_template("builtin_theme_wiki_nodes.jade", wiki_title=wiki_title)
def show_wiki_as_sub_site(): bucket = get_bucket_in_request_context() if not bucket: return request_path = get_request_path().strip("/") if not re.match("wiki(/|$)", request_path): return wiki_configs = get_json_content_by_path(bucket, "__wiki.json", force_dict=True) wiki_root = smart_unicode(wiki_configs.get("wiki_root", "")) if not wiki_root: return set_data_root_in_request(wiki_root) # set data_root to request wiki_root = wiki_root.strip("/") wiki_title = wiki_configs.get("wiki_title") or get_just_name(wiki_root, for_folder=True) wiki_root = wiki_root.lower() kwargs = dict(wiki_root=wiki_root, wiki_title=wiki_title, wiki_configs=wiki_configs) if re.match("wiki/?$", request_path): # index docs = [] user_categories = wiki_configs.get("categories") if not isinstance(user_categories, (list, tuple)): user_categories = [] for user_category in user_categories: if not isinstance(user_category, dict): continue category_path = user_category.get("path") summary = smart_unicode(user_category.get("summary") or "") icon = smart_unicode(user_category.get("icon") or "") doc = get_record_by_path(bucket=bucket, path=category_path) if not doc: category_path = "%s/%s" % (wiki_root, category_path.strip("/")) doc = get_record_by_path(bucket=bucket, path=category_path) if not doc: continue doc_type = get_type_from_record(doc) if doc_type not in ["post", "folder"]: continue doc["icon"] = icon or get_value_from_data(doc, "metadata.icon") doc["summary"] = summary or get_value_from_data(doc, "metadata.summary") docs.append(doc) if not docs: # by default docs = Data.get_data(type='folder', level=1, limit=50, with_page=False, path=wiki_root) # 处理 url, 取 relative index_docs = [] for doc in docs: wiki_url = get_wiki_url_for_doc(wiki_root, doc) if not wiki_url: continue doc["wiki_url"] = wiki_url index_docs.append(doc) return render_api_template("builtin_theme_knowbase_index.jade", docs=index_docs, **kwargs) elif re.match("wiki/tag/", request_path): current_tag = get_offset_path(request_path, 2) if not current_tag: abort(404, "no tag?") docs = get_records_by_tag(bucket, current_tag, sort_by="-date") for doc in docs: doc["wiki_url"] = get_wiki_url_for_doc(wiki_root, doc) return render_api_template("builtin_theme_knowbase_tag.jade", current_tag=current_tag, docs=docs, **kwargs) elif re.search("wiki/search(/|$)", request_path): keywords = request.values.get("s") data_namespace = get_data_namespace() docs = data_namespace.get_data(bucket=bucket, keywords=keywords, pager_name="wiki", path=wiki_root, sort_by='-date', min_limit=8) for doc in docs: doc["wiki_url"] = get_wiki_url_for_doc(wiki_root, doc) return render_api_template("builtin_theme_knowbase_search.jade", docs=docs, **kwargs) elif re.match("wiki/category/", request_path): # category category_path = get_offset_path(request_path, 2).lower() wiki_nodes_url = "/wiki_nodes?path=%s" % category_path category_path = "%s/%s" % (wiki_root, category_path) folder_doc = get_record_by_path(bucket, category_path) enable_wiki_nodes = auto_type(wiki_configs.get("enable_wiki_nodes", True)) if not enable_wiki_nodes: wiki_nodes_url = "" if not folder_doc or get_type_from_record(folder_doc) != "folder": abort(404, "no category found") else: category = Category(folder_doc) docs = auto_pg(bucket=bucket, data_type="post", pager_name="wiki", path=category.path, ignore_marked_id=True, prefix_to_ignore='_', sort_by='-date', min_limit=8) for doc in docs: doc["wiki_url"] = get_wiki_url_for_doc(wiki_root, doc) return render_api_template("builtin_theme_knowbase_category.jade", category=category, docs=docs, wiki_nodes_url=wiki_nodes_url, **kwargs) elif re.match("wiki/post/", request_path): # detail doc_path = get_offset_path(request_path, 2) doc_path = "%s/%s" % (wiki_root, doc_path) doc = get_record_by_path(bucket, doc_path) if not doc: abort(404, "no doc found") else: return render_api_template("builtin_theme_knowbase_post.jade", doc=doc, **kwargs)