fix(poll): harden lifecycle policy boundaries
This commit is contained in:
@@ -22,6 +22,7 @@ class PollTransitionRule:
|
||||
source_statuses: frozenset[str]
|
||||
target_status: str | None
|
||||
restore_archived_status: bool = False
|
||||
missing_restore_target_status: str | None = None
|
||||
|
||||
|
||||
@dataclass(frozen=True)
|
||||
@@ -56,8 +57,12 @@ class PollTransitionPolicy:
|
||||
if rule.restore_archived_status:
|
||||
if rule.target_status is not None:
|
||||
raise ValueError(f"Restoring transition {action!r} cannot have a fixed target")
|
||||
if rule.missing_restore_target_status not in (POLL_STATUSES - {"archived"}) | {None}:
|
||||
raise ValueError(f"Restoring transition {action!r} has an invalid missing-origin target")
|
||||
elif rule.target_status not in POLL_STATUSES:
|
||||
raise ValueError(f"Poll transition rule {action!r} has an unknown target status")
|
||||
elif rule.missing_restore_target_status is not None:
|
||||
raise ValueError(f"Non-restoring transition {action!r} cannot have a missing-origin target")
|
||||
self._rules = MappingProxyType(normalized)
|
||||
|
||||
@property
|
||||
@@ -97,6 +102,7 @@ DEFAULT_POLL_TRANSITION_POLICY = PollTransitionPolicy(
|
||||
source_statuses=frozenset({"archived"}),
|
||||
target_status=None,
|
||||
restore_archived_status=True,
|
||||
missing_restore_target_status="draft",
|
||||
),
|
||||
}
|
||||
)
|
||||
@@ -125,9 +131,12 @@ class PollTransitionEngine:
|
||||
|
||||
target_status = rule.target_status
|
||||
if rule.restore_archived_status:
|
||||
if archived_from_status not in POLL_STATUSES - {"archived"}:
|
||||
if archived_from_status is None and rule.missing_restore_target_status is not None:
|
||||
target_status = rule.missing_restore_target_status
|
||||
elif archived_from_status not in POLL_STATUSES - {"archived"}:
|
||||
raise ValueError("Archived poll has no valid status to restore")
|
||||
target_status = archived_from_status
|
||||
else:
|
||||
target_status = archived_from_status
|
||||
assert target_status is not None
|
||||
return PollTransitionPlan(action=action, from_status=current_status, to_status=target_status)
|
||||
|
||||
@@ -140,7 +149,11 @@ class PollTransitionEngine:
|
||||
actions: list[PollTransitionAvailability] = []
|
||||
for action in POLL_TRANSITION_ACTIONS:
|
||||
rule = self.policy.rules[action]
|
||||
target_status = archived_from_status if rule.restore_archived_status else rule.target_status
|
||||
target_status = (
|
||||
archived_from_status or rule.missing_restore_target_status
|
||||
if rule.restore_archived_status
|
||||
else rule.target_status
|
||||
)
|
||||
try:
|
||||
self.plan(
|
||||
current_status=current_status,
|
||||
|
||||
Reference in New Issue
Block a user