"""people.py — ответственные: владелец, сотрудники, поиск контакта. Ответственный хранится в VTODO как `X-RESPONSIBLE` (id контакта) плюс копии имени и адреса. Владелец определяется по `TASK_OWNER_EMAIL` из `.env` проекта; сотрудники — контакты с `is_employee: true`. """ from __future__ import annotations import os from pathlib import Path from typing import Any ENV_FILE = Path(__file__).resolve().parent.parent / ".env" DEFAULT_OWNER_EMAIL = "e.storozhenko@vinogorod.ru" def _env(name: str, default: str = "") -> str: value = os.environ.get(name) if value: return value.strip() try: for line in ENV_FILE.read_text(encoding="utf-8").splitlines(): line = line.strip() if line.startswith("#") or "=" not in line: continue key, _, val = line.partition("=") if key.strip() == name: return val.strip().strip('"').strip("'") except OSError: pass return default def owner_email() -> str: return (_env("TASK_OWNER_EMAIL", DEFAULT_OWNER_EMAIL) or DEFAULT_OWNER_EMAIL).lower() def owner_name() -> str: return _env("TASK_OWNER_NAME", "Стороженко Евгений Владимирович") def _contacts() -> list[dict[str, Any]]: from webui import contacts as contacts_mod db = contacts_mod.load() return contacts_mod.visible(db) def employees() -> list[dict[str, Any]]: """Контакты, помеченные сотрудниками (звёздочкой в карточке).""" return [c for c in _contacts() if c.get("is_employee")] def contact_by_id(contact_id: str) -> dict[str, Any] | None: if not contact_id: return None for contact in _contacts(): if contact.get("id") == contact_id: return contact return None def contact_by_email(email: str) -> dict[str, Any] | None: needle = (email or "").strip().lower() if not needle: return None for contact in _contacts(): if (contact.get("email") or "").strip().lower() == needle: return contact return None def owner_contact() -> dict[str, Any] | None: return contact_by_email(owner_email()) def is_owner(task: dict[str, Any]) -> bool: """Задача «моя»: ответственный — владелец (по id, иначе по адресу).""" email = (task.get("responsible_email") or "").strip().lower() if email: return email == owner_email() contact = contact_by_id(task.get("responsible") or "") if contact is not None: return (contact.get("email") or "").strip().lower() == owner_email() return False def assignee(task: dict[str, Any]) -> dict[str, Any]: """Описание ответственного для интерфейса.""" contact_id = (task.get("responsible") or "").strip() name = (task.get("responsible_name") or "").strip() email = (task.get("responsible_email") or "").strip() if contact_id: contact = contact_by_id(contact_id) if contact is not None: name = name or (contact.get("full_name") or "") email = email or (contact.get("email") or "") return {"id": contact_id, "name": name, "email": email, "owner": is_owner(task), "set": bool(contact_id or name)}