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>
This commit is contained in:
co-authored by
Claude
parent
92130ef070
commit
658ee6d596
@@ -0,0 +1,201 @@
|
||||
"""
|
||||
登录日志模型模块
|
||||
=============================================================
|
||||
|
||||
提供登录日志管理的数据模型定义,用于记录用户登录、退出等操作日志。
|
||||
"""
|
||||
|
||||
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 LoginLog(BaseModel, BaseDB):
|
||||
"""
|
||||
登录日志模型
|
||||
|
||||
用于记录用户登录和退出操作的详细信息,包括:登录账号、日志类型、
|
||||
来源、请求信息、IP地址、归属地、设备信息、请求耗时、操作状态等。
|
||||
|
||||
Attributes:
|
||||
id: 主键ID(继承自 BaseModel)
|
||||
username: 登录账号(String(50),必填)
|
||||
log_type: 日志类型(Integer,1-登录,2-退出,必填)
|
||||
source: 登录来源(Integer,0-后台,1-前台,必填)
|
||||
method: 请求方法(String(10),必填)
|
||||
request_method: 请求方式(String(255),必填)
|
||||
url: 请求URL(String(500),必填)
|
||||
param: 请求参数(Text,可选)
|
||||
result: 返回结果(Text,必填)
|
||||
ip: IP地址(String(50),必填)
|
||||
location: IP归属地(String(100),必填)
|
||||
os: 操作系统(String(100),必填)
|
||||
browser: 浏览器(String(100),必填)
|
||||
consume_time: 请求耗时(Integer,必填)
|
||||
status: 操作状态(Integer,0-正常,1-异常,必填)
|
||||
error: 错误信息(Text,可选)
|
||||
"""
|
||||
|
||||
# ============================================================
|
||||
# 表配置
|
||||
# ============================================================
|
||||
|
||||
# 设置表名
|
||||
__tablename__ = DB_PREFIX + "login_log"
|
||||
|
||||
# ============================================================
|
||||
# 字段定义
|
||||
# ============================================================
|
||||
|
||||
# 登录账号
|
||||
username = db.Column(
|
||||
db.String(50),
|
||||
nullable=False,
|
||||
comment="登录账号"
|
||||
)
|
||||
|
||||
# 日志类型:1-登录,2-退出
|
||||
log_type = db.Column(
|
||||
db.Integer,
|
||||
nullable=False,
|
||||
comment="日志类型:1-登录,2-退出"
|
||||
)
|
||||
|
||||
# 登录来源:0-后台,1-前台
|
||||
source = db.Column(
|
||||
db.Integer,
|
||||
nullable=False,
|
||||
comment="登录来源:0-后台,1-前台"
|
||||
)
|
||||
|
||||
# 请求方法(HTTP方法,如POST/GET)
|
||||
method = db.Column(
|
||||
db.String(10),
|
||||
nullable=False,
|
||||
comment="请求方法"
|
||||
)
|
||||
|
||||
# 请求方式(视图方法路径,如application.views.login.logout)
|
||||
request_method = db.Column(
|
||||
db.String(255),
|
||||
nullable=False,
|
||||
comment="请求方式"
|
||||
)
|
||||
|
||||
# 请求URL
|
||||
url = db.Column(
|
||||
db.String(500),
|
||||
nullable=False,
|
||||
comment="请求URL"
|
||||
)
|
||||
|
||||
# 请求参数
|
||||
param = db.Column(
|
||||
db.Text,
|
||||
nullable=True,
|
||||
comment="请求参数"
|
||||
)
|
||||
|
||||
# 返回结果
|
||||
result = db.Column(
|
||||
db.Text,
|
||||
nullable=False,
|
||||
comment="返回结果"
|
||||
)
|
||||
|
||||
# IP地址
|
||||
ip = db.Column(
|
||||
db.String(50),
|
||||
nullable=False,
|
||||
comment="IP地址"
|
||||
)
|
||||
|
||||
# IP归属地
|
||||
location = db.Column(
|
||||
db.String(100),
|
||||
nullable=False,
|
||||
comment="IP归属地"
|
||||
)
|
||||
|
||||
# 操作系统
|
||||
os = db.Column(
|
||||
db.String(100),
|
||||
nullable=False,
|
||||
comment="操作系统"
|
||||
)
|
||||
|
||||
# 浏览器
|
||||
browser = db.Column(
|
||||
db.String(100),
|
||||
nullable=False,
|
||||
comment="浏览器"
|
||||
)
|
||||
|
||||
# 请求耗时(毫秒)
|
||||
consume_time = db.Column(
|
||||
db.Integer,
|
||||
nullable=False,
|
||||
comment="请求耗时"
|
||||
)
|
||||
|
||||
# 操作状态:0-正常,1-异常
|
||||
status = db.Column(
|
||||
db.Integer,
|
||||
nullable=False,
|
||||
comment="操作状态:0-正常,1-异常"
|
||||
)
|
||||
|
||||
# 错误信息
|
||||
error = db.Column(
|
||||
db.Text,
|
||||
nullable=True,
|
||||
comment="错误信息"
|
||||
)
|
||||
|
||||
# ============================================================
|
||||
# 常量定义
|
||||
# ============================================================
|
||||
|
||||
class LogType:
|
||||
"""日志类型常量"""
|
||||
LOGIN = 1 # 登录
|
||||
LOGOUT = 2 # 退出
|
||||
|
||||
class Source:
|
||||
"""登录来源常量"""
|
||||
ADMIN = 0 # 后台
|
||||
FRONT = 1 # 前台
|
||||
|
||||
class Status:
|
||||
"""操作状态常量"""
|
||||
NORMAL = 0 # 正常
|
||||
ERROR = 1 # 异常
|
||||
|
||||
# ============================================================
|
||||
# 魔法方法
|
||||
# ============================================================
|
||||
|
||||
def __repr__(self):
|
||||
"""
|
||||
返回模型的字符串表示
|
||||
|
||||
Returns:
|
||||
str: 格式为 "登录日志:{username} - {log_type}" 的字符串
|
||||
"""
|
||||
log_type_name = "登录" if self.log_type == self.LogType.LOGIN else "退出"
|
||||
return '登录日志:{} - {}'.format(self.username, log_type_name)
|
||||
|
||||
def __str__(self):
|
||||
"""
|
||||
返回模型的字符串表示
|
||||
|
||||
Returns:
|
||||
str: 格式为 "登录日志:{username} - {log_type}" 的字符串
|
||||
"""
|
||||
return self.__repr__()
|
||||
Reference in New Issue
Block a user