Exemple #1
0
def wp_delete(config_agent, tpl_render, req_path):
    folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
    local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path)

    delete_page_file_by_full_path(local_full_path)
    cache.update_recent_change_cache(config_agent)
    cache.update_all_pages_list_cache(config_agent)

    web.seeother("/")
    return
Exemple #2
0
def wp_rename_post(config_agent, tpl_render, req_path, new_path):
    folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")

    old_path = req_path
    old_full_path = mdutils.req_path_to_local_full_path(
        old_path, folder_pages_full_path)
    new_full_path = mdutils.req_path_to_local_full_path(
        new_path, folder_pages_full_path)

    if old_full_path == new_full_path:
        return web.seeother("/%s" % new_path)
    elif not os.path.exists(old_full_path):
        msg = "old %s doesn't exists" % old_full_path
        raise web.BadRequest(msg)

    msg = """<pre>
allow
    file -> new_file
    folder -> new_folder

not allow
    file -> new_folder
    folder -> new_file
</pre>"""

    if os.path.isfile(old_full_path):
        if new_full_path.endswith("/"):
            raise web.BadRequest(msg)
    elif os.path.isdir(old_full_path):
        if not new_full_path.endswith("/"):
            raise web.BadRequest(msg)

    parent = os.path.dirname(new_full_path)
    if not os.path.exists(parent):
        os.makedirs(parent)

    shutil.move(old_full_path, new_full_path)

    cache.update_all_pages_list_cache(folder_pages_full_path)
    cache.update_recent_change_cache(folder_pages_full_path)

    if os.path.isfile(new_full_path):
        return web.seeother("/%s" % new_path)
    elif os.path.isdir(new_full_path):
        return web.seeother("/%s/" % new_path)
    else:
        raise web.BadRequest()
Exemple #3
0
def wp_rename_post(config_agent, tpl_render, req_path, new_path):
    folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")

    old_path = req_path
    old_full_path = mdutils.req_path_to_local_full_path(old_path, folder_pages_full_path)
    new_full_path = mdutils.req_path_to_local_full_path(new_path, folder_pages_full_path)

    if old_full_path == new_full_path:
        return web.seeother("/%s" % new_path)
    elif not os.path.exists(old_full_path):
        msg = "old %s doesn't exists" % old_full_path
        raise web.BadRequest(msg)

    msg = """<pre>
allow
    file -> new_file
    folder -> new_folder

not allow
    file -> new_folder
    folder -> new_file
</pre>"""

    if os.path.isfile(old_full_path):
        if new_full_path.endswith("/"):
            raise web.BadRequest(msg)
    elif os.path.isdir(old_full_path):
        if not new_full_path.endswith("/"):
            raise web.BadRequest(msg)

    parent = os.path.dirname(new_full_path)
    if not os.path.exists(parent):
        os.makedirs(parent)

    shutil.move(old_full_path, new_full_path)

    cache.update_all_pages_list_cache(folder_pages_full_path)
    cache.update_recent_change_cache(folder_pages_full_path)

    if os.path.isfile(new_full_path):
        return web.seeother("/%s" % new_path)
    elif os.path.isdir(new_full_path):
        return web.seeother("/%s/" % new_path)
    else:
        raise web.BadRequest()
Exemple #4
0
def wp_update_post(config_agent, req_path, new_content):
    path_info = web.ctx.environ["PATH_INFO"]

    folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
    local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path)

    folder_parent = os.path.dirname(local_full_path)
    if not os.path.exists(folder_parent):
        os.makedirs(folder_parent)

    content = new_content.replace("\r\n", "\n")

    with open(local_full_path, "w") as f:
        f.write(content)

    cache.update_recent_change_cache(folder_pages_full_path)

    return web.seeother(path_info)
Exemple #5
0
def wp_delete(config_agent, tpl_render, req_path):
    folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
    local_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path)

    path_info = web.ctx.environ["PATH_INFO"]

    if os.path.isfile(local_full_path):
        redirect_to = os.path.dirname(path_info)
        delete_page_file_by_full_path(local_full_path)
    elif os.path.isdir(local_full_path):
        redirect_to = os.path.dirname(commons.strutils.rstrip(path_info, "/"))
        delete_page_file_by_full_path(local_full_path)
    else:
        raise web.NotFound()

    cache.update_recent_change_cache(folder_pages_full_path)
    cache.update_all_pages_list_cache(folder_pages_full_path)

    return web.seeother(redirect_to)
Exemple #6
0
def wp_update_post(config_agent, req_path, new_content):
    path_info = web.ctx.environ["PATH_INFO"]

    folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
    local_full_path = mdutils.req_path_to_local_full_path(
        req_path, folder_pages_full_path)

    folder_parent = os.path.dirname(local_full_path)
    if not os.path.exists(folder_parent):
        os.makedirs(folder_parent)

    content = new_content.replace("\r\n", "\n")

    with open(local_full_path, "w") as f:
        f.write(content)

    cache.update_recent_change_cache(folder_pages_full_path)

    return web.seeother(path_info)
Exemple #7
0
def wp_create(config_agent, req_path, path, content):
    folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
    local_full_path = mdutils.req_path_to_local_full_path(path, folder_pages_full_path)

    if path.endswith("/"):
        file_full_path = os.path.join(local_full_path, "index.md")
    else:
        file_full_path = local_full_path

    parent_path = os.path.dirname(file_full_path)
    if not os.path.exists(parent_path):
        os.makedirs(parent_path)

    with open(file_full_path, "w'") as f:
        f.write(content)

    cache.update_recent_change_cache(folder_pages_full_path)
    cache.update_all_pages_list_cache(folder_pages_full_path = folder_pages_full_path)

    web.seeother(path)
Exemple #8
0
def wp_delete(config_agent, tpl_render, req_path):
    folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
    local_full_path = mdutils.req_path_to_local_full_path(
        req_path, folder_pages_full_path)

    path_info = web.ctx.environ["PATH_INFO"]

    if os.path.isfile(local_full_path):
        redirect_to = os.path.dirname(path_info)
        delete_page_file_by_full_path(local_full_path)
    elif os.path.isdir(local_full_path):
        redirect_to = os.path.dirname(commons.strutils.rstrip(path_info, "/"))
        delete_page_file_by_full_path(local_full_path)
    else:
        raise web.NotFound()

    cache.update_recent_change_cache(folder_pages_full_path)
    cache.update_all_pages_list_cache(folder_pages_full_path)

    return web.seeother(redirect_to)
Exemple #9
0
def wp_create(config_agent, req_path, path, content):
    folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
    local_full_path = mdutils.req_path_to_local_full_path(
        path, folder_pages_full_path)

    if path.endswith("/"):
        file_full_path = os.path.join(local_full_path, "index.md")
    else:
        file_full_path = local_full_path

    parent_path = os.path.dirname(file_full_path)
    if not os.path.exists(parent_path):
        os.makedirs(parent_path)

    with open(file_full_path, "w'") as f:
        f.write(content)

    cache.update_recent_change_cache(folder_pages_full_path)
    cache.update_all_pages_list_cache(
        folder_pages_full_path=folder_pages_full_path)

    web.seeother(path)
Exemple #10
0
    def POST(self, req_path):
        inputs = web.input()
            
        if req_path == "~search":
            view_settings = page.get_view_settings(config_agent)

            keywords = inputs.get("k")
            keywords = web.utils.safestr(keywords)
            title = "Search %s" % keywords

            if keywords:
                limit = config_agent.config.getint("pagination", "search_page_limit")
                lines = search.search_by_filename_and_file_content(keywords, limit = limit)
                buf = mdutils.sequence_to_unorder_list(seq = lines, **view_settings)
            else:
                buf = None

            if buf:
                content = mdutils.md2html(config_agent = config_agent, req_path = req_path, text = buf, **view_settings)
            else:
                content = "matched not found"

            static_files = static_file.get_global_static_files(**view_settings)

            return tpl_render.search(static_files = static_files, title = title, keywords = keywords, content = content)

        elif req_path == "~settings":
            show_full_path = inputs.get("show_full_path")
            auto_toc = inputs.get("auto_toc")
            highlight_code = inputs.get("highlight_code")

            if show_full_path == "on":
                show_full_path = 1
            else:
                show_full_path = 0
            web.setcookie(name = "zw_show_full_path", value = show_full_path, expires = 31536000)

            if auto_toc == "on":
                auto_toc = 1
            else:
                auto_toc = 0
            web.setcookie(name = "zw_auto_toc", value = auto_toc, expires = 31536000)

            if highlight_code == "on":
                highlight_code = 1
            else:
                highlight_code = 0
            web.setcookie(name = "zw_highlight", value = highlight_code, expires = 31536000)


            latest_req_path = web.cookies().get("zw_latest_req_path")

            if latest_req_path and (latest_req_path not in consts.g_redirect_paths) and latest_req_path != "/":
                web.setcookie(name = "zw_latest_req_path", value = "", expires = -1)
                latest_req_path = "/" + latest_req_path
            else:
                latest_req_path = "/"

            web.seeother(latest_req_path)
            return

        elif req_path == "~new":
            real_req_path = inputs.get("path")
            fixed_req_path = web.lstrips(real_req_path, "/")

            content = inputs.get("content")
            content = web.utils.safestr(content)
        
            page.update_page_by_req_path(req_path = fixed_req_path, content = content)

            cache.update_recent_change_cache(config_agent)
            cache.update_all_pages_list_cache(config_agent)

            web.seeother(real_req_path)
            return

        else:
            raise web.NotFound()
Exemple #11
0
    def POST(self, req_path):
        req_path = cgi.escape(req_path)

        inputs = web.input()
        action = inputs.get("action")
        if (not action) or (action not in ("edit", "rename")):
            raise web.BadRequest()

        content = inputs.get("content")
        content = web.utils.safestr(content)

        folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
        # NOTICE: if req_path == `users/`, full_path will be `/path/to/users/`,
        #         its parent will be `/path/to/users`.
        full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path)

        parent = os.path.dirname(full_path)
        if not os.path.exists(parent):
            os.makedirs(parent)

        if action == "edit":
            page.update_page_by_req_path(req_path = req_path, content = content)

            web.seeother("/%s" % req_path)
            return
        elif action == "rename":
            new_path = inputs.get("new_path")
            if not new_path:
                raise web.BadRequest()

            old_full_path = mdutils.req_path_to_local_full_path(req_path, folder_pages_full_path)
            if os.path.isfile(old_full_path):
                new_full_path = mdutils.req_path_to_local_full_path(new_path)
            elif os.path.isdir(old_full_path):
                folder_pages_full_path = config_agent.get_full_path("paths", "pages_path")
                new_full_path = os.path.join(folder_pages_full_path, new_path)
            else:
                raise Exception("un-expected path '%s'" % req_path)

            if os.path.exists(new_full_path):
                err_info = "WARNING: The page %s already exists" % new_full_path
                return tpl_render.rename(req_path, err_info, static_files = static_file.g_global_static_files)

            parent = os.path.dirname(new_full_path)
            if not os.path.exists(parent):
                os.makedirs(parent)

            shutil.move(old_full_path, new_full_path)

            cache.update_all_pages_list_cache()
            cache.update_recent_change_cache()

            if os.path.isfile(new_full_path):
                web.seeother("/%s" % new_path)
                return
            elif os.path.isdir(new_full_path):
                web.seeother("/%s/" % new_path)
                return
            else:
                raise Exception("un-expected path '%s'" % new_path)

        url = os.path.join("/", req_path)
        web.redirect(url)
        return