2014-10-21 01:27:38 +03:00
|
|
|
from flask import abort, g, render_template, request, redirect, Blueprint, flash, url_for, current_app
|
2014-10-22 00:06:27 +03:00
|
|
|
from flask.ext.login import login_required, current_user
|
2013-12-03 22:09:57 +02:00
|
|
|
from realms.lib.util import to_canonical, remove_ext
|
2014-10-22 23:38:32 +03:00
|
|
|
from .models import PageNotFound
|
2013-12-03 22:09:57 +02:00
|
|
|
|
2014-10-22 00:06:27 +03:00
|
|
|
blueprint = Blueprint('wiki', __name__)
|
2014-08-30 18:06:12 +03:00
|
|
|
|
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):
|
2015-07-18 12:02:59 +03:00
|
|
|
if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous():
|
|
|
|
return current_app.login_manager.unauthorized()
|
|
|
|
|
2013-12-03 22:09:57 +02:00
|
|
|
cname = to_canonical(name)
|
|
|
|
|
2014-09-29 23:22:20 +03:00
|
|
|
data = g.current_wiki.get_page(cname, sha=sha)
|
2014-10-21 01:27:38 +03:00
|
|
|
|
|
|
|
if not data:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return render_template('wiki/page.html', name=name, page=data, commit=sha)
|
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):
|
2015-07-18 12:02:59 +03:00
|
|
|
if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous():
|
|
|
|
return current_app.login_manager.unauthorized()
|
|
|
|
|
2014-09-29 23:22:20 +03:00
|
|
|
diff = g.current_wiki.compare(name, fsha, lsha)
|
2014-10-22 00:06:27 +03:00
|
|
|
return render_template('wiki/compare.html',
|
|
|
|
name=name, diff=diff, old=fsha, new=lsha)
|
2013-12-03 22:09:57 +02:00
|
|
|
|
|
|
|
|
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-10-22 23:38:32 +03:00
|
|
|
cname = to_canonical(request.form.get('name'))
|
2014-08-30 18:06:12 +03:00
|
|
|
commit = request.form.get('commit')
|
2014-10-21 01:27:38 +03:00
|
|
|
message = request.form.get('message', "Reverting %s" % cname)
|
2014-09-11 00:08:19 +03:00
|
|
|
|
2014-11-11 19:35:09 +02:00
|
|
|
if not current_app.config.get('ALLOW_ANON') and current_user.is_anonymous():
|
2014-10-31 00:59:19 +02:00
|
|
|
return dict(error=True, message="Anonymous posting not allowed"), 403
|
|
|
|
|
2014-10-22 00:06:27 +03:00
|
|
|
if cname in current_app.config.get('WIKI_LOCKED_PAGES'):
|
2014-10-22 23:38:32 +03:00
|
|
|
return dict(error=True, message="Page is locked"), 403
|
|
|
|
|
|
|
|
try:
|
|
|
|
sha = g.current_wiki.revert_page(cname,
|
|
|
|
commit,
|
|
|
|
message=message,
|
|
|
|
username=current_user.username,
|
|
|
|
email=current_user.email)
|
|
|
|
except PageNotFound as e:
|
|
|
|
return dict(error=True, message=e.message), 404
|
2014-09-11 00:08:19 +03:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
if sha:
|
|
|
|
flash("Page reverted")
|
|
|
|
|
|
|
|
return dict(sha=sha)
|
2014-08-20 18:28:25 +03:00
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/_history/<name>")
|
2013-12-03 22:09:57 +02:00
|
|
|
def history(name):
|
2015-07-18 12:02:59 +03:00
|
|
|
if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous():
|
|
|
|
return current_app.login_manager.unauthorized()
|
|
|
|
|
2014-09-29 23:22:20 +03:00
|
|
|
return render_template('wiki/history.html', name=name, history=g.current_wiki.get_history(name))
|
2013-12-03 22:09:57 +02:00
|
|
|
|
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
@blueprint.route("/_edit/<name>")
|
2014-08-30 18:06:12 +03:00
|
|
|
@login_required
|
2013-12-03 22:09:57 +02:00
|
|
|
def edit(name):
|
|
|
|
cname = to_canonical(name)
|
2014-10-21 01:27:38 +03:00
|
|
|
page = g.current_wiki.get_page(name)
|
|
|
|
|
|
|
|
if not page:
|
|
|
|
# Page doesn't exist
|
|
|
|
return redirect(url_for('wiki.create', name=cname))
|
2014-09-10 21:58:47 +03:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
name = remove_ext(page['name'])
|
|
|
|
g.assets['js'].append('editor.js')
|
|
|
|
return render_template('wiki/edit.html',
|
|
|
|
name=name,
|
|
|
|
content=page.get('data'),
|
|
|
|
info=page.get('info'),
|
|
|
|
sha=page.get('sha'),
|
|
|
|
partials=page.get('partials'))
|
2014-09-10 21:58:47 +03:00
|
|
|
|
2014-01-17 01:37:55 +02:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
@blueprint.route("/_create/", defaults={'name': None})
|
|
|
|
@blueprint.route("/_create/<name>")
|
2014-08-30 18:06:12 +03:00
|
|
|
@login_required
|
2014-10-21 01:27:38 +03:00
|
|
|
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))
|
2013-12-03 22:09:57 +02:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
g.assets['js'].append('editor.js')
|
|
|
|
return render_template('wiki/edit.html',
|
|
|
|
name=cname,
|
|
|
|
content="",
|
|
|
|
info={})
|
2013-12-03 22:09:57 +02:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
|
|
|
|
@blueprint.route("/_index")
|
|
|
|
def index():
|
2015-07-18 12:02:59 +03:00
|
|
|
if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous():
|
|
|
|
return current_app.login_manager.unauthorized()
|
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
return render_template('wiki/index.html', index=g.current_wiki.get_index())
|
|
|
|
|
|
|
|
|
|
|
|
@blueprint.route("/<name>", methods=['POST', 'PUT', 'DELETE'])
|
2014-08-30 18:06:12 +03:00
|
|
|
@login_required
|
2014-10-21 01:27:38 +03:00
|
|
|
def page_write(name):
|
|
|
|
cname = to_canonical(name)
|
|
|
|
|
|
|
|
if not cname:
|
2014-10-22 00:06:27 +03:00
|
|
|
return dict(error=True, message="Invalid name")
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2014-11-11 19:35:09 +02:00
|
|
|
if not current_app.config.get('ALLOW_ANON') and current_user.is_anonymous():
|
2014-10-31 00:59:19 +02:00
|
|
|
return dict(error=True, message="Anonymous posting not allowed"), 403
|
|
|
|
|
2013-12-03 22:09:57 +02:00
|
|
|
if request.method == 'POST':
|
2014-10-21 01:27:38 +03:00
|
|
|
# Create
|
2014-10-22 00:06:27 +03:00
|
|
|
if cname in current_app.config.get('WIKI_LOCKED_PAGES'):
|
2014-10-22 23:38:32 +03:00
|
|
|
return dict(error=True, message="Page is locked"), 403
|
2014-09-10 21:58:47 +03:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
sha = g.current_wiki.write_page(cname,
|
|
|
|
request.form['content'],
|
|
|
|
message=request.form['message'],
|
|
|
|
create=True,
|
2014-10-22 00:06:27 +03:00
|
|
|
username=current_user.username,
|
|
|
|
email=current_user.email)
|
2014-09-10 21:58:47 +03:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
elif request.method == 'PUT':
|
|
|
|
edit_cname = to_canonical(request.form['name'])
|
2014-09-10 21:58:47 +03:00
|
|
|
|
2014-10-22 00:06:27 +03:00
|
|
|
if edit_cname in current_app.config.get('WIKI_LOCKED_PAGES'):
|
2014-10-22 23:38:32 +03:00
|
|
|
return dict(error=True, message="Page is locked"), 403
|
2014-01-17 01:37:55 +02:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
if edit_cname != cname.lower():
|
|
|
|
g.current_wiki.rename_page(cname, edit_cname)
|
2013-12-03 22:09:57 +02:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
sha = g.current_wiki.write_page(edit_cname,
|
|
|
|
request.form['content'],
|
|
|
|
message=request.form['message'],
|
2014-10-22 00:06:27 +03:00
|
|
|
username=current_user.username,
|
|
|
|
email=current_user.email)
|
2013-12-03 22:09:57 +02:00
|
|
|
|
2014-10-21 01:27:38 +03:00
|
|
|
return dict(sha=sha)
|
|
|
|
|
|
|
|
else:
|
|
|
|
# DELETE
|
2014-10-22 23:38:32 +03:00
|
|
|
if cname in current_app.config.get('WIKI_LOCKED_PAGES'):
|
|
|
|
return dict(error=True, message="Page is locked"), 403
|
|
|
|
|
2014-10-22 00:06:27 +03:00
|
|
|
sha = g.current_wiki.delete_page(name,
|
|
|
|
username=current_user.username,
|
|
|
|
email=current_user.email)
|
2014-10-21 01:27:38 +03:00
|
|
|
|
|
|
|
return dict(sha=sha)
|
2014-10-09 23:47:12 +03:00
|
|
|
|
|
|
|
|
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):
|
2015-07-18 12:02:59 +03:00
|
|
|
if current_app.config.get('PRIVATE_WIKI') and current_user.is_anonymous():
|
|
|
|
return current_app.login_manager.unauthorized()
|
|
|
|
|
2013-12-03 22:09:57 +02:00
|
|
|
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-09-29 23:22:20 +03:00
|
|
|
data = g.current_wiki.get_page(cname)
|
2014-10-21 01:27:38 +03:00
|
|
|
|
2013-12-03 22:09:57 +02:00
|
|
|
if data:
|
2014-09-08 03:53:36 +03:00
|
|
|
return render_template('wiki/page.html', name=cname, page=data, partials=data.get('partials'))
|
2013-12-03 22:09:57 +02:00
|
|
|
else:
|
2014-11-17 19:25:26 +02:00
|
|
|
return redirect(url_for('wiki.create', name=cname))
|