feat: 初始化仓库

This commit is contained in:
2025-02-12 02:38:29 +08:00
commit 46e9e79670
67 changed files with 8960 additions and 0 deletions

30
models/__init__.py Normal file
View File

@@ -0,0 +1,30 @@
# _*_ coding : UTF-8 _*_
# @Time : 2025/01/18 02:42
# @UpdateTime : 2025/01/18 02:42
# @Author : sonder
# @File : __init__.py.py
# @Software : PyCharm
# @Comment : 本程序
from models.department import Department, DepartmentRole
from models.file import File
from models.log import LoginLog, OperationLog
from models.permission import Permission
from models.role import Role, RolePermission
from models.user import User, UserRole
from models.i18n import I18n,Locale
__all__ = [
'Department',
'DepartmentRole',
'File',
'LoginLog',
'OperationLog',
'Permission',
'Role',
'RolePermission',
'User',
'UserRole',
'I18n',
'Locale'
]

59
models/common.py Normal file
View File

@@ -0,0 +1,59 @@
# _*_ coding : UTF-8 _*_
# @Time : 2025/01/18 02:43
# @UpdateTime : 2025/01/18 02:43
# @Author : sonder
# @File : common.py
# @Software : PyCharm
# @Comment : 本程序
from tortoise import fields, models
class BaseModel(models.Model):
"""
抽象模型,用于定义数据表的公共字段。
"""
id = fields.UUIDField(pk=True, description="主键", autoincrement=True)
"""
自增 UUID作为主键。
- 使用 UUIDField 生成唯一标识符。
"""
del_flag = fields.SmallIntField(default=1, description="删除标志 1存在 0删除")
"""
删除标志。
- 1 代表存在0 代表删除。
- 默认为 1。
"""
create_by = fields.CharField(max_length=255, default='', description="创建者")
"""
创建者。
- 默认为空字符串。
"""
create_time = fields.DatetimeField(auto_now_add=True, description="创建时间", null=True)
"""
创建时间。
- 自动设置为当前时间。
- 允许为空null=True
"""
update_by = fields.CharField(max_length=255, default='', description="更新者")
"""
更新者。
- 默认为空字符串。
"""
update_time = fields.DatetimeField(auto_now=True, description="更新时间", null=True)
"""
更新时间。
- 自动更新为当前时间。
- 允许为空null=True
"""
class Meta:
abstract = True # 标记为抽象类,不会创建对应的数据库表
ordering = ["-create_time"] # 默认按创建时间倒序排序
indexes = ("del_flag",) # 为 del_flag 字段创建索引

155
models/department.py Normal file
View File

@@ -0,0 +1,155 @@
# _*_ coding : UTF-8 _*_
# @Time : 2025/01/18 03:21
# @UpdateTime : 2025/01/18 03:21
# @Author : sonder
# @File : department.py
# @Software : PyCharm
# @Comment : 本程序
from tortoise import fields
from models.common import BaseModel
class Department(BaseModel):
"""
部门表模型。
"""
name = fields.CharField(
max_length=100,
description="部门名称",
source_field="name" # 映射到数据库字段 name
)
"""
部门名称。
- 最大长度为 100 个字符。
- 映射到数据库字段 name。
"""
parent_id = fields.CharField(
max_length=36,
default="",
null=True,
description="父部门ID",
source_field="parent_id" # 映射到数据库字段 parent_id
)
"""
父部门ID。
- 用于表示部门的层级关系。
- 默认为空字符串,表示顶级部门。
- 映射到数据库字段 parent_id。
"""
sort = fields.IntField(
default=0,
description="排序权重0最高",
source_field="sort" # 映射到数据库字段 sort
)
"""
排序权重。
- 用于部门列表的排序,值越小越靠前。
- 默认为 0。
- 映射到数据库字段 sort。
"""
phone = fields.CharField(
max_length=30,
null=True,
description="部门电话",
source_field="phone" # 映射到数据库字段 phone
)
"""
部门电话。
- 最大长度为 30 个字符。
- 允许为空。
- 映射到数据库字段 phone。
"""
principal = fields.CharField(
max_length=64,
description="部门负责人",
source_field="principal" # 映射到数据库字段 principal
)
"""
部门负责人。
- 最大长度为 64 个字符。
- 映射到数据库字段 principal。
"""
email = fields.CharField(
max_length=128,
null=True,
description="部门邮箱",
source_field="email" # 映射到数据库字段 email
)
"""
部门邮箱。
- 最大长度为 128 个字符。
- 允许为空。
- 映射到数据库字段 email。
"""
status = fields.SmallIntField(
default=1,
description="状态0正常 1停用",
source_field="status" # 映射到数据库字段 status
)
"""
状态。
- 1 表示正常0 表示停用。
- 默认为 1。
- 映射到数据库字段 status。
"""
remark = fields.CharField(
max_length=255,
null=True,
description="备注信息",
source_field="remark" # 映射到数据库字段 remark
)
"""
备注信息。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 remark。
"""
class Meta:
table = "department" # 数据库表名
table_description = "部门表" # 表描述
ordering = ["sort", "-create_time"] # 默认按排序权重和创建时间排序
class DepartmentRole(BaseModel):
"""
部门角色表模型。
"""
department = fields.ForeignKeyField(
"models.Department",
related_name="department_roles",
description="部门ID",
source_field="department_id" # 映射到数据库字段 department_id
)
"""
部门ID。
- 外键关联到 Department 表。
- 映射到数据库字段 department_id。
"""
role = fields.ForeignKeyField(
"models.Role",
related_name="department_roles",
description="角色ID",
source_field="role_id" # 映射到数据库字段 role_id
)
"""
角色ID。
- 外键关联到 Role 表。
- 映射到数据库字段 role_id。
"""
class Meta:
table = "department_role" # 数据库表名
table_description = "部门角色表" # 表描述
unique_together = (("department_id", "role_id"),) # 唯一约束,防止重复分配

93
models/file.py Normal file
View File

@@ -0,0 +1,93 @@
# _*_ coding : UTF-8 _*_
# @Time : 2025/01/19 00:46
# @UpdateTime : 2025/01/19 00:46
# @Author : sonder
# @File : file.py
# @Software : PyCharm
# @Comment : 本程序
from tortoise import fields
from models.common import BaseModel
class File(BaseModel):
"""
文件表模型。
"""
name = fields.CharField(
max_length=255,
description="文件名",
source_field="name" # 映射到数据库字段 name
)
"""
文件名。
- 包括文件扩展名。
- 最大长度为 255 个字符。
- 映射到数据库字段 name。
"""
size = fields.BigIntField(
description="文件大小(字节)",
source_field="size" # 映射到数据库字段 size
)
"""
文件大小。
- 单位:字节。
- 映射到数据库字段 size。
"""
file_type = fields.CharField(
max_length=100,
description="文件类型",
source_field="file_type" # 映射到数据库字段 file_type
)
"""
文件类型。
- 例如image/png、application/pdf。
- 最大长度为 100 个字符。
- 映射到数据库字段 file_type。
"""
absolute_path = fields.CharField(
max_length=512,
description="绝对路径",
source_field="absolute_path" # 映射到数据库字段 absolute_path
)
"""
绝对路径。
- 文件在服务器上的绝对路径。
- 最大长度为 512 个字符。
- 映射到数据库字段 absolute_path。
"""
relative_path = fields.CharField(
max_length=512,
description="相对路径",
source_field="relative_path" # 映射到数据库字段 relative_path
)
"""
相对路径。
- 文件相对于某个根目录的相对路径。
- 最大长度为 512 个字符。
- 映射到数据库字段 relative_path。
"""
uploader = fields.ForeignKeyField(
"models.User",
related_name="uploaded_files",
null=True, # 允许为空
description="上传人员",
source_field="uploader_id" # 映射到数据库字段 uploader_id
)
"""
上传人员。
- 外键关联到 User 表。
- 允许为空(例如系统自动上传的文件)。
- 映射到数据库字段 uploader_id。
"""
class Meta:
table = "file" # 数据库表名
table_description = "文件表" # 表描述
ordering = ["-create_time"] # 默认按创建时间倒序排序

87
models/i18n.py Normal file
View File

@@ -0,0 +1,87 @@
# _*_ coding : UTF-8 _*_
# @Time : 2025/02/04 15:43
# @UpdateTime : 2025/02/04 15:43
# @Author : sonder
# @File : i18n.py
# @Software : PyCharm
# @Comment : 本程序
from tortoise import fields
from models.common import BaseModel
class Locale(BaseModel):
"""
语言模型.
"""
code = fields.CharField(
max_length=10,
description="语言代码",
source_field="code",
unique=True
)
"""
语言代码。
- 例如en英语、zh中文、fr法语
- 最大长度为10个字符。
- 映射到数据库字段 code
"""
name = fields.CharField(
max_length=50,
description="语言名称",
source_field="name",
unique=True
)
"""
语言名称。
- 最大长度为50个字符。
- 映射到数据库字段 name
"""
class Meta:
table = "locale"
table_description = "语言表"
class I18n(BaseModel):
"""
国际化模型.
"""
key = fields.CharField(
max_length=255,
description="国际化key",
source_field="key"
)
"""
国际化key。
- 最大长度为255个字符。
- 映射到数据库字段 key
"""
locale = fields.ForeignKeyField(
"models.Locale",
related_name="i18n",
description="语言",
source_field="locale_id"
)
"""
语言。
- 关联到 Locale 模型。
- 映射到数据库字段 locale_id
"""
translation = fields.TextField(
description="翻译内容",
source_field="translation"
)
"""
翻译内容。
- 存储具体的翻译文本。
- 映射到数据库字段 translation
"""
class Meta:
table = "i18n"
table_description = "国际化表"

345
models/log.py Normal file
View File

@@ -0,0 +1,345 @@
# _*_ coding : UTF-8 _*_
# @Time : 2025/01/19 00:03
# @UpdateTime : 2025/01/19 00:03
# @Author : sonder
# @File : log.py
# @Software : PyCharm
# @Comment : 本程序
from tortoise import fields
from models.common import BaseModel
class LoginLog(BaseModel):
"""
系统访问记录表模型。
"""
user = fields.ForeignKeyField(
"models.User",
related_name="login_logs",
description="用户ID",
source_field="user_id" # 映射到数据库字段 user_id
)
"""
用户ID。
- 外键关联到 User 表。
- 映射到数据库字段 user_id。
"""
login_ip = fields.CharField(
max_length=50,
description="登录IP地址",
source_field="login_ip" # 映射到数据库字段 login_ip
)
"""
登录IP地址。
- 最大长度为 50 个字符。
- 映射到数据库字段 login_ip。
"""
login_location = fields.CharField(
max_length=255,
null=True,
description="登录地点",
source_field="login_location" # 映射到数据库字段 login_location
)
"""
登录地点。
- 根据 IP 地址解析的地理位置信息。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 login_location。
"""
browser = fields.CharField(
max_length=255,
null=True,
description="浏览器类型",
source_field="browser" # 映射到数据库字段 browser
)
"""
浏览器类型。
- 记录用户登录时使用的浏览器类型。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 browser。
"""
os = fields.CharField(
max_length=255,
null=True,
description="操作系统",
source_field="os" # 映射到数据库字段 os
)
"""
操作系统。
- 记录用户登录时使用的操作系统。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 os。
"""
status = fields.SmallIntField(
default=1,
description="登录状态1成功0失败",
source_field="status" # 映射到数据库字段 status
)
"""
登录状态。
- 1成功
- 0失败
- 默认为 1。
- 映射到数据库字段 status。
"""
login_time = fields.DatetimeField(
auto_now_add=True,
description="登录时间",
source_field="login_time" # 映射到数据库字段 login_time
)
"""
登录时间。
- 自动设置为当前时间。
- 映射到数据库字段 login_time。
"""
session_id = fields.CharField(
max_length=36,
null=True,
description="会话ID",
source_field="session_id"
)
"""
会话ID。
- 记录用户登录时的会话ID。
- 允许为空。
- 映射到数据库字段 session_id。
"""
class Meta:
table = "login_log" # 数据库表名
table_description = "系统访问记录表" # 表描述
ordering = ["-login_time"] # 默认按登录时间倒序排序
class OperationLog(BaseModel):
"""
操作日志表模型。
"""
operation_name = fields.CharField(
max_length=255,
description="操作名称",
source_field="operation_name" # 映射到数据库字段 operation_name
)
"""
操作名称。
- 最大长度为 255 个字符。
- 映射到数据库字段 operation_name。
"""
operation_type = fields.SmallIntField(
description="操作类型(增删改查)",
source_field="operation_type" # 映射到数据库字段 operation_type
)
"""
操作类型。
- 增、删、改、查等操作类型。
- 最大长度为 50 个字符。
- 映射到数据库字段 operation_type。
"""
request_path = fields.TextField(
description="请求路径",
source_field="request_path" # 映射到数据库字段 request_path
)
"""
请求路径。
- 记录用户请求的 API 路径。
- 最大长度为 255 个字符。
- 映射到数据库字段 request_path。
"""
request_method = fields.CharField(
max_length=10,
description="请求方法",
source_field="request_method" # 映射到数据库字段 request_method
)
"""
请求方法。
- 记录用户请求的 HTTP 方法(如 GET、POST、PUT、DELETE
- 最大长度为 10 个字符。
- 映射到数据库字段 request_method。
"""
operator = fields.ForeignKeyField(
"models.User",
related_name="operation_logs",
null=True, # 允许操作人员为空
description="操作人员",
source_field="operator_id" # 映射到数据库字段 operator_id
)
"""
操作人员。
- 外键关联到 User 表。
- 允许为空。
- 映射到数据库字段 operator_id。
"""
department = fields.ForeignKeyField(
"models.Department",
related_name="operation_logs",
null=True, # 允许操作人员为空
description="操作人员所属部门",
source_field="department_id" # 映射到数据库字段 department_id
)
"""
操作人员所属部门。
- 外键关联到 Department 表。
- 允许为空。
- 映射到数据库字段 department_id。
"""
department_name = fields.CharField(
max_length=255,
description="部门名称",
source_field="department_name" # 映射到数据库字段 department_name
)
"""
部门名称。
- 记录操作人员所属的部门名称。
- 最大长度为 255 个字符。
- 映射到数据库字段 department_name。
"""
host = fields.CharField(
max_length=50,
description="主机地址",
source_field="host" # 映射到数据库字段 host
)
"""
主机地址。
- 记录用户请求的 IP 地址。
- 最大长度为 50 个字符。
- 映射到数据库字段 host。
"""
location = fields.CharField(
max_length=255,
null=True,
description="操作地点",
source_field="location" # 映射到数据库字段 location
)
"""
操作地点。
- 根据 IP 地址解析的地理位置信息。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 location。
"""
user_agent = fields.TextField(
null=True,
description="用户请求头",
source_field="user_agent" # 映射到数据库字段 user_agent
)
"""
用户请求头。
- 记录用户请求的 User-Agent 信息。
- 允许为空。
- 映射到数据库字段 user_agent。
"""
browser = fields.CharField(
max_length=255,
null=True,
description="浏览器类型",
source_field="browser" # 映射到数据库字段 browser
)
"""
浏览器类型。
- 记录用户登录时使用的浏览器类型。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 browser。
"""
os = fields.CharField(
max_length=255,
null=True,
description="操作系统",
source_field="os" # 映射到数据库字段 os
)
"""
操作系统。
- 记录用户登录时使用的操作系统。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 os。
"""
request_params = fields.TextField(
null=True,
description="请求参数",
source_field="request_params" # 映射到数据库字段 request_params
)
"""
请求参数。
- 记录用户请求的参数任意格式如字符串、JSON、XML 等)。
- 允许为空。
- 映射到数据库字段 request_params。
"""
response_result = fields.TextField(
null=True,
description="返回结果",
source_field="response_result" # 映射到数据库字段 response_result
)
"""
返回结果。
- 记录操作的返回结果任意格式如字符串、JSON、XML 等)。
- 允许为空。
- 映射到数据库字段 response_result。
"""
status = fields.SmallIntField(
default=1,
description="操作状态1成功0失败",
source_field="status" # 映射到数据库字段 status
)
"""
操作状态。
- 1成功
- 0失败
- 默认为 1。
- 映射到数据库字段 status。
"""
operation_time = fields.DatetimeField(
auto_now_add=True,
description="操作时间",
source_field="operation_time" # 映射到数据库字段 operation_time
)
"""
操作时间。
- 自动设置为当前时间。
- 映射到数据库字段 operation_time。
"""
cost_time = fields.FloatField(
default=0,
description="消耗时间(毫秒)",
source_field="cost_time" # 映射到数据库字段 cost_time
)
"""
消耗时间。
- 记录操作消耗的时间(单位:毫秒)。
- 默认为 0。
- 映射到数据库字段 cost_time。
"""
class Meta:
table = "operation_log" # 数据库表名
table_description = "操作日志表" # 表描述
ordering = ["-operation_time"] # 默认按操作时间倒序排序

286
models/permission.py Normal file
View File

@@ -0,0 +1,286 @@
# _*_ coding : UTF-8 _*_
# @Time : 2025/01/18 03:40
# @UpdateTime : 2025/01/18 03:40
# @Author : sonder
# @File : permission.py
# @Software : PyCharm
# @Comment : 本程序
from tortoise import fields
from models.common import BaseModel
class Permission(BaseModel):
"""
权限表模型。
"""
menu_type = fields.SmallIntField(
description="菜单类型0菜单、1iframe、2外链、3按钮",
source_field="menu_type" # 映射到数据库字段 menu_type
)
"""
菜单类型。
- 0菜单
- 1iframe
- 2外链
- 3按钮
- 映射到数据库字段 menu_type。
"""
parent_id = fields.CharField(
max_length=36,
default="",
description="父权限ID",
source_field="parent_id" # 映射到数据库字段 parent_id
)
"""
父权限ID。
- 用于表示权限的层级关系。
- 默认为空字符串,表示顶级权限。
- 映射到数据库字段 parent_id。
"""
title = fields.CharField(
max_length=255,
description="菜单名称",
source_field="title" # 映射到数据库字段 title
)
"""
菜单名称。
- 兼容国际化、非国际化。
- 最大长度为 255 个字符。
- 映射到数据库字段 title。
"""
name = fields.CharField(
max_length=255,
null=True,
description="路由名称",
source_field="name" # 映射到数据库字段 name
)
"""
路由名称。
- 必须唯一,并且与前端路由组件的 `name` 保持一致。
- 最大长度为 255 个字符。
- 映射到数据库字段 name。
"""
path = fields.CharField(
max_length=255,
description="路由路径",
source_field="path" # 映射到数据库字段 path
)
"""
路由路径。
- 最大长度为 255 个字符。
- 映射到数据库字段 path。
"""
component = fields.CharField(
max_length=255,
null=True,
description="组件路径",
source_field="component" # 映射到数据库字段 component
)
"""
组件路径。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 component。
"""
rank = fields.IntField(
default=1,
description="菜单排序",
source_field="rank" # 映射到数据库字段 rank
)
"""
菜单排序。
- 平台规定只有 `home` 路由的 `rank` 才能为 0。
- 默认为 1。
- 映射到数据库字段 rank。
"""
redirect = fields.CharField(
max_length=255,
null=True,
description="路由重定向",
source_field="redirect" # 映射到数据库字段 redirect
)
"""
路由重定向。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 redirect。
"""
icon = fields.CharField(
max_length=255,
null=True,
description="菜单图标",
source_field="icon" # 映射到数据库字段 icon
)
"""
菜单图标。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 icon。
"""
extra_icon = fields.CharField(
max_length=255,
null=True,
description="右侧图标",
source_field="extra_icon" # 映射到数据库字段 extra_icon
)
"""
右侧图标。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 extra_icon。
"""
enter_transition = fields.CharField(
max_length=255,
null=True,
description="进场动画",
source_field="enter_transition" # 映射到数据库字段 enter_transition
)
"""
进场动画。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 enter_transition。
"""
leave_transition = fields.CharField(
max_length=255,
null=True,
description="离场动画",
source_field="leave_transition" # 映射到数据库字段 leave_transition
)
"""
离场动画。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 leave_transition。
"""
active_path = fields.CharField(
max_length=255,
null=True,
description="菜单激活路径",
source_field="active_path" # 映射到数据库字段 active_path
)
"""
菜单激活路径。
- 用于指定激活菜单的路径。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 active_path。
"""
auths = fields.CharField(
max_length=255,
null=True,
description="权限标识",
source_field="auths" # 映射到数据库字段 auths
)
"""
权限标识。
- 用于按钮级别权限设置。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 auths。
"""
frame_src = fields.CharField(
max_length=255,
null=True,
description="iframe链接地址",
source_field="frame_src" # 映射到数据库字段 frame_src
)
"""
iframe 链接地址。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 frame_src。
"""
frame_loading = fields.BooleanField(
default=True,
description="iframe加载动画",
source_field="frame_loading" # 映射到数据库字段 frame_loading
)
"""
iframe 加载动画。
- 是否开启首次加载动画。
- 默认为 True。
- 映射到数据库字段 frame_loading。
"""
keep_alive = fields.BooleanField(
default=False,
description="缓存页面",
source_field="keep_alive" # 映射到数据库字段 keep_alive
)
"""
缓存页面。
- 是否缓存该路由页面。
- 默认为 False。
- 映射到数据库字段 keep_alive。
"""
hidden_tag = fields.BooleanField(
default=False,
description="隐藏标签页",
source_field="hidden_tag" # 映射到数据库字段 hidden_tag
)
"""
隐藏标签页。
- 是否禁止将当前菜单名称添加到标签页。
- 默认为 False。
- 映射到数据库字段 hidden_tag。
"""
fixed_tag = fields.BooleanField(
default=False,
description="固定标签页",
source_field="fixed_tag" # 映射到数据库字段 fixed_tag
)
"""
固定标签页。
- 是否固定显示在标签页且不可关闭。
- 默认为 False。
- 映射到数据库字段 fixed_tag。
"""
show_link = fields.BooleanField(
default=True,
description="显示菜单",
source_field="show_link" # 映射到数据库字段 show_link
)
"""
显示菜单。
- 是否显示该菜单。
- 默认为 True。
- 映射到数据库字段 show_link。
"""
show_parent = fields.BooleanField(
default=True,
description="显示父级菜单",
source_field="show_parent" # 映射到数据库字段 show_parent
)
"""
显示父级菜单。
- 是否显示父级菜单。
- 默认为 True。
- 映射到数据库字段 show_parent。
"""
class Meta:
table = "permission" # 数据库表名
table_description = "权限表" # 表描述
ordering = ["rank", "-create_time"] # 默认按排序权重和创建时间排序

119
models/role.py Normal file
View File

@@ -0,0 +1,119 @@
# _*_ coding : UTF-8 _*_
# @Time : 2025/01/18 23:00
# @UpdateTime : 2025/01/18 23:00
# @Author : sonder
# @File : role.py
# @Software : PyCharm
# @Comment : 本程序
from tortoise import fields
from models.common import BaseModel
class Role(BaseModel):
"""
角色表模型。
"""
name = fields.CharField(
max_length=255,
description="角色名称",
source_field="role_name" # 映射到数据库字段 role_name
)
"""
角色名称。
- 允许重复,因为不同部门可能有相同的角色名称。
- 最大长度为 255 个字符。
- 映射到数据库字段 role_name。
"""
code = fields.CharField(
max_length=255,
unique=True,
description="角色编码",
source_field="role_code" # 映射到数据库字段 role_code
)
"""
角色编码。
- 用于系统内部识别角色。
- 必须唯一。
- 最大长度为 255 个字符。
- 映射到数据库字段 role_code。
"""
description = fields.CharField(
max_length=255,
null=True,
description="角色描述",
source_field="role_description" # 映射到数据库字段 role_description
)
"""
角色描述。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 role_description。
"""
status=fields.SmallIntField(
default=1,
description="角色状态",
source_field="status"
)
"""
角色状态。
- 1: 正常
- 0: 禁用
- 映射到数据库字段 status。
"""
permissions = fields.ManyToManyField(
"models.Permission",
related_name="roles",
through="role_permission",
description="角色权限"
)
"""
角色权限。
- 多对多关系,表示角色拥有的权限。
- 通过中间表 `role_permission` 关联权限表。
"""
department = fields.ForeignKeyField(
"models.Department",
related_name="roles",
null=True,
description="所属部门",
source_field="department_id" # 映射到数据库字段 department_id
)
"""
所属部门。
- 表示角色所属的部门。
- 如果为 null则表示角色是全局角色。
- 映射到数据库字段 department_id。
"""
class Meta:
table = "role" # 数据库表名
table_description = "角色表" # 表描述
ordering = ["-create_time"] # 默认按创建时间倒序排序
class RolePermission(BaseModel):
"""
角色权限中间表。
"""
role = fields.ForeignKeyField(
"models.Role",
related_name="role_permissions",
source_field="role_id" # 映射到数据库字段 role_id
)
permission = fields.ForeignKeyField(
"models.Permission",
related_name="role_permissions",
source_field="permission_id" # 映射到数据库字段 permission_id
)
class Meta:
table = "role_permission" # 数据库表名
table_description = "角色权限中间表" # 表描述

172
models/user.py Normal file
View File

@@ -0,0 +1,172 @@
# _*_ coding : UTF-8 _*_
# @Time : 2025/01/18 03:20
# @UpdateTime : 2025/01/18 03:20
# @Author : sonder
# @File : user.py
# @Software : PyCharm
# @Comment : 本程序
from tortoise import fields
from models.common import BaseModel
class User(BaseModel):
"""
用户表模型。
"""
username = fields.CharField(
max_length=255,
unique=True,
description="用户名",
source_field="username" # 映射到数据库字段 username
)
"""
用户名。
- 必须唯一。
- 最大长度为 255 个字符。
- 映射到数据库字段 username。
"""
password = fields.CharField(
max_length=255,
description="密码",
source_field="password" # 映射到数据库字段 password
)
"""
密码。
- 存储加密后的密码。
- 最大长度为 255 个字符。
- 映射到数据库字段 password。
"""
email = fields.CharField(
max_length=255,
null=True,
description="邮箱",
source_field="email" # 映射到数据库字段 email
)
"""
邮箱。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 email。
"""
phone = fields.CharField(
max_length=30,
null=True,
description="手机号",
source_field="phone" # 映射到数据库字段 phone
)
"""
手机号。
- 最大长度为 30 个字符。
- 允许为空。
- 映射到数据库字段 phone。
"""
nickname = fields.CharField(
max_length=255,
null=True,
description="昵称",
source_field="nickname" # 映射到数据库字段 nickname
)
"""
昵称。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 nickname。
"""
avatar = fields.CharField(
max_length=255,
null=True,
description="头像",
source_field="avatar" # 映射到数据库字段 avatar
)
"""
头像。
- 最大长度为 255 个字符。
- 允许为空。
- 映射到数据库字段 avatar。
"""
gender = fields.SmallIntField(
default=0,
description="性别1男0女",
source_field="gender" # 映射到数据库字段 gender
)
"""
性别。
- 1
- 0
- 默认为 0。
- 映射到数据库字段 gender。
"""
status = fields.SmallIntField(
default=1,
description="用户状态1启用0禁用",
source_field="status" # 映射到数据库字段 status
)
"""
用户状态。
- 1启用
- 0禁用
- 默认为 1。
- 映射到数据库字段 status。
"""
department = fields.ForeignKeyField(
"models.Department",
related_name="users",
null=True,
description="所属部门",
source_field="department_id" # 映射到数据库字段 department_id
)
"""
所属部门。
- 外键关联到 Department 表。
- 如果为 null则表示用户未分配部门。
- 映射到数据库字段 department_id。
"""
roles = fields.ManyToManyField(
"models.Role",
related_name="users",
through="user_role",
description="用户角色"
)
"""
用户角色。
- 多对多关系,表示用户拥有的角色。
- 通过中间表 `user_role` 关联角色表。
"""
class Meta:
table = "user" # 数据库表名
table_description = "用户表" # 表描述
ordering = ["-create_time"] # 默认按创建时间倒序排序
class UserRole(BaseModel):
"""
用户角色中间表。
"""
user = fields.ForeignKeyField(
"models.User",
related_name="user_roles",
source_field="user_id" # 映射到数据库字段 user_id
)
role = fields.ForeignKeyField(
"models.Role",
related_name="user_roles",
source_field="role_id" # 映射到数据库字段 role_id
)
class Meta:
table = "user_role" # 数据库表名
table_description = "用户角色中间表" # 表描述
unique_together = (("user_id", "role_id"),) # 唯一约束,防止重复分配