120 lines
3.1 KiB
Python
120 lines
3.1 KiB
Python
# _*_ 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 = "角色权限中间表" # 表描述
|