1
0
Fork 0
mirror of https://gitlab.com/SIGBUS/nyaa.git synced 2024-06-09 02:28:30 +00:00
nyaa/nyaa/views/__init__.py
Anna-Maria Meriniemi c5d705210d Read-only maintenance mode setting for config.py (#356)
Disables all POSTs, optionally allowing users to log in (without updating last login date)
Blocked POSTs will redirect to the GET endpoint if possible, otherwise to referrer or in last case, home page.
API requests will get a plaintext message with 405 status code.
2017-09-04 18:16:52 -04:00

50 lines
1.7 KiB
Python

import flask
from nyaa.views import ( # isort:skip
account,
admin,
main,
site,
torrents,
users,
)
def _maintenance_mode_hook():
''' Blocks POSTs, unless MAINTENANCE_MODE_LOGINS is True and the POST is for a login. '''
if flask.request.method == 'POST':
allow_logins = flask.current_app.config['MAINTENANCE_MODE_LOGINS']
endpoint = flask.request.endpoint
if not (allow_logins and endpoint == 'account.login'):
message = 'Site is currently in maintenance mode.'
# In case of an API request, return a plaintext error message
if endpoint.startswith('api.'):
resp = flask.make_response(message, 405)
resp.headers['Content-Type'] = 'text/plain'
return resp
else:
# Otherwise redirect to the target page and flash a message
flask.flash(flask.Markup(message), 'danger')
try:
target_url = flask.url_for(endpoint)
except:
# Non-GET-able endpoint, try referrer or default to home page
target_url = flask.request.referrer or flask.url_for('main.home')
return flask.redirect(target_url)
def register_views(flask_app):
""" Register the blueprints using the flask_app object """
# Add our POST blocker first
if flask_app.config['MAINTENANCE_MODE']:
flask_app.before_request(_maintenance_mode_hook)
flask_app.register_blueprint(account.bp)
flask_app.register_blueprint(admin.bp)
flask_app.register_blueprint(main.bp)
flask_app.register_blueprint(site.bp)
flask_app.register_blueprint(torrents.bp)
flask_app.register_blueprint(users.bp)