-
Notifications
You must be signed in to change notification settings - Fork 136
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add search para for /admin/users/search
- Loading branch information
Showing
7 changed files
with
56 additions
and
57 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,36 @@ | ||
from fastapi import APIRouter, Depends | ||
from fastapi_pagination import Params, Page | ||
from fastapi_pagination.ext.sqlmodel import paginate | ||
from fastapi_pagination import Params | ||
from sqlmodel import select | ||
|
||
from app.api.deps import SessionDep, CurrentSuperuserDep | ||
from app.models import User | ||
|
||
from app.api.admin_routes.models import ( | ||
UserDescriptor, | ||
) | ||
from fuzzywuzzy import process | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.get("/admin/users") | ||
def list_users( | ||
@router.get("/admin/users/search") | ||
def search_users( | ||
session: SessionDep, | ||
user: CurrentSuperuserDep, | ||
search: str | None = None, | ||
params: Params = Depends(), | ||
) -> Page[User]: | ||
return paginate( | ||
session, | ||
select(User).order_by(User.id), | ||
params=params, | ||
) | ||
) -> list[UserDescriptor]: | ||
users = [] | ||
for user in session.exec(select(User).order_by(User.id)): | ||
users.append(UserDescriptor(id=user.id, email=user.email)) | ||
# when search is empty, return all users | ||
if not search: | ||
return users | ||
# when search is not empty, filter users by email | ||
emails = [user.email for user in users] | ||
matches = process.extract(search, emails, limit=len(emails)) | ||
|
||
threshold = 70 | ||
matched_emails = {match[0] for match in matches if match[1] >= threshold} | ||
|
||
return [user for user in users if user.email in matched_emails] |
This file contains 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
This file contains 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
This file contains 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
This file contains 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