add paginate
This commit is contained in:
@@ -2,6 +2,7 @@ from typing import List
|
|||||||
from django.contrib.auth import authenticate, login, logout
|
from django.contrib.auth import authenticate, login, logout
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from ninja import Router
|
from ninja import Router
|
||||||
|
from ninja.pagination import paginate, PageNumberPagination
|
||||||
from ninja.errors import HttpError
|
from ninja.errors import HttpError
|
||||||
from .schemas import UserListSchema, UserRegistrationSchema, UserLoginSchema
|
from .schemas import UserListSchema, UserRegistrationSchema, UserLoginSchema
|
||||||
from .models import RoleChoices, User
|
from .models import RoleChoices, User
|
||||||
@@ -49,9 +50,12 @@ def my_profile(request):
|
|||||||
|
|
||||||
@router.get("/list", response=List[UserListSchema])
|
@router.get("/list", response=List[UserListSchema])
|
||||||
@super_required
|
@super_required
|
||||||
def list(request, username: str):
|
@paginate
|
||||||
# 之后加上分页
|
def list(request, username: str, role: str = None):
|
||||||
|
# 用户列表
|
||||||
users = User.objects.filter(username__icontains=username)
|
users = User.objects.filter(username__icontains=username)
|
||||||
|
if role:
|
||||||
|
users = users.filter(role=role)
|
||||||
return [UserListSchema.from_orm(user) for user in users]
|
return [UserListSchema.from_orm(user) for user in users]
|
||||||
|
|
||||||
|
|
||||||
@@ -60,3 +64,18 @@ def list(request, username: str):
|
|||||||
def batch_create(request):
|
def batch_create(request):
|
||||||
# 批量创建账号
|
# 批量创建账号
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
@router.put("/active/{id}")
|
||||||
|
@super_required
|
||||||
|
def toggle_user_is_active(request, id: int):
|
||||||
|
# 封号和解封
|
||||||
|
try:
|
||||||
|
user = User.objects.get(id=id)
|
||||||
|
user.is_active = not user.is_active
|
||||||
|
user.save()
|
||||||
|
return {
|
||||||
|
"message": f"{user.username} {'解封' if user.is_active else '封号'}成功"
|
||||||
|
}
|
||||||
|
except User.DoesNotExist:
|
||||||
|
raise HttpError(404, "查无此人")
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ class UserListSchema(ModelSchema):
|
|||||||
def from_orm(cls, obj):
|
def from_orm(cls, obj):
|
||||||
raw_password = obj.raw_password if obj.role == RoleChoices.NORMAL else ""
|
raw_password = obj.raw_password if obj.role == RoleChoices.NORMAL else ""
|
||||||
return cls(
|
return cls(
|
||||||
|
id=obj.id,
|
||||||
username=obj.username,
|
username=obj.username,
|
||||||
raw_password=raw_password,
|
raw_password=raw_password,
|
||||||
role=obj.role,
|
role=obj.role,
|
||||||
@@ -20,6 +21,7 @@ class UserListSchema(ModelSchema):
|
|||||||
class Meta:
|
class Meta:
|
||||||
model = User
|
model = User
|
||||||
fields = [
|
fields = [
|
||||||
|
"id",
|
||||||
"username",
|
"username",
|
||||||
"raw_password",
|
"raw_password",
|
||||||
"role",
|
"role",
|
||||||
|
|||||||
@@ -163,3 +163,7 @@ else:
|
|||||||
CORS_ALLOWED_ORIGINS = ["https://web.xuyue.cc"]
|
CORS_ALLOWED_ORIGINS = ["https://web.xuyue.cc"]
|
||||||
|
|
||||||
CORS_ALLOW_CREDENTIALS = True
|
CORS_ALLOW_CREDENTIALS = True
|
||||||
|
|
||||||
|
NINJA_PAGINATION_CLASS = "ninja.pagination.PageNumberPagination"
|
||||||
|
|
||||||
|
NINJA_PAGINATION_PER_PAGE = 20
|
||||||
|
|||||||
Reference in New Issue
Block a user