implements #4, use env to configure app

This commit is contained in:
Matthew Scragg 2014-09-29 15:22:20 -05:00
parent 675ec9c9f9
commit 2895308667
5 changed files with 48 additions and 41 deletions

View file

@ -23,7 +23,6 @@ from flask.ext.assets import Environment, Bundle
from werkzeug.routing import BaseConverter from werkzeug.routing import BaseConverter
from werkzeug.exceptions import HTTPException from werkzeug.exceptions import HTTPException
from realms import config
from realms.lib.util import to_canonical, remove_ext, mkdir_safe, gravatar_url, to_dict from realms.lib.util import to_canonical, remove_ext, mkdir_safe, gravatar_url, to_dict
@ -100,8 +99,7 @@ class Assets(Environment):
class RegexConverter(BaseConverter): class RegexConverter(BaseConverter):
""" """ Enables Regex matching on endpoints
Enables Regex matching on endpoints
""" """
def __init__(self, url_map, *items): def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map) super(RegexConverter, self).__init__(url_map)
@ -167,10 +165,10 @@ def _jinja2_filter_datetime(ts):
def page_not_found(e): def page_not_found(e):
return render_template('errors/404.html'), 404 return render_template('errors/404.html'), 404
if config.RELATIVE_PATH: if app.config['RELATIVE_PATH']:
@app.route("/") @app.route("/")
def root(): def root():
return redirect(url_for(config.ROOT_ENDPOINT)) return redirect(url_for(app.config['ROOT_ENDPOINT']))
@click.group() @click.group()

View file

@ -82,6 +82,13 @@ LOCKED = WIKI_LOCKED_PAGES
ROOT_ENDPOINT = 'wiki.page' ROOT_ENDPOINT = 'wiki.page'
__env = {}
for k, v in os.environ.items():
if k.startswith('REALMS_'):
__env[k[7:]] = v
globals().update(__env)
try: try:
with open(os.path.join(APP_PATH, 'config.json')) as f: with open(os.path.join(APP_PATH, 'config.json')) as f:
__settings = json.load(f) __settings = json.load(f)

View file

@ -1,6 +1,6 @@
from flask_wtf import Form, RecaptchaField from flask_wtf import Form, RecaptchaField
from wtforms import StringField, PasswordField, validators from wtforms import StringField, PasswordField, validators
from realms import config from realms import app
class RegistrationForm(Form): class RegistrationForm(Form):
@ -12,7 +12,7 @@ class RegistrationForm(Form):
]) ])
confirm = PasswordField('Repeat Password') confirm = PasswordField('Repeat Password')
if config.RECAPTCHA_ENABLE: if app.config['RECAPTCHA_ENABLE']:
setattr(RegistrationForm, 'recaptcha', RecaptchaField("You Human?")) setattr(RegistrationForm, 'recaptcha', RecaptchaField("You Human?"))

View file

@ -1,16 +1,16 @@
from flask import g, render_template, request, redirect, Blueprint, flash, url_for from flask import g, render_template, request, redirect, Blueprint, flash, url_for
from realms.modules.auth.models import User from realms.modules.auth.models import User
from realms.modules.auth.forms import LoginForm, RegistrationForm from realms.modules.auth.forms import LoginForm, RegistrationForm
from realms import config from realms import app
blueprint = Blueprint('auth', __name__, url_prefix=config.RELATIVE_PATH) blueprint = Blueprint('auth', __name__, url_prefix=app.config['RELATIVE_PATH'])
@blueprint.route("/logout") @blueprint.route("/logout")
def logout_page(): def logout_page():
User.logout() User.logout()
flash("You are now logged out") flash("You are now logged out")
return redirect(url_for(config.ROOT_ENDPOINT)) return redirect(url_for(app.config['ROOT_ENDPOINT']))
@blueprint.route("/login", methods=['GET', 'POST']) @blueprint.route("/login", methods=['GET', 'POST'])
@ -23,7 +23,7 @@ def login():
return redirect(url_for('auth.login')) return redirect(url_for('auth.login'))
if User.auth(request.form['email'], request.form['password']): if User.auth(request.form['email'], request.form['password']):
return redirect(request.args.get("next") or url_for(config.ROOT_ENDPOINT)) return redirect(request.args.get("next") or url_for(app.config['ROOT_ENDPOINT']))
else: else:
flash('Email or Password Incorrect', 'warning') flash('Email or Password Incorrect', 'warning')
return redirect(url_for('auth.login')) return redirect(url_for('auth.login'))
@ -34,9 +34,9 @@ def login():
@blueprint.route("/register", methods=['GET', 'POST']) @blueprint.route("/register", methods=['GET', 'POST'])
def register(): def register():
if not config.REGISTRATION_ENABLED: if not app.config['REGISTRATION_ENABLED']:
flash("Registration is disabled") flash("Registration is disabled")
return redirect(url_for(config.ROOT_ENDPOINT)) return redirect(url_for(app.config['ROOT_ENDPOINT']))
form = RegistrationForm() form = RegistrationForm()
@ -57,7 +57,7 @@ def register():
User.create(request.form['username'], request.form['email'], request.form['password']) User.create(request.form['username'], request.form['email'], request.form['password'])
User.auth(request.form['email'], request.form['password']) User.auth(request.form['email'], request.form['password'])
return redirect(request.args.get("next") or url_for(config.ROOT_ENDPOINT)) return redirect(request.args.get("next") or url_for(app.config['ROOT_ENDPOINT']))
return render_template("auth/register.html", form=form) return render_template("auth/register.html", form=form)

View file

@ -2,18 +2,21 @@ from flask import g, render_template, request, redirect, Blueprint, flash, url_f
from flask.ext.login import login_required from flask.ext.login import login_required
from realms.lib.util import to_canonical, remove_ext from realms.lib.util import to_canonical, remove_ext
from realms.modules.wiki.models import Wiki from realms.modules.wiki.models import Wiki
from realms import config, current_user from realms import current_user, app
blueprint = Blueprint('wiki', __name__, url_prefix=config.RELATIVE_PATH) blueprint = Blueprint('wiki', __name__, url_prefix=app.config['RELATIVE_PATH'])
wiki = Wiki(config.WIKI_PATH)
@app.before_request
def init_wiki():
g.current_wiki = Wiki(app.config['WIKI_PATH'])
@blueprint.route("/_commit/<sha>/<name>") @blueprint.route("/_commit/<sha>/<name>")
def commit(name, sha): def commit(name, sha):
cname = to_canonical(name) cname = to_canonical(name)
data = wiki.get_page(cname, sha=sha) data = g.current_wiki.get_page(cname, sha=sha)
if data: if data:
return render_template('wiki/page.html', name=name, page=data, commit=sha) return render_template('wiki/page.html', name=name, page=data, commit=sha)
else: else:
@ -22,7 +25,7 @@ def commit(name, sha):
@blueprint.route("/_compare/<name>/<regex('[^.]+'):fsha><regex('\.{2,3}'):dots><regex('.+'):lsha>") @blueprint.route("/_compare/<name>/<regex('[^.]+'):fsha><regex('\.{2,3}'):dots><regex('.+'):lsha>")
def compare(name, fsha, dots, lsha): def compare(name, fsha, dots, lsha):
diff = wiki.compare(name, fsha, lsha) diff = g.current_wiki.compare(name, fsha, lsha)
return render_template('wiki/compare.html', name=name, diff=diff, old=fsha, new=lsha) return render_template('wiki/compare.html', name=name, diff=diff, old=fsha, new=lsha)
@ -33,11 +36,11 @@ def revert():
commit = request.form.get('commit') commit = request.form.get('commit')
cname = to_canonical(name) cname = to_canonical(name)
if cname in config.WIKI_LOCKED_PAGES: if cname.lower() in app.config.WIKI_LOCKED_PAGES:
flash("Page is locked") flash("Page is locked")
return redirect(url_for(config.ROOT_ENDPOINT)) return redirect(url_for(app.config['ROOT_ENDPOINT']))
wiki.revert_page(name, commit, message="Reverting %s" % cname, g.current_wiki.revert_page(name, commit, message="Reverting %s" % cname,
username=current_user.username) username=current_user.username)
flash('Page reverted', 'success') flash('Page reverted', 'success')
return redirect(url_for('wiki.page', name=cname)) return redirect(url_for('wiki.page', name=cname))
@ -45,31 +48,30 @@ def revert():
@blueprint.route("/_history/<name>") @blueprint.route("/_history/<name>")
def history(name): def history(name):
return render_template('wiki/history.html', name=name, history=wiki.get_history(name)) return render_template('wiki/history.html', name=name, history=g.current_wiki.get_history(name))
@blueprint.route("/_edit/<name>", methods=['GET', 'POST']) @blueprint.route("/_edit/<name>", methods=['GET', 'POST'])
@login_required @login_required
def edit(name): def edit(name):
data = wiki.get_page(name) data = g.current_wiki.get_page(name)
cname = to_canonical(name) cname = to_canonical(name)
if request.method == 'POST': if request.method == 'POST':
edit_cname = to_canonical(request.form['name']) edit_cname = to_canonical(request.form['name'])
if edit_cname in config.WIKI_LOCKED_PAGES: if edit_cname.lower() in app.config['WIKI_LOCKED_PAGES']:
return redirect(url_for(config.ROOT_ENDPOINT)) return redirect(url_for(app.config['ROOT_ENDPOINT']))
if edit_cname.lower() != cname.lower(): if edit_cname.lower() != cname.lower():
wiki.rename_page(cname, edit_cname) g.current_wiki.rename_page(cname, edit_cname)
wiki.write_page(edit_cname, g.current_wiki.write_page(edit_cname,
request.form['content'], request.form['content'],
message=request.form['message'], message=request.form['message'],
username=current_user.username) username=current_user.username)
else: else:
if data: if data:
name = remove_ext(data['name']) name = remove_ext(data['name'])
content = data.get('data') content = data.get('data')
g.assets['js'].append('editor.js') g.assets['js'].append('editor.js')
return render_template('wiki/edit.html', name=name, content=content, partials=data.get('partials')) return render_template('wiki/edit.html', name=name, content=content, partials=data.get('partials'))
@ -90,20 +92,20 @@ def create(name):
if request.method == 'POST': if request.method == 'POST':
cname = to_canonical(request.form['name']) cname = to_canonical(request.form['name'])
if cname in config.WIKI_LOCKED_PAGES: if cname in app.config['WIKI_LOCKED_PAGES']:
return redirect(url_for("wiki.create")) return redirect(url_for("wiki.create"))
if not cname: if not cname:
return redirect(url_for("wiki.create")) return redirect(url_for("wiki.create"))
wiki.write_page(request.form['name'], g.current_wiki.write_page(request.form['name'],
request.form['content'], request.form['content'],
message=request.form['message'], message=request.form['message'],
create=True, create=True,
username=current_user.username) username=current_user.username)
else: else:
cname = to_canonical(name) if name else "" cname = to_canonical(name) if name else ""
if cname and wiki.get_page(cname): if cname and g.current_wiki.get_page(cname):
# Page exists, edit instead # Page exists, edit instead
return redirect(url_for('wiki.edit', name=cname)) return redirect(url_for('wiki.edit', name=cname))
@ -118,7 +120,7 @@ def page(name):
if cname != name: if cname != name:
return redirect(url_for('wiki.page', name=cname)) return redirect(url_for('wiki.page', name=cname))
data = wiki.get_page(cname) data = g.current_wiki.get_page(cname)
if data: if data:
return render_template('wiki/page.html', name=cname, page=data, partials=data.get('partials')) return render_template('wiki/page.html', name=cname, page=data, partials=data.get('partials'))