Skip to content

Commit

Permalink
feat(base): add redirect_slashes option to mount_to method
Browse files Browse the repository at this point in the history
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 <noam@factify.com>
  • Loading branch information
noamsto committed Jan 9, 2025
1 parent f1c54e3 commit c2adafd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 6 additions & 1 deletion starlette_admin/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
4 changes: 2 additions & 2 deletions starlette_admin/contrib/sqla/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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:
Expand Down

0 comments on commit c2adafd

Please sign in to comment.