Merge pull request 'project-filters-fix: список org_unit в фильтре проектов with-reports' (#150) from project-filters-fix into test

Reviewed-on: #150
This commit is contained in:
tsygankoviva 2026-09-08 12:01:44 +03:00
commit 2fd464869d
3 changed files with 25 additions and 4 deletions

View File

@ -33,6 +33,20 @@ router = APIRouter(tags=["forms"])
FORM3_ALLOWED_SECTIONS = {"q1", "q2", "q3", "q4", "year"} FORM3_ALLOWED_SECTIONS = {"q1", "q2", "q3", "q4", "year"}
def _parse_org_units_csv(org_units: Optional[str]) -> Optional[list[int]]:
if org_units is None:
return None
values = [value.strip() for value in org_units.split(",") if value.strip()]
if not values:
return None
try:
return [int(value) for value in values]
except ValueError as exc:
raise HTTPException(400, "org_units должен содержать ID через запятую") from exc
def _parse_sections(sections: Optional[str]) -> Optional[list[str]]: def _parse_sections(sections: Optional[str]) -> Optional[list[str]]:
if not sections: if not sections:
return None return None
@ -74,7 +88,7 @@ async def get_projects(
@router.get("/projects/with-reports") @router.get("/projects/with-reports")
async def get_projects_with_reports( async def get_projects_with_reports(
year: Optional[int] = None, year: Optional[int] = None,
branch_id: Optional[int] = None, branch_id: Optional[str] = None,
limit: Optional[int] = None, limit: Optional[int] = None,
offset: Optional[int] = None, offset: Optional[int] = None,
status_in: Optional[str] = None, status_in: Optional[str] = None,
@ -82,6 +96,9 @@ async def get_projects_with_reports(
db: AsyncSession = Depends(get_db), db: AsyncSession = Depends(get_db),
current_user: AppUser = Depends(get_current_active_user_with_set_db), current_user: AppUser = Depends(get_current_active_user_with_set_db),
) -> BaseListResponse[dict]: ) -> BaseListResponse[dict]:
branch_id = _parse_org_units_csv(branch_id)
project_service = ProjectService(db) project_service = ProjectService(db)
status_in = status_in.split(",") if status_in is not None else None status_in = status_in.split(",") if status_in is not None else None
count, projects = await project_service.get_list_with_reports( count, projects = await project_service.get_list_with_reports(

View File

@ -42,7 +42,7 @@ class ProjectRepository:
def _apply_project_filters( def _apply_project_filters(
query, query,
year: int | None, year: int | None,
branch_id: int | None, branch_id: int | list[int] | None,
org_unit_ids: list[int] | None, org_unit_ids: list[int] | None,
status_in: list[str] | None = None, status_in: list[str] | None = None,
search: str | None = None, search: str | None = None,
@ -60,7 +60,11 @@ class ProjectRepository:
) )
) )
if branch_id is not None: if branch_id is not None:
if isinstance(branch_id, int):
query = query.where(Project.org_unit_id == branch_id) query = query.where(Project.org_unit_id == branch_id)
else:
query = query.where(Project.org_unit_id.in_(branch_id))
if status_in is not None: if status_in is not None:
query = query.where(Project.status.in_(status_in)) query = query.where(Project.status.in_(status_in))
if search is not None and (normalized_search := search.strip()): if search is not None and (normalized_search := search.strip()):

View File

@ -39,7 +39,7 @@ class ProjectService:
self, self,
user: AppUser, user: AppUser,
year: int | None = None, year: int | None = None,
branch_id: int | None = None, branch_id: int | list[int] | None = None,
offset: int | None = None, offset: int | None = None,
limit: int | None = None, limit: int | None = None,
with_count: bool = False, with_count: bool = False,