51 lines
1.6 KiB
Python
51 lines
1.6 KiB
Python
|
|
"""Единообразные поля проекта в JSON API."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
from typing import Any
|
||
|
|
|
||
|
|
|
||
|
|
def ensure_project_id(item: dict[str, Any]) -> dict[str, Any]:
|
||
|
|
"""Добавляет project_id, если в записи уже есть данные проекта."""
|
||
|
|
if item.get("project_id") is not None:
|
||
|
|
return item
|
||
|
|
proj = item.get("project")
|
||
|
|
if proj is not None:
|
||
|
|
try:
|
||
|
|
item["project_id"] = int(proj)
|
||
|
|
except (TypeError, ValueError):
|
||
|
|
pass
|
||
|
|
return item
|
||
|
|
# id в work-report — emp_id; project_id задаётся явно в эндпоинте
|
||
|
|
if "employee" in item:
|
||
|
|
return item
|
||
|
|
pid = item.get("id")
|
||
|
|
if pid is None:
|
||
|
|
return item
|
||
|
|
if any(
|
||
|
|
k in item
|
||
|
|
for k in ("project_code", "project_name", "code", "director", "step", "team")
|
||
|
|
):
|
||
|
|
try:
|
||
|
|
item["project_id"] = int(pid)
|
||
|
|
except (TypeError, ValueError):
|
||
|
|
pass
|
||
|
|
return item
|
||
|
|
|
||
|
|
|
||
|
|
def ensure_project_ids(items: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||
|
|
return [ensure_project_id(dict(it)) for it in items]
|
||
|
|
|
||
|
|
|
||
|
|
def project_catalog_item(item: dict[str, Any]) -> dict[str, Any]:
|
||
|
|
"""Элемент GET /api/projects: только project_id, без id."""
|
||
|
|
row = dict(item)
|
||
|
|
pid = row.pop("id", None)
|
||
|
|
if pid is not None and row.get("project_id") is None:
|
||
|
|
row["project_id"] = int(pid)
|
||
|
|
return row
|
||
|
|
|
||
|
|
|
||
|
|
def project_catalog_items(items: list[dict[str, Any]]) -> list[dict[str, Any]]:
|
||
|
|
return [project_catalog_item(it) for it in items]
|