Skip to content

Commit

Permalink
fix:标注搜索日志权限,追加time log字段(resolve #144)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunlin92 committed May 18, 2021
1 parent 2cfe42a commit 123f748
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
49 changes: 46 additions & 3 deletions app/api/cms/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@


@log_api.route("")
@log_api.route("/search")
@permission_meta(name="查询日志", module="日志")
# @group_required
@group_required
@api.validate(
headers=AuthorizationSchema,
query=LogQuerySearchSchema,
Expand All @@ -34,6 +33,36 @@ def get_logs():
"""
日志浏览查询(人员,时间, 关键字),分页展示
"""
logs = Log.query.filter()
total = logs.count()
items = (
logs.order_by(text("create_time desc")).offset(g.offset).limit(g.count).all()
)
total_page = math.ceil(total / g.count)

return LogPageSchema(
page=g.page,
count=g.count,
total=total,
items=get_items_with_time_field(items),
total_page=total_page,
)


@log_api.route("/search")
@permission_meta(name="搜索日志", module="日志")
@group_required
@api.validate(
headers=AuthorizationSchema,
query=LogQuerySearchSchema,
resp=DocResponse(r=LogPageSchema),
before=LogQuerySearchSchema.offset_handler,
tags=["日志"],
)
def search_logs():
"""
日志搜索(人员,时间, 关键字),分页展示
"""
if g.keyword:
logs = Log.query.filter(Log.message.like(f"%{g.keyword}%"))
else:
Expand All @@ -50,7 +79,11 @@ def get_logs():
total_page = math.ceil(total / g.count)

return LogPageSchema(
page=g.page, count=g.count, total=total, items=items, total_page=total_page
page=g.page,
count=g.count,
total=total,
items=get_items_with_time_field(items),
total_page=total_page,
)


Expand All @@ -74,3 +107,13 @@ def get_users_for_log():
.all()
)
return UsernameListSchema(items=[u.username for u in usernames])


# TODO:临时time字段, 等待lin 核心库中调整后移除
def get_items_with_time_field(items):
new_items = list()
for item in items:
item = dict(item)
item["time"] = item["create_time"]
new_items.append(item)
return new_items
2 changes: 2 additions & 0 deletions app/validator/schema.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import re
from datetime import datetime
from enum import Enum
from typing import Any, List, Optional

Expand Down Expand Up @@ -49,6 +50,7 @@ class LogSchema(BaseModel):
method: str
path: str
permission: str
time: datetime


class BasePageSchema(BaseModel):
Expand Down

0 comments on commit 123f748

Please sign in to comment.