You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
89 lines
3.4 KiB
89 lines
3.4 KiB
|
10 months ago
|
# -*- coding: utf-8 -*-
|
||
|
|
import logging
|
||
|
|
import traceback
|
||
|
|
|
||
|
|
from rest_framework.permissions import IsAuthenticated
|
||
|
|
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
|
||
|
|
from rest_framework.views import APIView
|
||
|
|
from django.core.paginator import Paginator
|
||
|
|
from hashids import Hashids
|
||
|
|
|
||
|
|
from ChaCeRndTrans.basic import CCAIResponse
|
||
|
|
from utils.custom import RbacPermission
|
||
|
|
from ChaCeRndTrans.settings import ID_KEY
|
||
|
|
from common.models import OperationHistoryLog
|
||
|
|
|
||
|
|
err_logger = logging.getLogger('error')
|
||
|
|
|
||
|
|
|
||
|
|
class HistoryLogAPIView(APIView):
|
||
|
|
"""
|
||
|
|
操作日志
|
||
|
|
"""
|
||
|
|
perms_map = ({"*": "admin"},)
|
||
|
|
authentication_classes = (JSONWebTokenAuthentication,)
|
||
|
|
permission_classes = (IsAuthenticated, RbacPermission)
|
||
|
|
|
||
|
|
def get(self, request, *args, **kwargs):
|
||
|
|
try:
|
||
|
|
hashids = Hashids(salt=ID_KEY, min_length=32) # 对ID加密再返回
|
||
|
|
params = request.GET
|
||
|
|
page = params.get("page")
|
||
|
|
page_size = params.get("size")
|
||
|
|
des = params.get("des")
|
||
|
|
username = params.get("username")
|
||
|
|
ip = params.get("ip")
|
||
|
|
create_time = params.get("create_time")
|
||
|
|
detail = params.get("detail")
|
||
|
|
|
||
|
|
if page != "" and page is not None:
|
||
|
|
page = int(page)
|
||
|
|
else:
|
||
|
|
page = 1
|
||
|
|
if page_size != "" and page_size is not None:
|
||
|
|
page_size = int(page_size)
|
||
|
|
if page_size > 100:
|
||
|
|
page_size = 10
|
||
|
|
else:
|
||
|
|
page_size = 10
|
||
|
|
|
||
|
|
query_result = OperationHistoryLog.objects.all().order_by("-create_time")
|
||
|
|
if des != "" and des is not None:
|
||
|
|
query_result = query_result.filter(des__icontains=des)
|
||
|
|
if username != "" and username is not None:
|
||
|
|
query_result = query_result.filter(username__icontains=username)
|
||
|
|
if detail != "" and detail is not None:
|
||
|
|
query_result = query_result.filter(detail__icontains=detail)
|
||
|
|
if ip != "" and ip is not None:
|
||
|
|
query_result = query_result.filter(ip__icontains=ip)
|
||
|
|
if create_time != "" and create_time is not None:
|
||
|
|
end_time = create_time.split(" ")[0]
|
||
|
|
end_time = end_time + " " + "23:59:59"
|
||
|
|
query_result = query_result.filter(create_time__range=[create_time, end_time])
|
||
|
|
|
||
|
|
p = Paginator(query_result, page_size)
|
||
|
|
try:
|
||
|
|
query = p.page(page).object_list.values()
|
||
|
|
except Exception as e:
|
||
|
|
query = p.page(p.num_pages).object_list.values()
|
||
|
|
page = p.num_pages
|
||
|
|
|
||
|
|
data = []
|
||
|
|
for each in query:
|
||
|
|
each['create_time'] = each['create_time'].strftime('%Y-%m-%d %H:%M:%S')
|
||
|
|
each['id'] = hashids.encode(each["id"])
|
||
|
|
del each["update_time"], each["user_id"], each["update_user_id"], each["update_username"]
|
||
|
|
data.append(each)
|
||
|
|
|
||
|
|
pagination = {
|
||
|
|
page: page,
|
||
|
|
page_size: page_size
|
||
|
|
}
|
||
|
|
count = p.count
|
||
|
|
return CCAIResponse(data=data, pagination=pagination, count=count, status=200)
|
||
|
|
|
||
|
|
except Exception as e:
|
||
|
|
err_logger.error("user: %s, get history log failed: \n%s" % (
|
||
|
|
request.user.id, traceback.format_exc()))
|
||
|
|
return CCAIResponse("获取操作日志失败", status=500)
|