Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change admin search_fields to favor USERNAME_FIELD instead of "email". #1428

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ Sandro Rodrigues
Shaheed Haque
Shaun Stanworth
Silvano Cerza
Simon Charette
Sora Yanai
Spencer Carroll
Stéphane Raimbault
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [unreleased]
### Added
### Changed
* #1428 Admin: changed `search_fields` to lookup usernames instead of user emails.
### Deprecated
### Removed
### Fixed
Expand Down
12 changes: 6 additions & 6 deletions oauth2_provider/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)


has_email = hasattr(get_user_model(), "email")
username_field = get_user_model().USERNAME_FIELD


class ApplicationAdmin(admin.ModelAdmin):
Expand All @@ -25,36 +25,36 @@ class ApplicationAdmin(admin.ModelAdmin):
"client_type": admin.HORIZONTAL,
"authorization_grant_type": admin.VERTICAL,
}
search_fields = ("name",) + (("user__email",) if has_email else ())
search_fields = ("name", f"user__{username_field}")
raw_id_fields = ("user",)


class AccessTokenAdmin(admin.ModelAdmin):
list_display = ("token", "user", "application", "expires")
list_select_related = ("application", "user")
raw_id_fields = ("user", "source_refresh_token")
search_fields = ("token",) + (("user__email",) if has_email else ())
search_fields = ("token", f"user__{username_field}")
list_filter = ("application",)


class GrantAdmin(admin.ModelAdmin):
list_display = ("code", "application", "user", "expires")
raw_id_fields = ("user",)
search_fields = ("code",) + (("user__email",) if has_email else ())
search_fields = ("code", f"user__{username_field}")


class IDTokenAdmin(admin.ModelAdmin):
list_display = ("jti", "user", "application", "expires")
raw_id_fields = ("user",)
search_fields = ("user__email",) if has_email else ()
search_fields = (f"user__{username_field}",)
list_filter = ("application",)
list_select_related = ("application", "user")


class RefreshTokenAdmin(admin.ModelAdmin):
list_display = ("token", "user", "application")
raw_id_fields = ("user", "access_token")
search_fields = ("token",) + (("user__email",) if has_email else ())
search_fields = ("token", f"user__{username_field}")
list_filter = ("application",)


Expand Down
Loading