2013-12-03 22:09:57 +02:00
|
|
|
from flask import g, render_template, request, redirect, Blueprint, flash, url_for
|
|
|
|
from flask.ext.login import login_required
|
|
|
|
from realms.lib.util import to_canonical, remove_ext
|
|
|
|
|
2013-12-09 22:24:22 +02:00
|
|
|
blueprint = Blueprint('wiki', __name__, url_prefix='/wiki')
|
2013-12-03 22:09:57 +02:00
|
|
|
|
|
|
|
|
2013-12-09 22:24:22 +02:00
|
|
|
@blueprint.route("/_commit/<sha>/<name>")
|
2013-12-04 00:28:16 +02:00
|
|
|
def commit(name, sha):
|
2013-12-03 22:09:57 +02:00
|
|
|
cname = to_canonical(name)
|
|
|
|
|
|
|
|
data = g.current_wiki.get_page(cname, sha=sha)
|
|
|
|
if data:
|
|
|
|
return render_template('wiki/page.html', name=name, page=data, commit=sha)
|
|
|
|
else:
|
|
|
|
return redirect(url_for('.create', name=cname))
|
|
|
|
|
|
|
|
|
2013-12-09 22:24:22 +02:00
|
|
|
@blueprint.route("/_compare/<name>/<regex('[^.]+'):fsha><regex('\.{2,3}'):dots><regex('.+'):lsha>")
|
2013-12-03 22:09:57 +02:00
|
|
|
def compare(name, fsha, dots, lsha):
|
|
|
|
diff = g.current_wiki.compare(name, fsha, lsha)
|
|
|
|
return render_template('wiki/compare.html', name=name, diff=diff, old=fsha, new=lsha)
|
|
|
|
|
|
|
|
|
2013-12-09 22:24:22 +02:00
|
|
|
@blueprint.route("/_revert", methods=['POST'])
|
2013-12-03 22:09:57 +02:00
|
|
|
def revert():
|
|
|
|
if request.method == 'POST':
|
|
|
|
name = request.form.get('name')
|
|
|
|
commit = request.form.get('commit')
|
|
|
|
cname = to_canonical(name)
|
2013-12-09 22:24:22 +02:00
|
|
|
g.current_wiki.revert_page(name, commit, message="Reverting %s" % cname,
|
|
|
|
username=g.current_user.get('username'))
|
2013-12-03 22:09:57 +02:00
|
|
|
flash('Page reverted', 'success')
|
|
|
|
return redirect(url_for('.page', name=cname))
|
|
|
|
|
2013-12-09 22:24:22 +02:00
|
|
|
@blueprint.route("/_history/<name>")
|
2013-12-03 22:09:57 +02:00
|
|
|
def history(name):
|
|
|
|
history = g.current_wiki.get_history(name)
|
2013-12-04 00:28:16 +02:00
|
|
|
return render_template('wiki/history.html', name=name, history=history, wiki_home=url_for('wiki.page'))
|
2013-12-03 22:09:57 +02:00
|
|
|
|
|
|
|
|
2013-12-09 22:24:22 +02:00
|
|
|
@blueprint.route("/_edit/<name>", methods=['GET', 'POST'])
|
2013-12-03 22:09:57 +02:00
|
|
|
def edit(name):
|
|
|
|
data = g.current_wiki.get_page(name)
|
|
|
|
cname = to_canonical(name)
|
|
|
|
if request.method == 'POST':
|
|
|
|
edit_cname = to_canonical(request.form['name'])
|
|
|
|
if edit_cname.lower() != cname.lower():
|
|
|
|
g.current_wiki.rename_page(cname, edit_cname)
|
|
|
|
g.current_wiki.write_page(edit_cname,
|
|
|
|
request.form['content'],
|
|
|
|
message=request.form['message'],
|
|
|
|
username=g.current_user.get('username'))
|
|
|
|
return redirect(url_for('.page', name=edit_cname))
|
|
|
|
else:
|
|
|
|
if data:
|
|
|
|
name = remove_ext(data['name'])
|
|
|
|
content = data['data']
|
|
|
|
return render_template('wiki/edit.html', name=name, content=content)
|
|
|
|
else:
|
|
|
|
return redirect(url_for('.create', name=cname))
|
|
|
|
|
|
|
|
|
2013-12-09 22:24:22 +02:00
|
|
|
@blueprint.route("/_delete/<name>", methods=['POST'])
|
2013-12-03 22:09:57 +02:00
|
|
|
@login_required
|
|
|
|
def delete(name):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2013-12-09 22:24:22 +02:00
|
|
|
@blueprint.route("/_create/", defaults={'name': None}, methods=['GET', 'POST'])
|
|
|
|
@blueprint.route("/_create/<name>", methods=['GET', 'POST'])
|
2013-12-03 22:09:57 +02:00
|
|
|
def create(name):
|
|
|
|
cname = ""
|
|
|
|
if name:
|
|
|
|
cname = to_canonical(name)
|
|
|
|
if g.current_wiki.get_page(cname):
|
|
|
|
# Page exists, edit instead
|
|
|
|
return redirect(url_for('.edit', name=cname))
|
|
|
|
|
|
|
|
if request.method == 'POST':
|
|
|
|
g.current_wiki.write_page(request.form['name'],
|
|
|
|
request.form['content'],
|
|
|
|
message=request.form['message'],
|
|
|
|
create=True,
|
|
|
|
username=g.current_user.get('username'))
|
|
|
|
return redirect(url_for('.page', name=cname))
|
|
|
|
else:
|
|
|
|
return render_template('wiki/edit.html', name=cname, content="")
|
|
|
|
|
|
|
|
|
2013-12-09 22:24:22 +02:00
|
|
|
@blueprint.route("/", defaults={'name': 'home'})
|
|
|
|
@blueprint.route("/<name>")
|
2013-12-03 22:09:57 +02:00
|
|
|
def page(name):
|
|
|
|
cname = to_canonical(name)
|
|
|
|
if cname != name:
|
|
|
|
return redirect(url_for('.page', name=cname))
|
|
|
|
|
|
|
|
data = g.current_wiki.get_page(cname)
|
|
|
|
|
|
|
|
if data:
|
|
|
|
return render_template('wiki/page.html', name=cname, page=data)
|
|
|
|
else:
|
|
|
|
return redirect(url_for('.create', name=cname))
|