projects-sheets: года в списке проектов, опциональный год при создании проекта #24

Merged
tsygankoviva merged 1 commits from project-sheets into test 2026-07-03 08:03:53 +03:00
2 changed files with 20 additions and 4 deletions

View File

@ -362,7 +362,7 @@ class AddProjectBody(BaseModel):
max_length=30, max_length=30,
pattern=r"^[^+\-\/\\=&*\s]{1,30}$", pattern=r"^[^+\-\/\\=&*\s]{1,30}$",
) )
year: int year: int = Field(default_factory=lambda: datetime.now().year)
branch_id: int branch_id: int
level: Literal["project", "program"] = "project" level: Literal["project", "program"] = "project"
parent_id: Optional[int] = None parent_id: Optional[int] = None

View File

@ -14,7 +14,7 @@ class ProjectRepository:
self.db = db self.db = db
@staticmethod @staticmethod
def _serialize_project(project: Project, org_unit_name: str | None, report_count: int) -> dict: def _serialize_project(project: Project, org_unit_name: str | None, report_count: int, years: list[InterruptedError] | None = None) -> dict:
return { return {
"id": project.id, "id": project.id,
"name": project.name, "name": project.name,
@ -29,6 +29,7 @@ class ProjectRepository:
"org_unit_id": project.org_unit_id, "org_unit_id": project.org_unit_id,
"org_unit_name": org_unit_name, "org_unit_name": org_unit_name,
"report_count": int(report_count), "report_count": int(report_count),
"years": years,
} }
@staticmethod @staticmethod
@ -66,11 +67,19 @@ class ProjectRepository:
.scalar_subquery() .scalar_subquery()
) )
report_years_subq = (
select(RfProjectReport.year)
.where(RfProjectReport.project_id == Project.id)
.distinct()
.scalar_subquery()
)
query = ( query = (
select( select(
Project, Project,
OrgUnit.title.label("org_unit_name"), OrgUnit.title.label("org_unit_name"),
report_count_subq.label("report_count"), report_count_subq.label("report_count"),
func.array(report_years_subq).label("years"),
) )
.outerjoin(OrgUnit, OrgUnit.id == Project.org_unit_id) .outerjoin(OrgUnit, OrgUnit.id == Project.org_unit_id)
.order_by(Project.id) .order_by(Project.id)
@ -82,7 +91,7 @@ class ProjectRepository:
query = query.limit(limit) query = query.limit(limit)
rows = (await self.db.execute(query)).all() rows = (await self.db.execute(query)).all()
payload = [self._serialize_project(row[0], row[1], row[2]) for row in rows] payload = [self._serialize_project(row[0], row[1], row[2], row[3]) for row in rows]
if not with_count: if not with_count:
return payload return payload
@ -101,11 +110,18 @@ class ProjectRepository:
.where(RfProjectReport.project_id == Project.id) .where(RfProjectReport.project_id == Project.id)
.scalar_subquery() .scalar_subquery()
) )
report_years_subq = (
select(RfProjectReport.year)
.where(RfProjectReport.project_id == Project.id)
.distinct()
.scalar_subquery()
)
query = ( query = (
select( select(
Project, Project,
OrgUnit.title.label("org_unit_name"), OrgUnit.title.label("org_unit_name"),
report_count_subq.label("report_count"), report_count_subq.label("report_count"),
func.array(report_years_subq).label("years"),
) )
.outerjoin(OrgUnit, OrgUnit.id == Project.org_unit_id) .outerjoin(OrgUnit, OrgUnit.id == Project.org_unit_id)
.where(Project.id == project_id) .where(Project.id == project_id)
@ -117,7 +133,7 @@ class ProjectRepository:
row = (await self.db.execute(query)).first() row = (await self.db.execute(query)).first()
if not row: if not row:
return None return None
return self._serialize_project(row[0], row[1], row[2]) return self._serialize_project(row[0], row[1], row[2], row[3])
async def get_reports(self, project_id: int) -> list[dict]: async def get_reports(self, project_id: int) -> list[dict]:
line_count_subq = ( line_count_subq = (