mirror of
https://github.com/wassname/Open-Assistant.git
synced 2026-07-25 13:00:24 +08:00
add /by_display_name user query & created_date to FrontEndUser model
This commit is contained in:
@@ -17,16 +17,33 @@ router = APIRouter()
|
||||
|
||||
@router.get("/", response_model=list[protocol.FrontEndUser])
|
||||
def get_users(
|
||||
api_client_id: Optional[UUID] = None,
|
||||
max_count: Optional[int] = Query(100, gt=0, le=10000),
|
||||
gte: Optional[str] = None,
|
||||
lt: Optional[str] = None,
|
||||
auth_method: Optional[str] = None,
|
||||
api_client: ApiClient = Depends(deps.get_api_client),
|
||||
db: Session = Depends(deps.get_db),
|
||||
):
|
||||
ur = UserRepository(db, api_client)
|
||||
users = ur.query_users(api_client_id=api_client_id, limit=max_count, gte=gte, lt=lt, auth_method=auth_method)
|
||||
return [u.to_protocol_frontend_user() for u in users]
|
||||
|
||||
|
||||
@router.get("/by_display_name")
|
||||
def query_frontend_users_by_display_name(
|
||||
search_text: str,
|
||||
exact: bool = False,
|
||||
api_client_id: UUID = None,
|
||||
max_count: int = Query(100, gt=0, le=10000),
|
||||
gte: str = None,
|
||||
lt: str = None,
|
||||
max_count: int = Query(20, gt=0, le=1000),
|
||||
auth_method: str = None,
|
||||
api_client: ApiClient = Depends(deps.get_api_client),
|
||||
db: Session = Depends(deps.get_db),
|
||||
):
|
||||
pr = UserRepository(db, api_client)
|
||||
users = pr.query_users(api_client_id=api_client_id, limit=max_count, gte=gte, lt=lt, auth_method=auth_method)
|
||||
ur = UserRepository(db, api_client)
|
||||
users = ur.query_users_by_display_name(
|
||||
search_text=search_text, exact=exact, api_client_id=api_client_id, limit=max_count, auth_method=auth_method
|
||||
)
|
||||
return [u.to_protocol_frontend_user() for u in users]
|
||||
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ def get_user(
|
||||
"""
|
||||
ur = UserRepository(db, api_client)
|
||||
user: User = ur.get_user(user_id, api_client_id)
|
||||
return protocol.User(user.username, user.display_name, user.auth_method)
|
||||
return user.to_protocol_frontend_user()
|
||||
|
||||
|
||||
@router.put("/users/{user_id}", status_code=HTTP_204_NO_CONTENT)
|
||||
|
||||
@@ -37,4 +37,5 @@ class User(SQLModel, table=True):
|
||||
enabled=self.enabled,
|
||||
deleted=self.deleted,
|
||||
notes=self.notes,
|
||||
created_date=self.created_date,
|
||||
)
|
||||
|
||||
@@ -193,3 +193,34 @@ class UserRepository:
|
||||
users = users.limit(limit)
|
||||
|
||||
return users.all()
|
||||
|
||||
def query_users_by_display_name(
|
||||
self,
|
||||
search_text: str,
|
||||
exact: Optional[bool] = False,
|
||||
limit: Optional[int] = 20,
|
||||
api_client_id: Optional[UUID] = None,
|
||||
auth_method: Optional[str] = None,
|
||||
) -> list[User]:
|
||||
if not self.api_client.trusted:
|
||||
if not api_client_id:
|
||||
api_client_id = self.api_client.id
|
||||
|
||||
if api_client_id != self.api_client.id:
|
||||
raise OasstError("Forbidden", OasstErrorCode.API_CLIENT_NOT_AUTHORIZED, HTTP_403_FORBIDDEN)
|
||||
|
||||
qry = self.db.query(User).order_by(User.display_name)
|
||||
|
||||
if exact:
|
||||
qry = qry.filter(User.display_name == search_text)
|
||||
else:
|
||||
pattern = "%{}%".format(search_text.replace("\\", "\\\\").replace("_", "\\_").replace("%", "\\%"))
|
||||
qry = qry.filter(User.display_name.like(pattern))
|
||||
|
||||
if auth_method:
|
||||
qry = qry.filter(User.auth_method == auth_method)
|
||||
|
||||
if limit is not None:
|
||||
qry = qry.limit(limit)
|
||||
|
||||
return qry.all()
|
||||
|
||||
@@ -34,6 +34,7 @@ class FrontEndUser(User):
|
||||
enabled: bool
|
||||
deleted: bool
|
||||
notes: str
|
||||
created_date: Optional[datetime] = None
|
||||
|
||||
|
||||
class ConversationMessage(BaseModel):
|
||||
|
||||
Reference in New Issue
Block a user