forked from iti-framework/iTi-Flask
You cannot 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
2.7 KiB
Python
89 lines
2.7 KiB
Python
from iti.applications.extensions import db
|
|
from iti.applications.common.crud import BaseWithoutIdModelMixin
|
|
from iti.applications.common.enums import StatusEnum
|
|
from iti.applications.common.utils import BaseSchema
|
|
from apiflask.fields import String, DateTime, Enum, Integer, Nested
|
|
import uuid
|
|
|
|
|
|
class SysDept(BaseWithoutIdModelMixin):
|
|
"""
|
|
部门表
|
|
"""
|
|
|
|
__tablename__ = "sys_dept"
|
|
id = db.Column(
|
|
db.String(36),
|
|
primary_key=True,
|
|
default=lambda: str(uuid.uuid4().hex),
|
|
comment="标识",
|
|
)
|
|
name = db.Column(db.String(255), nullable=False, comment="部门名称")
|
|
parent_id = db.Column(db.String(36), nullable=True, comment="父部门ID")
|
|
desc = db.Column(db.Text, nullable=True, comment="部门描述")
|
|
sort = db.Column(db.Integer, nullable=False, default=0, comment="排序")
|
|
leader_id = db.Column(db.String(36), nullable=True, comment="负责人ID")
|
|
status = db.Column(
|
|
db.Enum(StatusEnum, values_callable=lambda x: [e.value for e in x]),
|
|
nullable=False,
|
|
default=StatusEnum.ENABLED.value,
|
|
comment="状态",
|
|
)
|
|
|
|
# 关系
|
|
leader = db.relationship(
|
|
"User",
|
|
primaryjoin="SysDept.leader_id == User.id",
|
|
foreign_keys="SysDept.leader_id",
|
|
uselist=False,
|
|
lazy="noload",
|
|
)
|
|
parent = db.relationship(
|
|
"SysDept",
|
|
primaryjoin="SysDept.parent_id == SysDept.id",
|
|
foreign_keys="SysDept.parent_id",
|
|
uselist=False,
|
|
remote_side=[id],
|
|
viewonly=True,
|
|
)
|
|
children = db.relationship(
|
|
"SysDept",
|
|
primaryjoin="SysDept.id == SysDept.parent_id",
|
|
foreign_keys="SysDept.parent_id",
|
|
uselist=True,
|
|
lazy="noload",
|
|
viewonly=True,
|
|
)
|
|
users = db.relationship(
|
|
"User",
|
|
secondary="sys_user_dept",
|
|
primaryjoin="SysDept.id == sys_user_dept.c.dept_id",
|
|
secondaryjoin="User.id == sys_user_dept.c.user_id",
|
|
back_populates="depts",
|
|
lazy="noload",
|
|
)
|
|
|
|
|
|
class SysDeptSchema(BaseSchema):
|
|
id = String()
|
|
name = String()
|
|
parent_id = String()
|
|
desc = String()
|
|
sort = Integer()
|
|
leader_id = String()
|
|
status = Enum(StatusEnum, by_value=True)
|
|
created_at = DateTime(format="%Y-%m-%d %H:%M:%S")
|
|
updated_at = DateTime(format="%Y-%m-%d %H:%M:%S")
|
|
# 关系
|
|
leader = Nested(
|
|
"UserSchema", dump_only=True, exclude=["depts", "roles", "permissions"]
|
|
)
|
|
parent = Nested("SysDeptSchema", dump_only=True, exclude=["children"])
|
|
children = Nested("SysDeptSchema", many=True, dump_only=True, exclude=["parent"])
|
|
users = Nested(
|
|
"UserSchema",
|
|
many=True,
|
|
dump_only=True,
|
|
exclude=["depts", "roles", "permissions"],
|
|
)
|