feat: nested department sections, attaching department sections in the user editor

This commit is contained in:
2025-01-19 12:01:10 +04:00
parent 43a95ef75c
commit b8947ce68e
7 changed files with 198 additions and 32 deletions

View File

@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional
from sqlalchemy import BigInteger, Table, ForeignKey, Column
from sqlalchemy.orm import Mapped, mapped_column, relationship
@@ -31,12 +31,6 @@ user_pay_rate = Table(
Column('user_id', ForeignKey('users.id'), primary_key=True, unique=True)
)
user_department_section = Table(
'user_department_section',
BaseModel.metadata,
Column('department_section_id', ForeignKey('department_sections.id'), primary_key=True),
Column('user_id', ForeignKey('users.id'), primary_key=True)
)
class Permission(BaseModel):
@@ -60,6 +54,18 @@ class Role(BaseModel):
# users: Mapped[list["User"]] = relationship("User", back_populates="users")
class UserDepartmentSection(BaseModel):
__tablename__ = 'user_department_section'
section_id: Mapped[int] = mapped_column(ForeignKey('department_sections.id'), primary_key=True)
section: Mapped["DepartmentSection"] = relationship(lazy='selectin', back_populates='users')
user_id: Mapped[int] = mapped_column(ForeignKey('users.id'), primary_key=True)
user: Mapped["User"] = relationship(lazy="selectin", back_populates='department_sections')
is_chief: Mapped[bool] = mapped_column(nullable=False, default=False, server_default='0')
class User(BaseModel):
__tablename__ = 'users'
id: Mapped[int] = mapped_column(primary_key=True)
@@ -126,6 +132,12 @@ class User(BaseModel):
lazy='selectin'
)
department_sections: Mapped[list[UserDepartmentSection]] = relationship(
"UserDepartmentSection",
back_populates='user',
lazy="noload",
)
class Position(BaseModel):
__tablename__ = 'positions'
@@ -166,14 +178,29 @@ class DepartmentSection(BaseModel):
id: Mapped[int] = mapped_column(primary_key=True)
name: Mapped[str] = mapped_column(index=True)
department_id: Mapped[int] = mapped_column(ForeignKey('departments.id'))
department_id: Mapped[Optional[int]] = mapped_column(ForeignKey('departments.id'))
department: Mapped["Department"] = relationship(
back_populates='sections',
lazy='selectin',
)
users: Mapped[list[User]] = relationship(
'User',
secondary=user_department_section,
uselist=True,
parent_department_section_id: Mapped[Optional[int]] = mapped_column(ForeignKey('department_sections.id'))
parent_department_section: Mapped["DepartmentSection"] = relationship(
"DepartmentSection",
back_populates="sections",
lazy='selectin',
remote_side=[id],
)
sections: Mapped[list["DepartmentSection"]] = relationship(
"DepartmentSection",
back_populates="parent_department_section",
uselist=True,
cascade='all, delete',
)
users: Mapped[list[UserDepartmentSection]] = relationship(
"UserDepartmentSection",
lazy='selectin',
back_populates='section',
cascade='all, delete',
)