realms-wiki/realms/__init__.py

223 lines
6.2 KiB
Python
Raw Normal View History

2014-01-14 01:07:13 +02:00
# Monkey patch stdlib.
import gevent.monkey
gevent.monkey.patch_all(aggressive=False)
# Set default encoding to UTF-8
import sys
reload(sys)
# noinspection PyUnresolvedReferences
sys.setdefaultencoding('utf-8')
# Silence Sentry and Requests.
2013-09-26 16:51:15 +03:00
import logging
2014-01-14 01:07:13 +02:00
logging.getLogger().setLevel(logging.INFO)
logging.getLogger('raven').setLevel(logging.WARNING)
logging.getLogger('requests').setLevel(logging.WARNING)
2013-10-02 04:50:48 +03:00
import time
2013-12-03 22:09:57 +02:00
import sys
2014-01-17 01:46:22 +02:00
import json
2014-01-14 01:07:13 +02:00
import httplib
import traceback
from flask import Flask, request, render_template, url_for, redirect, session, flash, g
2013-11-08 20:20:40 +02:00
from flask.ctx import _AppCtxGlobals
2014-08-20 18:28:25 +03:00
from flask.ext.script import Manager
from flask.ext.login import LoginManager
2013-10-03 17:58:07 +03:00
from werkzeug.routing import BaseConverter
2013-11-08 20:20:40 +02:00
from werkzeug.utils import cached_property
2014-01-14 01:07:13 +02:00
from werkzeug.exceptions import HTTPException
2013-10-05 00:42:45 +03:00
2013-12-03 22:09:57 +02:00
from realms import config
2013-11-08 20:20:40 +02:00
from realms.lib.ratelimit import get_view_rate_limit, ratelimiter
from realms.lib.session import RedisSessionInterface
from realms.lib.wiki import Wiki
2014-01-14 01:07:13 +02:00
from realms.lib.util import to_canonical, remove_ext, mkdir_safe, gravatar_url, to_dict
2013-12-09 22:24:22 +02:00
2013-11-08 20:20:40 +02:00
class AppCtxGlobals(_AppCtxGlobals):
2013-12-03 22:09:57 +02:00
@cached_property
2014-08-20 18:28:25 +03:00
def current_user(self):
return session.get('user') if session.get('user') else {'username': 'Anon'}
2013-12-03 22:09:57 +02:00
2013-12-03 01:50:19 +02:00
@cached_property
2013-12-09 22:24:22 +02:00
def current_wiki(self):
2014-08-20 18:28:25 +03:00
return Wiki(config.WIKI_PATH)
2013-11-08 20:20:40 +02:00
class Application(Flask):
app_ctx_globals_class = AppCtxGlobals
2013-12-03 01:50:19 +02:00
def __call__(self, environ, start_response):
path_info = environ.get('PATH_INFO')
2013-11-08 20:20:40 +02:00
2013-12-03 01:50:19 +02:00
if path_info and len(path_info) > 1 and path_info.endswith('/'):
environ['PATH_INFO'] = path_info[:-1]
2013-10-05 00:42:45 +03:00
2013-12-03 01:50:19 +02:00
scheme = environ.get('HTTP_X_SCHEME')
2013-10-05 00:42:45 +03:00
2013-12-03 01:50:19 +02:00
if scheme:
environ['wsgi.url_scheme'] = scheme
2013-10-05 00:42:45 +03:00
2013-12-03 01:50:19 +02:00
real_ip = environ.get('HTTP_X_REAL_IP')
2013-10-05 00:42:45 +03:00
2013-12-03 01:50:19 +02:00
if real_ip:
environ['REMOTE_ADDR'] = real_ip
2013-10-05 00:42:45 +03:00
2013-12-03 01:50:19 +02:00
return super(Application, self).__call__(environ, start_response)
2013-10-05 00:52:41 +03:00
2013-12-03 22:09:57 +02:00
def discover(self):
IMPORT_NAME = 'realms.modules'
FROMLIST = (
'assets',
'commands',
2014-08-20 18:28:25 +03:00
'models',
2013-12-03 22:09:57 +02:00
'views',
)
start_time = time.time()
__import__(IMPORT_NAME, fromlist=FROMLIST)
for module_name in self.config['MODULES']:
sources = __import__('%s.%s' % (IMPORT_NAME, module_name), fromlist=FROMLIST)
# Blueprint
if hasattr(sources, 'views'):
self.register_blueprint(sources.views.blueprint)
# Flask-Script
if hasattr(sources, 'commands'):
manager.add_command(module_name, sources.commands.manager)
print >> sys.stderr, ' * Ready in %.2fms' % (1000.0 * (time.time() - start_time))
2014-01-17 01:46:22 +02:00
def make_response(self, rv):
if rv is None:
rv = '', httplib.NO_CONTENT
elif not isinstance(rv, tuple):
rv = rv,
rv = list(rv)
if isinstance(rv[0], (list, dict)):
rv[0] = self.response_class(json.dumps(rv[0]), mimetype='application/json')
return super(Application, self).make_response(tuple(rv))
2013-10-05 00:52:41 +03:00
2013-10-03 17:58:07 +03:00
class RegexConverter(BaseConverter):
2013-10-10 00:35:06 +03:00
"""
Enables Regex matching on endpoints
"""
2013-10-03 17:58:07 +03:00
def __init__(self, url_map, *items):
super(RegexConverter, self).__init__(url_map)
self.regex = items[0]
2013-09-26 16:51:15 +03:00
2013-10-05 00:42:45 +03:00
def redirect_url(referrer=None):
if not referrer:
referrer = request.referrer
return request.args.get('next') or referrer or url_for('index')
2013-10-04 05:57:19 +03:00
2013-09-26 16:51:15 +03:00
2013-10-10 00:35:06 +03:00
2013-12-03 01:50:19 +02:00
app = Application(__name__)
2013-12-03 22:09:57 +02:00
app.config.from_object('realms.config')
2013-12-03 01:50:19 +02:00
app.session_interface = RedisSessionInterface()
app.url_map.converters['regex'] = RegexConverter
2013-12-03 22:09:57 +02:00
app.url_map.strict_slashes = False
2014-08-20 18:28:25 +03:00
app.debug = config.DEBUG
2014-01-14 01:07:13 +02:00
2013-12-03 22:09:57 +02:00
manager = Manager(app)
2013-12-03 01:50:19 +02:00
login_manager = LoginManager()
login_manager.init_app(app)
2013-09-26 16:51:15 +03:00
2014-01-14 01:07:13 +02:00
def error_handler(e):
try:
if isinstance(e, HTTPException):
status_code = e.code
message = e.description if e.description != type(e).description else None
tb = None
else:
status_code = httplib.INTERNAL_SERVER_ERROR
message = None
tb = traceback.format_exc() if g.current_user.staff else None
if request.is_xhr or request.accept_mimetypes.best in ['application/json', 'text/javascript']:
response = {
'message': message,
'traceback': tb,
}
else:
response = render_template('errors/error.html',
title=httplib.responses[status_code],
status_code=status_code,
message=message,
traceback=tb)
except HTTPException as e2:
return error_handler(e2)
return response, status_code
for status_code in httplib.responses:
if status_code >= 400:
app.register_error_handler(status_code, error_handler)
2014-08-20 18:28:25 +03:00
from realms.lib.assets import register, assets
2013-12-03 01:50:19 +02:00
assets.init_app(app)
2014-02-07 05:40:58 +02:00
assets.app = app
2014-08-20 18:28:25 +03:00
assets.debug = config.DEBUG
register('main',
'vendor/jquery/jquery.js',
'vendor/components-underscore/underscore.js',
'vendor/components-bootstrap/js/bootstrap.js',
'vendor/handlebars/handlebars.js',
'vendor/showdown/src/showdown.js',
'vendor/showdown/src/extensions/table.js',
'js/wmd.js',
'js/html-sanitizer-minified.js', # don't minify
'vendor/highlightjs/highlight.pack.js',
'vendor/parsleyjs/dist/parsley.js',
'js/main.js')
2014-02-07 05:40:58 +02:00
2013-12-03 01:50:19 +02:00
2013-12-09 22:24:22 +02:00
@app.before_request
2014-08-20 18:28:25 +03:00
def init_g():
g.assets = ['main']
2013-12-09 22:24:22 +02:00
2013-12-03 01:50:19 +02:00
@app.after_request
def inject_x_rate_headers(response):
limit = get_view_rate_limit()
if limit and limit.send_x_headers:
h = response.headers
h.add('X-RateLimit-Remaining', str(limit.remaining))
h.add('X-RateLimit-Limit', str(limit.limit))
h.add('X-RateLimit-Reset', str(limit.reset))
return response
@app.template_filter('datetime')
def _jinja2_filter_datetime(ts):
return time.strftime('%b %d, %Y %I:%M %p', time.localtime(ts))
2013-12-03 22:09:57 +02:00
2013-12-03 01:50:19 +02:00
@app.errorhandler(404)
def page_not_found(e):
return render_template('errors/404.html'), 404
2014-08-20 18:28:25 +03:00
if config.RELATIVE_PATH:
@app.route("/")
def root():
return redirect(url_for(config.ROOT_ENDPOINT))
2013-12-03 01:50:19 +02:00
2014-01-17 01:12:21 +02:00
app.discover()
2013-12-03 01:50:19 +02:00