diff --git a/OpenOversight/app/__init__.py b/OpenOversight/app/__init__.py index 69ebd3fbd..23f22a4d7 100644 --- a/OpenOversight/app/__init__.py +++ b/OpenOversight/app/__init__.py @@ -6,19 +6,19 @@ from flask import Flask, jsonify, render_template, request from flask_bootstrap import Bootstrap5 from flask_compress import Compress -from flask_limiter import Limiter -from flask_limiter.util import get_remote_address from flask_login import LoginManager from flask_migrate import Migrate -from flask_sitemap import Sitemap from flask_wtf.csrf import CSRFProtect +from OpenOversight.app.auth.views import auth as auth_blueprint from OpenOversight.app.email_client import EmailClient from OpenOversight.app.filters import instantiate_filters +from OpenOversight.app.main.views import main as main_blueprint from OpenOversight.app.models.config import config from OpenOversight.app.models.database import db from OpenOversight.app.models.users import AnonymousUser from OpenOversight.app.utils.constants import MEGABYTE +from OpenOversight.app.utils.flask import limiter, sitemap bootstrap = Bootstrap5() @@ -29,11 +29,6 @@ login_manager.anonymous_user = AnonymousUser login_manager.login_view = "auth.login" -limiter = Limiter( - key_func=get_remote_address, default_limits=["100 per minute", "5 per second"] -) - -sitemap = Sitemap() csrf = CSRFProtect() @@ -52,15 +47,11 @@ def create_app(config_name="default"): sitemap.init_app(app) compress.init_app(app) - from OpenOversight.app.main import main as main_blueprint - + # Register Blueprints for application routes + app.register_blueprint(auth_blueprint) app.register_blueprint(main_blueprint) - from OpenOversight.app.auth import auth as auth_blueprint - - app.register_blueprint(auth_blueprint, url_prefix="/auth") - - max_log_size = 10 * MEGABYTE # start new log file after 10 MB + max_log_size = 10 * MEGABYTE # Start new log file after 10 MB num_logs_to_keep = 5 file_handler = RotatingFileHandler( "/tmp/openoversight.log", "a", max_log_size, num_logs_to_keep @@ -77,7 +68,7 @@ def create_app(config_name="default"): app.logger.addHandler(file_handler) app.logger.info("OpenOversight startup") - # Also log when endpoints are getting hit hard + # Log when endpoints are getting hit hard limiter.logger.addHandler(file_handler) # Define error handlers diff --git a/OpenOversight/app/auth/__init__.py b/OpenOversight/app/auth/__init__.py index 09585ca9a..e69de29bb 100644 --- a/OpenOversight/app/auth/__init__.py +++ b/OpenOversight/app/auth/__init__.py @@ -1,6 +0,0 @@ -from flask import Blueprint - - -auth = Blueprint("auth", __name__) - -from . import views # noqa: E402,F401 diff --git a/OpenOversight/app/auth/views.py b/OpenOversight/app/auth/views.py index 8a3fc0a26..88e509e8e 100644 --- a/OpenOversight/app/auth/views.py +++ b/OpenOversight/app/auth/views.py @@ -2,6 +2,7 @@ from http import HTTPMethod, HTTPStatus from flask import ( + Blueprint, current_app, flash, redirect, @@ -12,8 +13,6 @@ ) from flask_login import current_user, login_required, login_user, logout_user -from OpenOversight.app import sitemap -from OpenOversight.app.auth import auth from OpenOversight.app.auth.forms import ( ChangeDefaultDepartmentForm, ChangeEmailForm, @@ -36,10 +35,12 @@ ) from OpenOversight.app.utils.auth import admin_required from OpenOversight.app.utils.constants import KEY_APPROVE_REGISTRATIONS +from OpenOversight.app.utils.flask import sitemap from OpenOversight.app.utils.forms import set_dynamic_default from OpenOversight.app.utils.general import validate_redirect_url +auth = Blueprint("auth", __name__, url_prefix="/auth") js_loads = ["js/zxcvbn.js", "js/password.js"] sitemap_endpoints = [] diff --git a/OpenOversight/app/main/__init__.py b/OpenOversight/app/main/__init__.py index 34232f053..e69de29bb 100644 --- a/OpenOversight/app/main/__init__.py +++ b/OpenOversight/app/main/__init__.py @@ -1,6 +0,0 @@ -from flask import Blueprint - - -main = Blueprint("main", __name__) - -from OpenOversight.app.main import views # noqa: E402,F401 diff --git a/OpenOversight/app/main/views.py b/OpenOversight/app/main/views.py index 6c5fb10d8..8d8cfc024 100644 --- a/OpenOversight/app/main/views.py +++ b/OpenOversight/app/main/views.py @@ -6,6 +6,7 @@ from typing import Optional from flask import ( + Blueprint, Response, abort, current_app, @@ -23,9 +24,7 @@ from sqlalchemy.orm import contains_eager, joinedload, selectinload from sqlalchemy.orm.exc import NoResultFound -from OpenOversight.app import limiter, sitemap from OpenOversight.app.auth.forms import LoginForm -from OpenOversight.app.main import main from OpenOversight.app.main.downloads import ( assignment_record_maker, descriptions_record_maker, @@ -100,6 +99,7 @@ dept_choices, unit_choices, ) +from OpenOversight.app.utils.flask import limiter, sitemap from OpenOversight.app.utils.forms import ( add_new_assignment, add_officer_profile, @@ -123,6 +123,7 @@ ) +main = Blueprint("main", __name__) sitemap_endpoints = [] diff --git a/OpenOversight/app/utils/flask.py b/OpenOversight/app/utils/flask.py new file mode 100644 index 000000000..f1836d0f8 --- /dev/null +++ b/OpenOversight/app/utils/flask.py @@ -0,0 +1,10 @@ +from flask_limiter import Limiter +from flask_limiter.util import get_remote_address +from flask_sitemap import Sitemap + + +limiter: Limiter = Limiter( + key_func=get_remote_address, default_limits=["100 per minute", "5 per second"] +) + +sitemap = Sitemap()