feat(users): Integrate real-time online status and enhance UI
This commit is contained in:
@ -4,19 +4,21 @@ from pydantic import BaseModel, RootModel, Field, field_validator
|
||||
|
||||
|
||||
class UserInfoResponse(BaseModel):
|
||||
username: str
|
||||
password: str
|
||||
max_download_bytes: int
|
||||
expiration_days: int
|
||||
account_creation_date: str
|
||||
account_creation_date: Optional[str] = None
|
||||
blocked: bool
|
||||
unlimited_ip: bool = Field(False, alias='unlimited_user')
|
||||
status: Optional[str] = None
|
||||
upload_bytes: Optional[int] = None
|
||||
download_bytes: Optional[int] = None
|
||||
online_count: int = 0
|
||||
|
||||
|
||||
class UserListResponse(RootModel):
|
||||
root: dict[str, UserInfoResponse]
|
||||
root: List[UserInfoResponse]
|
||||
|
||||
class UsernamesRequest(BaseModel):
|
||||
usernames: List[str]
|
||||
|
||||
@ -1,4 +1,3 @@
|
||||
|
||||
from fastapi import APIRouter, HTTPException, Request, Depends
|
||||
from fastapi.templating import Jinja2Templates
|
||||
|
||||
@ -13,10 +12,8 @@ router = APIRouter()
|
||||
@router.get('/')
|
||||
async def users(request: Request, templates: Jinja2Templates = Depends(get_templates)):
|
||||
try:
|
||||
dict_users = cli_api.list_users() # type: ignore
|
||||
users: list[User] = []
|
||||
if dict_users:
|
||||
users: list[User] = [User.from_dict(key, value) for key, value in dict_users.items()] # type: ignore
|
||||
users_list = cli_api.list_users() or []
|
||||
users: list[User] = [User.from_dict(user_data.get('username', ''), user_data) for user_data in users_list]
|
||||
|
||||
return templates.TemplateResponse('users.html', {'users': users, 'request': request})
|
||||
except Exception as e:
|
||||
|
||||
@ -11,6 +11,7 @@ class User(BaseModel):
|
||||
expiry_days: str
|
||||
enable: bool
|
||||
unlimited_ip: bool
|
||||
online_count: int = 0
|
||||
|
||||
@staticmethod
|
||||
def from_dict(username: str, user_data: dict):
|
||||
@ -36,7 +37,8 @@ class User(BaseModel):
|
||||
'expiry_date': 'N/A',
|
||||
'expiry_days': 'N/A',
|
||||
'enable': False,
|
||||
'unlimited_ip': False
|
||||
'unlimited_ip': False,
|
||||
'online_count': 0
|
||||
}
|
||||
|
||||
expiration_days = user_data.get('expiration_days', 0)
|
||||
@ -77,7 +79,8 @@ class User(BaseModel):
|
||||
'expiry_date': display_expiry_date,
|
||||
'expiry_days': display_expiry_days,
|
||||
'enable': not user_data.get('blocked', False),
|
||||
'unlimited_ip': user_data.get('unlimited_user', False)
|
||||
'unlimited_ip': user_data.get('unlimited_user', False),
|
||||
'online_count': user_data.get('online_count', 0)
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
|
||||
Reference in New Issue
Block a user