Exemple #1
0
def feed(name):
    if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous:
        return current_app.login_manager.unauthorized()
    cname = to_canonical(name)
    wiki_name = current_app.config['SITE_TITLE']
    start = 0
    length = int(request.args.get('length', 20))

    the_feed = AtomFeed(
        title="{} - Recent changes for page '{}'".format(wiki_name, cname),
        url=url_for('wiki.page', name=cname, _external=True),
        id="{}_pagefeed_{}".format(to_canonical(wiki_name), cname),
        feed_url=url_for('wiki.feed', name=cname, _external=True),
        generator=("Realms wiki", 'https://github.com/scragg0x/realms-wiki', __version__)
    )

    page = g.current_wiki.get_page(cname)
    items = list(itertools.islice(page.history, start, start + length))  # type: list[dict]

    for item in items:
        the_feed.add(
            title="Commit '{}'".format(item['sha']),
            content=item['message'],
            url=url_for('wiki.commit', name=name, sha=item['sha'], _external=True),
            id="{}/{}".format(item['sha'], cname),
            author=item['author'],
            updated=datetime.fromtimestamp(item['time'])
        )

    response = make_response((the_feed.to_string(), {'Content-type': 'application/atom+xml; charset=utf-8'}))
    response.add_etag()
    return response.make_conditional(request)
Exemple #2
0
def page_write(name):
    cname = to_canonical(name)

    if not cname:
        return dict(error=True, message="Invalid name")

    if not current_app.config.get('ALLOW_ANON') and current_user.is_anonymous:
        return dict(error=True, message="Anonymous posting not allowed"), 403

    if request.method == 'POST':
        # Create
        if cname in current_app.config.get('WIKI_LOCKED_PAGES'):
            return dict(error=True, message="Page is locked"), 403

        sha = g.current_wiki.get_page(cname).write(request.form['content'],
                                                   message=request.form['message'],
                                                   username=current_user.username,
                                                   email=current_user.email)

    elif request.method == 'PUT':
        edit_cname = to_canonical(request.form['name'])

        if edit_cname in current_app.config.get('WIKI_LOCKED_PAGES'):
            return dict(error=True, message="Page is locked"), 403

        if edit_cname != cname:
            g.current_wiki.get_page(cname).rename(edit_cname)

        sha = g.current_wiki.get_page(edit_cname).write(request.form['content'],
                                                        message=request.form['message'],
                                                        username=current_user.username,
                                                        email=current_user.email)

        return dict(sha=sha.decode())

    elif request.method == 'DELETE':
        # DELETE
        if cname in current_app.config.get('WIKI_LOCKED_PAGES'):
            return dict(error=True, message="Page is locked"), 403

        sha = g.current_wiki.get_page(cname).delete(username=current_user.username,
                                                    email=current_user.email)

    return dict(sha=sha.decode())
Exemple #3
0
def create(name):
    cname = to_canonical(name) if name else ""
    if cname and g.current_wiki.get_page(cname):
        # Page exists, edit instead
        return redirect(url_for('wiki.edit', name=cname))

    g.assets['js'].append('editor.js')
    return render_template('wiki/edit.html',
                           name=cname,
                           content="",
                           info={})
Exemple #4
0
def index(path):
    if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous:
        return current_app.login_manager.unauthorized()

    items = g.current_wiki.get_index()
    if path:
        path = to_canonical(path) + "/"
        items = filter(lambda x: x['name'].startswith(path), items)
    if not request.args.get('flat', '').lower() in ['yes', '1', 'true']:
        items = _tree_index(items, path=path)

    return render_template('wiki/index.html', index=items, path=path)
Exemple #5
0
def page(name):
    if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous:
        return current_app.login_manager.unauthorized()

    cname = to_canonical(name)
    if cname != name:
        return redirect(url_for('wiki.page', name=cname))

    data = g.current_wiki.get_page(cname)

    if data:
        return render_template('wiki/page.html', name=cname, page=data, partials=_partials(data.imports))
    else:
        return redirect(url_for('wiki.create', name=cname))
Exemple #6
0
def commit(name, sha):
    if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous:
        return current_app.login_manager.unauthorized()

    cname = to_canonical(name)

    data = g.current_wiki.get_page(cname, sha=sha.decode())

    if not data:
        abort(404)

    partials = _partials(data.imports, sha=sha.decode())

    return render_template('wiki/page.html', name=name, page=data, commit=sha, partials=partials)
Exemple #7
0
def edit(name):
    cname = to_canonical(name)
    page = g.current_wiki.get_page(cname)

    if not page:
        # Page doesn't exist
        return redirect(url_for('wiki.create', name=cname))

    g.assets['js'].append('editor.js')
    return render_template('wiki/edit.html',
                           name=cname,
                           content=page.data.decode(),
                           # TODO: Remove this? See #148
                           info=next(page.history),
                           sha=page.sha)
Exemple #8
0
def revert():
    cname = to_canonical(request.form.get('name'))
    commit = request.form.get('commit')
    message = request.form.get('message', "Reverting %s" % cname)

    if not current_app.config.get('ALLOW_ANON') and current_user.is_anonymous:
        return dict(error=True, message="Anonymous posting not allowed"), 403

    if cname in current_app.config.get('WIKI_LOCKED_PAGES'):
        return dict(error=True, message="Page is locked"), 403

    try:
        sha = g.current_wiki.get_page(cname).revert(commit,
                                                    message=message,
                                                    username=current_user.username,
                                                    email=current_user.email)
    except PageNotFound as e:
        return dict(error=True, message=e.message), 404

    if sha:
        flash("Page reverted")

    return dict(sha=sha.decode())