Skip to content

Add factory-pattern Object loaders for Admin Views #19699

@miketheman

Description

@miketheman

For admin routes, we're often a little less guarded, and can lead to 5xx errors in production.

An improvement could be to have a factory pattern on the routes, which pre-hydrates the object from the DB before entering the view, like we do for Users:

config.add_route(
"admin.user.detail",
"/admin/users/{username}/",
domain=warehouse,
factory="warehouse.accounts.models:UserFactory",
traverse="/{username}",
)

class UserFactory:
def __init__(self, request):
self.request = request
def __getitem__(self, username):
try:
return self.request.db.query(User).filter(User.username == username).one()
except NoResultFound:
raise KeyError from None

Originally posted by @miketheman in #19695 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    adminFeatures needed for the Admin UI (people running the site)

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions