Files
DjangoAdmin_Flask_EleVue/application/models/operation_log.py
T
DjangoAdmin DevandClaude 658ee6d596 refactor: P2 core+common 建设(应用工厂/扩展/工具层迁入 src/)
【结构】
- src/core/:database/response/jwt/password/security/redis_client/mail/scheduler
- src/common/:base_model/base_db/forms(base_form+fields)/constants/utils
- src/models_registry.py:模型聚合器(Flask-Migrate 全量表识别)

【绞杀者 shim】extensions/、utils/、application/constants|forms|models 打兼容转发
【兼容修复】应用工厂抽取至 application/factory.py(P2 前置,P1 已完成)

【基线修复】application/models(25) + middleware(7) 原被嵌套 .gitignore(*)
误排除出版本库,已补入(base_model/base_db 现为 shim,原版在 src/common/)

【登记既有缺陷】
1. flask db CLI 因 .env FLASK_VERSION 与 --version 冲突报错 → Makefile 已加 FLASK_SKIP_DOTENV=1
2. 本机 Redis 未设密码但 REDIS_AUTH=True,/captcha 报 AUTH 错误(环境问题)

【验收】22 模型表全量识别;src/ flake8 零告警;pytest 5 passed;应用启动;
/login 表单校验、/user/page 鉴权拦截冒烟正常

Co-Authored-By: Claude <noreply@anthropic.com>
2026-08-07 13:20:46 +08:00

196 lines
5.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
操作日志模型模块
=============================================================
提供操作日志管理的数据模型定义,用于记录用户操作行为日志。
"""
from application.models.base_db import BaseDB
from application.models.base_model import BaseModel
from config import DB_PREFIX
from extensions import db
from utils.case_converter import convert_camel_to_snake
# ============================================================
# 操作日志模型
# ============================================================
class OperationLog(BaseModel, BaseDB):
"""
操作日志模型
用于记录用户操作行为的详细信息,包括:操作标题、操作类型、
来源、请求信息、IP地址、归属地、设备信息、请求耗时、操作状态等。
Attributes:
id: 主键ID(继承自 BaseModel)
title: 请求标题(String(150),必填)
type: 请求类型(Integer,必填)
source: 请求来源(Integer,必填,0-后台,1-前台)
method: 请求方法(String(100),必填)
request_method: 请求方式(String(255),必填)
url: 请求URL(String(1000),必填)
param: 请求参数(Text,必填)
result: 请求结果(Text,必填)
ip: 请求IP(String(100),必填)
location: 请求所在区域(String(255),必填)
os: 操作系统(String(100),必填)
browser: 浏览器(String(100),必填)
consume_time: 请求耗时(Integer,必填,单位毫秒)
status: 请求状态(Integer,必填,0-正常,1-异常)
error: 错误描述(Text,必填)
"""
# ============================================================
# 表配置
# ============================================================
# 设置表名
__tablename__ = DB_PREFIX + "operation_log"
# ============================================================
# 字段定义
# ============================================================
# 请求标题
title = db.Column(
db.String(150),
nullable=False,
comment="请求标题"
)
# 请求类型
type = db.Column(
db.Integer,
nullable=False,
comment="请求类型"
)
# 请求来源:0-后台,1-前台
source = db.Column(
db.Integer,
nullable=False,
comment="请求来源:0-后台,1-前台"
)
# 请求方法(如:UserController.add)
method = db.Column(
db.String(100),
nullable=False,
comment="请求方法"
)
# 请求方式(如:GET、POST、PUT、DELETE)
request_method = db.Column(
db.String(255),
nullable=False,
comment="请求方式"
)
# 请求URL
url = db.Column(
db.String(1000),
nullable=False,
comment="请求URL"
)
# 请求参数
param = db.Column(
db.Text,
nullable=False,
comment="请求参数"
)
# 请求结果
result = db.Column(
db.Text,
nullable=False,
comment="请求结果"
)
# 请求IP
ip = db.Column(
db.String(100),
nullable=False,
comment="请求IP"
)
# 请求所在区域
location = db.Column(
db.String(255),
nullable=False,
comment="请求所在区域"
)
# 操作系统
os = db.Column(
db.String(100),
nullable=False,
comment="操作系统"
)
# 浏览器
browser = db.Column(
db.String(100),
nullable=False,
comment="浏览器"
)
# 请求耗时(毫秒)
consume_time = db.Column(
db.BigInteger,
nullable=False,
comment="请求耗时"
)
# 请求状态:0-正常,1-异常
status = db.Column(
db.Integer,
nullable=False,
comment="请求状态:0-正常,1-异常"
)
# 错误描述
error = db.Column(
db.Text,
nullable=False,
comment="错误描述"
)
# ============================================================
# 常量定义
# ============================================================
class Source:
"""请求来源常量"""
ADMIN = 0 # 后台
FRONT = 1 # 前台
class Status:
"""请求状态常量"""
NORMAL = 0 # 正常
ERROR = 1 # 异常
# ============================================================
# 魔法方法
# ============================================================
def __repr__(self):
"""
返回模型的字符串表示
Returns:
str: 格式为 "操作日志:{title} - {method}" 的字符串
"""
return '操作日志:{} - {}'.format(self.title, self.method)
def __str__(self):
"""
返回模型的字符串表示
Returns:
str: 格式为 "操作日志:{title} - {method}" 的字符串
"""
return self.__repr__()