realms-wiki/realms/modules/wiki/views.py

127 lines
4.1 KiB
Python
Raw Normal View History

2014-08-30 18:06:12 +03:00
from flask import g, render_template, request, redirect, Blueprint, flash, url_for, current_app
from flask.ext.login import login_required
2013-12-03 22:09:57 +02:00
from realms.lib.util import to_canonical, remove_ext
2014-08-30 18:06:12 +03:00
from realms.modules.wiki.models import Wiki
from realms import config, current_user
2013-12-03 22:09:57 +02:00
2014-08-20 18:28:25 +03:00
blueprint = Blueprint('wiki', __name__, url_prefix=config.RELATIVE_PATH)
2013-12-03 22:09:57 +02:00
2014-08-30 18:06:12 +03:00
wiki = Wiki(config.WIKI_PATH)
2013-12-03 22:09:57 +02:00
2014-08-20 18:28:25 +03: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)
2014-08-30 18:06:12 +03:00
data = wiki.get_page(cname, sha=sha)
2013-12-03 22:09:57 +02:00
if data:
return render_template('wiki/page.html', name=name, page=data, commit=sha)
else:
2014-01-17 01:12:21 +02:00
return redirect(url_for('wiki.create', name=cname))
2013-12-03 22:09:57 +02:00
2014-08-20 18:28:25 +03: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):
2014-08-30 18:06:12 +03:00
diff = wiki.compare(name, fsha, lsha)
2013-12-03 22:09:57 +02:00
return render_template('wiki/compare.html', name=name, diff=diff, old=fsha, new=lsha)
2014-08-20 18:28:25 +03:00
@blueprint.route("/_revert", methods=['POST'])
2014-08-30 18:06:12 +03:00
@login_required
2013-12-03 22:09:57 +02:00
def revert():
2014-08-30 18:06:12 +03:00
name = request.form.get('name')
commit = request.form.get('commit')
cname = to_canonical(name)
2014-09-11 00:08:19 +03:00
if cname in config.LOCKED:
flash("Page is locked")
return redirect(url_for(config.ROOT_ENDPOINT))
2014-08-30 18:06:12 +03:00
wiki.revert_page(name, commit, message="Reverting %s" % cname,
2014-09-02 17:29:04 +03:00
username=current_user.username)
2014-08-30 18:06:12 +03:00
flash('Page reverted', 'success')
return redirect(url_for('wiki.page', name=cname))
2014-08-20 18:28:25 +03:00
@blueprint.route("/_history/<name>")
2013-12-03 22:09:57 +02:00
def history(name):
2014-08-30 18:06:12 +03:00
history = 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
2014-08-20 18:28:25 +03:00
@blueprint.route("/_edit/<name>", methods=['GET', 'POST'])
2014-08-30 18:06:12 +03:00
@login_required
2013-12-03 22:09:57 +02:00
def edit(name):
2014-08-30 18:06:12 +03:00
data = wiki.get_page(name)
2013-12-03 22:09:57 +02:00
cname = to_canonical(name)
if request.method == 'POST':
edit_cname = to_canonical(request.form['name'])
if edit_cname in config.LOCKED:
return redirect(url_for(config.ROOT_ENDPOINT))
2013-12-03 22:09:57 +02:00
if edit_cname.lower() != cname.lower():
2014-08-30 18:06:12 +03:00
wiki.rename_page(cname, edit_cname)
2014-01-17 01:37:55 +02:00
2014-08-30 18:06:12 +03:00
wiki.write_page(edit_cname,
request.form['content'],
message=request.form['message'],
2014-09-02 17:29:04 +03:00
username=current_user.username)
2013-12-03 22:09:57 +02:00
else:
if data:
name = remove_ext(data['name'])
content = data.get('data')
g.assets['js'].append('editor.js')
return render_template('wiki/edit.html', name=name, content=content, partials=data.get('partials'))
2013-12-03 22:09:57 +02:00
else:
2014-01-17 01:12:21 +02:00
return redirect(url_for('wiki.create', name=cname))
2013-12-03 22:09:57 +02:00
2014-08-20 18:28:25 +03:00
@blueprint.route("/_delete/<name>", methods=['POST'])
2014-08-30 18:06:12 +03:00
@login_required
2013-12-03 22:09:57 +02:00
def delete(name):
pass
2014-08-20 18:28:25 +03:00
@blueprint.route("/_create/", defaults={'name': None}, methods=['GET', 'POST'])
@blueprint.route("/_create/<name>", methods=['GET', 'POST'])
2014-08-30 18:06:12 +03:00
@login_required
2013-12-03 22:09:57 +02:00
def create(name):
if request.method == 'POST':
cname = to_canonical(request.form['name'])
if cname in config.LOCKED:
return redirect(url_for("wiki.create"))
if not cname:
return redirect(url_for("wiki.create"))
2014-08-30 18:06:12 +03:00
wiki.write_page(request.form['name'],
request.form['content'],
message=request.form['message'],
create=True,
2014-09-02 17:29:04 +03:00
username=current_user.username)
2013-12-03 22:09:57 +02:00
else:
2014-01-17 01:37:55 +02:00
cname = to_canonical(name) if name else ""
2014-08-30 18:06:12 +03:00
if cname and wiki.get_page(cname):
2014-01-17 01:37:55 +02:00
# Page exists, edit instead
return redirect(url_for('wiki.edit', name=cname))
g.assets['js'].append('editor.js')
2013-12-03 22:09:57 +02:00
return render_template('wiki/edit.html', name=cname, content="")
2014-08-20 18:28:25 +03: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:
2014-01-17 01:12:21 +02:00
return redirect(url_for('wiki.page', name=cname))
2013-12-03 22:09:57 +02:00
2014-08-30 18:06:12 +03:00
data = wiki.get_page(cname)
2013-12-03 22:09:57 +02:00
if data:
return render_template('wiki/page.html', name=cname, page=data, partials=data.get('partials'))
2013-12-03 22:09:57 +02:00
else:
2014-01-17 01:12:21 +02:00
return redirect(url_for('wiki.create', name=cname))