Skip to content

Commit

Permalink
Restructure application routing (#1141)
Browse files Browse the repository at this point in the history
## Fixes issue
Sets the groundwork for this issue:
#385

## Description of Changes
Simplifying the import logic for the `auth` and `main` routes of the
application. This also sets up the work for this PR:
#1138

## Tests and Linting
- [x] This branch is up-to-date with the `develop` branch.
- [x] `pytest` passes on my local development environment.
- [x] `pre-commit` passes on my local development environment.
  • Loading branch information
michplunkett authored Jan 6, 2025
1 parent a6e078e commit fe6e054
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 32 deletions.
23 changes: 7 additions & 16 deletions OpenOversight/app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()


Expand All @@ -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
Expand All @@ -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
Expand Down
6 changes: 0 additions & 6 deletions OpenOversight/app/auth/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
from flask import Blueprint


auth = Blueprint("auth", __name__)

from . import views # noqa: E402,F401
5 changes: 3 additions & 2 deletions OpenOversight/app/auth/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from http import HTTPMethod, HTTPStatus

from flask import (
Blueprint,
current_app,
flash,
redirect,
Expand All @@ -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,
Expand All @@ -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 = []

Expand Down
6 changes: 0 additions & 6 deletions OpenOversight/app/main/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +0,0 @@
from flask import Blueprint


main = Blueprint("main", __name__)

from OpenOversight.app.main import views # noqa: E402,F401
5 changes: 3 additions & 2 deletions OpenOversight/app/main/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing import Optional

from flask import (
Blueprint,
Response,
abort,
current_app,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -123,6 +123,7 @@
)


main = Blueprint("main", __name__)
sitemap_endpoints = []


Expand Down
10 changes: 10 additions & 0 deletions OpenOversight/app/utils/flask.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit fe6e054

Please sign in to comment.