From c2adafdcd63615f3cf9627ae27ce6cebff7347d4 Mon Sep 17 00:00:00 2001 From: Noam Stolero Date: Thu, 9 Jan 2025 14:07:19 +0200 Subject: [PATCH] feat(base): add redirect_slashes option to mount_to method Add optional redirect_slashes parameter to BaseAdmin.mount_to() and propagate it to SQLAlchemy Admin class. This allows users to control whether trailing slashes in URLs should be redirected, providing more flexibility in URL handling. Signed-off-by: Noam Stolero --- starlette_admin/base.py | 7 ++++++- starlette_admin/contrib/sqla/admin.py | 4 ++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/starlette_admin/base.py b/starlette_admin/base.py index fccf074c..ea6cf1eb 100644 --- a/starlette_admin/base.py +++ b/starlette_admin/base.py @@ -524,7 +524,11 @@ async def form_to_dict( data[field.name] = await field.parse_form_data(request, form_data, action) return data - def mount_to(self, app: Starlette) -> None: + def mount_to( + self, + app: Starlette, + redirect_slashes: bool = True, + ) -> None: admin_app = Starlette( routes=self.routes, middleware=self.middlewares, @@ -537,3 +541,4 @@ def mount_to(self, app: Starlette) -> None: app=admin_app, name=self.route_name, ) + admin_app.router.redirect_slashes = redirect_slashes diff --git a/starlette_admin/contrib/sqla/admin.py b/starlette_admin/contrib/sqla/admin.py index dfb0317f..8ca29f0d 100644 --- a/starlette_admin/contrib/sqla/admin.py +++ b/starlette_admin/contrib/sqla/admin.py @@ -57,7 +57,7 @@ def __init__( self.middlewares = [] if self.middlewares is None else list(self.middlewares) self.middlewares.insert(0, Middleware(DBSessionMiddleware, engine=engine)) - def mount_to(self, app: Starlette) -> None: + def mount_to(self, app: Starlette, redirect_slashes: bool = True) -> None: try: """Automatically add route to serve sqlalchemy_file files""" __import__("sqlalchemy_file") @@ -71,7 +71,7 @@ def mount_to(self, app: Starlette) -> None: ) except ImportError: # pragma: no cover pass - super().mount_to(app) + super().mount_to(app, redirect_slashes=redirect_slashes) def _serve_file(request: Request) -> Response: