mirror of
https://gitlab.com/SIGBUS/nyaa.git
synced 2024-12-22 15:40:00 +00:00
Move 'before_request' into 'main' blueprint
Update comment in api_handler with new before_request location
This commit is contained in:
parent
eccb0ebdff
commit
4aac17ff23
|
@ -24,7 +24,7 @@ api_blueprint = flask.Blueprint('api', __name__)
|
||||||
def basic_auth_user(f):
|
def basic_auth_user(f):
|
||||||
''' A decorator that will try to validate the user into g.user from basic auth.
|
''' A decorator that will try to validate the user into g.user from basic auth.
|
||||||
Note: this does not set user to None on failure, so users can also authorize
|
Note: this does not set user to None on failure, so users can also authorize
|
||||||
themselves with the cookie (handled in routes.before_request). '''
|
themselves with the cookie (handled in views.main.before_request). '''
|
||||||
@functools.wraps(f)
|
@functools.wraps(f)
|
||||||
def decorator(*args, **kwargs):
|
def decorator(*args, **kwargs):
|
||||||
auth = flask.request.authorization
|
auth = flask.request.authorization
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import json
|
import json
|
||||||
import os.path
|
import os.path
|
||||||
from datetime import datetime, timedelta
|
|
||||||
from urllib.parse import quote
|
from urllib.parse import quote
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
|
@ -25,25 +24,6 @@ def not_found(error):
|
||||||
return flask.render_template('404.html'), 404
|
return flask.render_template('404.html'), 404
|
||||||
|
|
||||||
|
|
||||||
@app.before_request
|
|
||||||
def before_request():
|
|
||||||
flask.g.user = None
|
|
||||||
if 'user_id' in flask.session:
|
|
||||||
user = models.User.by_id(flask.session['user_id'])
|
|
||||||
if not user:
|
|
||||||
return views.account.logout()
|
|
||||||
|
|
||||||
flask.g.user = user
|
|
||||||
|
|
||||||
if 'timeout' not in flask.session or flask.session['timeout'] < datetime.now():
|
|
||||||
flask.session['timeout'] = datetime.now() + timedelta(days=7)
|
|
||||||
flask.session.permanent = True
|
|
||||||
flask.session.modified = True
|
|
||||||
|
|
||||||
if flask.g.user.status == models.UserStatusType.BANNED:
|
|
||||||
return 'You are banned.', 403
|
|
||||||
|
|
||||||
|
|
||||||
@cached_function
|
@cached_function
|
||||||
def get_category_id_map():
|
def get_category_id_map():
|
||||||
''' Reads database for categories and turns them into a dict with
|
''' Reads database for categories and turns them into a dict with
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
import math
|
import math
|
||||||
import re
|
import re
|
||||||
|
from datetime import datetime, timedelta
|
||||||
|
|
||||||
import flask
|
import flask
|
||||||
from flask_paginate import Pagination
|
from flask_paginate import Pagination
|
||||||
|
@ -8,10 +9,30 @@ from nyaa import app, models
|
||||||
from nyaa.search import (DEFAULT_MAX_SEARCH_RESULT, DEFAULT_PER_PAGE, SERACH_PAGINATE_DISPLAY_MSG,
|
from nyaa.search import (DEFAULT_MAX_SEARCH_RESULT, DEFAULT_PER_PAGE, SERACH_PAGINATE_DISPLAY_MSG,
|
||||||
_generate_query_string, search_db, search_elastic)
|
_generate_query_string, search_db, search_elastic)
|
||||||
from nyaa.utils import chain_get
|
from nyaa.utils import chain_get
|
||||||
|
from nyaa.views.account import logout
|
||||||
|
|
||||||
bp = flask.Blueprint('main', __name__)
|
bp = flask.Blueprint('main', __name__)
|
||||||
|
|
||||||
|
|
||||||
|
@bp.before_app_request
|
||||||
|
def before_request():
|
||||||
|
flask.g.user = None
|
||||||
|
if 'user_id' in flask.session:
|
||||||
|
user = models.User.by_id(flask.session['user_id'])
|
||||||
|
if not user:
|
||||||
|
return logout()
|
||||||
|
|
||||||
|
flask.g.user = user
|
||||||
|
|
||||||
|
if 'timeout' not in flask.session or flask.session['timeout'] < datetime.now():
|
||||||
|
flask.session['timeout'] = datetime.now() + timedelta(days=7)
|
||||||
|
flask.session.permanent = True
|
||||||
|
flask.session.modified = True
|
||||||
|
|
||||||
|
if flask.g.user.status == models.UserStatusType.BANNED:
|
||||||
|
return 'You are banned.', 403
|
||||||
|
|
||||||
|
|
||||||
@bp.route('/rss', defaults={'rss': True})
|
@bp.route('/rss', defaults={'rss': True})
|
||||||
@bp.route('/', defaults={'rss': False})
|
@bp.route('/', defaults={'rss': False})
|
||||||
def home(rss):
|
def home(rss):
|
||||||
|
|
Loading…
Reference in a new issue