Merge pull request 'ssp-fix: org_unit в списке форм' (#14) from ssp-fix into test

Reviewed-on: #14
Reviewed-by: Raykov-MS <RaykovMS@avt.rshb.ru>
This commit is contained in:
tsygankoviva 2026-05-21 11:03:34 +03:00
commit c247d13829
5 changed files with 27 additions and 5 deletions

View File

@ -69,6 +69,7 @@ async def get_forms(
with_count=True,
offset=offset,
limit=limit,
load_org=True,
)
return BaseListResponse(
result = [

View File

@ -1,4 +1,5 @@
from datetime import datetime
import typing
from sqlalchemy import DateTime, ForeignKey, Integer, String, func
from sqlalchemy.orm import Mapped, mapped_column, relationship
@ -39,9 +40,9 @@ class BudgetForm(Base):
# updater: Mapped[typing.Optional["AppUser"]] = relationship(
# "AppUser", back_populates="updated_budget_forms", foreign_keys=[updated_by]
# )
# org_unit: Mapped[typing.Optional["OrgUnit"]] = relationship(
# "OrgUnit", back_populates="budget_forms"
# )
org_unit: Mapped[typing.Optional["OrgUnit"]] = relationship(
"OrgUnit", #back_populates="budget_forms"
)
# budget_lines: Mapped[list["BudgetLine"]] = relationship(
# "BudgetLine", back_populates="budget_form"
# )

View File

@ -172,6 +172,15 @@ class FormTypeSchemaEnum(str, enum.Enum):
FORM_4 = "FORM_4"
class OrgUnitSchema(BaseModel):
id: int
title: str
is_active: bool
is_ssp: bool
class Config:
from_attributes = True
class BudgetFormResponse(BaseModel):
id: int
created_at: datetime
@ -180,6 +189,7 @@ class BudgetFormResponse(BaseModel):
form_type_code: FormTypeSchemaEnum
year: int
org_unit_id: int
org_unit: OrgUnitSchema | None = None
class Config:
from_attributes = True

View File

@ -16,6 +16,7 @@ class BudgetFormRepository:
limit: int | None = None,
org_unit: int | list[int] | None = None,
with_count: bool = False,
load_org: bool = False,
) -> list[BudgetForm] | tuple[int, list[BudgetForm]]:
if with_count:
query = select(func.count().over().label("total_count"), BudgetForm)
@ -28,7 +29,13 @@ class BudgetFormRepository:
else:
where.append(BudgetForm.org_unit_id.in_(org_unit))
query = query.where(*where).order_by(BudgetForm.id)
query = query.where(*where)
if load_org:
query = query.options(
joinedload(BudgetForm.org_unit)
)
query = query.order_by(BudgetForm.id)
if offset is not None:
query = query.offset(offset)

View File

@ -20,13 +20,15 @@ class BudgetFormService:
user: AppUser,
offset: int | None = None,
limit: int | None = None,
with_count: bool = False
with_count: bool = False,
load_org: bool = False,
) -> list[BudgetForm] | tuple[int, list[BudgetForm]]:
if user.role_id == UserRoleEnum.ADMIN:
return await self.bf_repo.get_list(
offset=offset,
limit=limit,
with_count=with_count,
load_org=load_org,
)
user = await self.user_repo.get(
user_id=user.id,
@ -37,6 +39,7 @@ class BudgetFormService:
limit=limit,
org_unit=[ou.id for ou in user.org_units],
with_count=with_count,
load_org=load_org,
)
async def get(