Пример #1
0
def edit_wiki(path):
    config = flask.current_app.config

    editable = config["EDITABLE"]
    if not editable:
        flask.abort(500)

    pages = get_wikipages()
    path = paths.normalize("/" + path)
    path = pages.source_path(path)
    if paths.extension(path) != config["WIKI_EXT"]:
        # TODO: better error here!
        flask.abort(500)

    userid = get_request_userid()
    cp = Checkpoints(userid, pages.store, pages.cachestore)

    from_autosave = False
    if pages.exists(path):
        lastmod = pages.last_modified(path)
        if cp.has_autosave_after(path, lastmod):
            source = cp.get_autosave(path)
            from_autosave = True
        else:
            source = pages.content(path, reformat=True)
    else:
        lastmod = 0
        source = ""

    return flask.render_template("/templates/edit.jinja2", source=source,
                                 path=path, rel=null_rel, lastmod=lastmod,
                                 from_autosave=from_autosave)
Пример #2
0
def debug_textify(path):
    pages = get_wikipages()
    txcls = get_textifier()
    indexer = get_indexer()
    searcher = indexer.searcher()

    path = paths.normalize("/" + path)
    path = pages.source_path(path)

    jsondata = pages.json(path, searcher=searcher, conditional=False)
    output = txcls(jsondata).transform()
    return flask.Response(output, mimetype="text/plain")
Пример #3
0
def debug_search(path):
    pages = get_wikipages()
    indexer = get_indexer()
    sables = indexer.searchables

    path = paths.normalize("/" + path)
    path = pages.source_path(path)

    jsondata = pages.json(path, conditional=False)
    docs = list(sables.documents(pages, path, jsondata, flask.request.args))
    return flask.render_template("/templates/debug_search.jinja2",
                                 path=path, docs=docs)
Пример #4
0
def debug_wiki_structure(path):
    pages = get_wikipages()
    indexer = get_indexer()
    searcher = indexer.searcher()

    path = paths.normalize("/" + path)
    path = pages.source_path(path)

    jsondata = pages.json(paths.basepath(path), conditional=False,
                          extra_context=flask.request.args, searcher=searcher)
    return flask.render_template("/templates/debug_wiki.jinja2",
                                 path=path, root=jsondata, searcher=searcher)
Пример #5
0
def debug_tooltip(path):
    pages = get_wikipages()
    indexer = get_indexer()
    searcher = indexer.searcher()

    path = paths.normalize("/" + path)
    path = pages.source_path(path)

    html = pages.html(
        path, templatename="/templates/plain.jinja2",
        stylesname="/templates/tooltip.jinja2",
        conditional=False, searcher=searcher,
    )
    return html
Пример #6
0
def test_normalize():
    assert paths.normalize("/") == "/"
    assert paths.normalize("/a/../") == "/"
    assert paths.normalize("/a/b/c") == "/a/b/c"
    assert paths.normalize("/a/b/c/") == "/a/b/c/"
    assert paths.normalize("/a/b/../c") == "/a/c"
    assert paths.normalize("/a/b/../c/") == "/a/c/"
    assert paths.normalize("/.") == "/"
    assert paths.normalize("/./") == "/"
    assert paths.normalize("/a/.") == "/a/"
    assert paths.normalize("/a/b/./c") == "/a/b/c"
    assert paths.normalize("/a/b/./c/") == "/a/b/c/"

    assert paths.normalize("/a/b//c") == "/a/b/c"
    assert paths.normalize("//a/b//c") == "/a/b/c"
    assert paths.normalize("//a/b//c//") == "/a/b/c/"

    with nose.tools.assert_raises(ValueError):
        _ = paths.normalize_abs("")
    with nose.tools.assert_raises(ValueError):
        _ = paths.normalize_abs("a/b/c")

    assert paths.normalize("/../a") == "/a"
    assert paths.normalize("/a/b/../../..") == "/"
Пример #7
0
def show(path):
    app = flask.current_app
    config = app.config

    pages = get_wikipages()
    indexer = get_indexer()
    searcher = indexer.searcher()
    editable = config["EDITABLE"]

    store = pages.store
    path = paths.normalize("/" + path)
    pathexists = store.exists(path)
    spath = pages.source_path(path)
    cond = not is_unconditional()
    # print("path=", path, "cond=", cond)
    isdir = pathexists and store.is_dir(path)

    if isdir:
        if not path.endswith("/"):
            return flask.redirect(path + "/", 302)
        if not store.exists(spath):
            return directory_page(pages, path)

    ext = paths.extension(path)
    if pathexists and not isdir:
        fpath = store.file_path(path)
        if fpath:
            return flask.send_file(fpath, add_etags=True, conditional=cond)
        else:
            try:
                fp = pages.store.open(path)
                if hasattr(fp, "name"):
                    fp.name = None
                mimetype, encoding = mimetypes.guess_type(path)
                resp = flask.send_file(fp, conditional=cond, mimetype=mimetype)
                etag = "%s.%s" % (path, str(store.last_modified(path)))
                resp.set_etag(etag)
                return resp
            except stores.ResourceNotFoundError:
                raise werkzeug.exceptions.NotFound

    elif not ext and store.exists(spath):
        etag = pages.etag(spath)
        if cond and etag:
            inm = flask.request.if_none_match
            if etag in inm:
                raise NotModified()

        try:
            extras = {
                "editable": editable,
                "q": flask.request.args.get('q', ''),
                "pages": pages, "searcher": searcher,
                "paths": paths,
            }
            try:
                html = pages.html(path, conditional=cond, searcher=searcher,
                                  extras=extras, allow_redirect=True)
            except wikipages.Redirect:
                e = sys.exc_info()[1]
                return flask.redirect(e.newpath, 302)

            resp = flask.Response(html)
            if etag:
                resp.set_etag(etag)
            return resp

        except stores.ResourceNotFoundError:
            e = sys.exc_info()[1]
            app.logger.error(e)
            raise werkzeug.exceptions.NotFound
    else:
        raise werkzeug.exceptions.NotFound