approval_cycle.py 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. """审批轮次过滤 — 驳回后重新提交时,进度接口仅展示当前轮次。"""
  2. from __future__ import annotations
  3. from datetime import datetime
  4. from typing import List, Sequence, TypeVar
  5. T = TypeVar("T")
  6. def filter_current_cycle_approvals(
  7. approvals: Sequence[T],
  8. apply_status: str,
  9. *,
  10. status_attr: str = "approval_status",
  11. time_attr: str = "approval_time",
  12. ) -> List[T]:
  13. """审批进度仅展示当前轮次;完整历史由 get_approval_history 返回。"""
  14. rejections = [a for a in approvals if getattr(a, status_attr) == "rejected"]
  15. if not rejections:
  16. return list(approvals)
  17. sorted_rejections = sorted(
  18. rejections,
  19. key=lambda a: getattr(a, time_attr) or datetime.min,
  20. )
  21. last_rejection_time = getattr(sorted_rejections[-1], time_attr) or datetime.min
  22. if apply_status != "rejected":
  23. return [
  24. a
  25. for a in approvals
  26. if (getattr(a, time_attr) or datetime.min) > last_rejection_time
  27. ]
  28. cycle_start = datetime.min
  29. if len(sorted_rejections) >= 2:
  30. cycle_start = getattr(sorted_rejections[-2], time_attr) or datetime.min
  31. return [
  32. a
  33. for a in approvals
  34. if (getattr(a, time_attr) or datetime.min) > cycle_start
  35. ]