Add user and request context for admin class#125
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This pull request adds request and user context support to admin model classes, allowing admin methods to access the current request object and authenticated user within their implementation. The feature uses Python's ContextVar to maintain thread-safe, async-context-isolated storage of request and user objects.
Changes:
- Added
requestanduserproperties toBaseModelAdminusingContextVarfor async-safe context isolation - Updated all API service methods to bind request and user context to admin models before executing operations
- Extended framework integrations (Flask, FastAPI, Django) to pass request objects through to the service layer
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| fastadmin/models/base.py | Added _request_context and _user_context ContextVar attributes, request/user properties, and set_context() method to BaseModelAdmin |
| fastadmin/api/service.py | Added _bind_admin_context() helper and _get_authenticated_user() method; updated all API methods to accept and bind request/user context |
| fastadmin/models/helpers.py | Updated generate_models_schema() to accept and propagate request/user context to admin models and inlines |
| fastadmin/api/frameworks/flask/api.py | Updated all API endpoints to pass request=request to service methods |
| fastadmin/api/frameworks/fastapi/api.py | Updated all API endpoints to pass request=request to service methods |
| fastadmin/api/frameworks/django/app/api.py | Updated all API endpoints to pass request=request to service methods |
| tests/models/test_base.py | Added test for request/user context get/set functionality |
| tests/api/test_service.py | Added test verifying context binding in the list operation |
| tests/api/frameworks/flask/test_app.py | Updated dashboard widget test to verify request parameter passing |
| tests/api/frameworks/fastapi/test_app.py | Updated dashboard widget test to verify request parameter passing |
| tests/api/frameworks/django/test_app.py | Updated dashboard widget test to verify request parameter passing |
| docs/index.html | Added documentation and example code demonstrating context usage in admin methods |
| docs/build.py | Updated version changelog and added documentation content for the new feature |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
References
Summary
Are there any open tasks or blockers?