Sub topic 11
Publishing only what was approved
This chapter assumes you have read the earlier chapters on content states and approval gates. You should also be comfortable reading short Python programs. Here you will make sure the thing that goes live is exactly the thing a person approved.
Why approval alone is not enough
An approval is a statement about a specific piece of text. If the text changes after approval, the approval no longer describes what you are about to publish. A late edit, a merge from another branch, or an automated "fix" can all alter the text silently.
So the publish step must check two things:
- The item is in the
APPROVEDstate. - The text being published matches the text that was approved.
Binding approval to a hash
A hash is a short fingerprint computed from text. Change one character and the fingerprint changes completely. At approval time, store the hash of the text. At publish time, hash the text again and compare.
The Item class from the workflow chapter does this in two places:
def approve(self, reviewer):
if reviewer == self.author:
raise PermissionError("the author cannot approve their own item")
self._move(State.APPROVED, reviewer)
self.approved_digest = digest(self.text)
def publish(self, actor):
if self.approved_digest != digest(self.text):
raise ValueError("text differs from the approved version")
self._move(State.PUBLISHED, actor)Two details matter here. First, approve refuses the author, which keeps separation of duties. Second, publish compares hashes before it calls _move. If the check fails, nothing changes and no log entry is written for a publish.
Closing the other doors
The hash check protects against changed text. Other rules protect the path to it:
reviseonly works inDRAFTorCHANGES_REQUESTED. An approved item cannot be edited in place.PUBLISHEDhas no allowed transitions, so a live item cannot be pushed back through the workflow by accident.- Only
APPROVEDcan move toPUBLISHED. There is no shortcut fromDRAFT.
A state machine plus a hash gives you two independent defenses. If someone bypasses one, the other still catches the problem.
Scaling the gate to risk
Higher-risk content needs more than one approver. Count only people other than the author:
REQUIRED_APPROVALS = {"low": 1, "medium": 1, "high": 2}
def can_publish(risk_tier, approvers, author):
independent = {name for name in approvers if name != author}
return len(independent) >= REQUIRED_APPROVALS[risk_tier]
print(can_publish("high", {"ana"}, "ben"))
print(can_publish("high", {"ana", "carla"}, "ben"))The first call prints False because one approver is not enough for a high-risk item. The second prints True. Using a set means the same person approving twice counts once.
Common errors
| Mistake | Result | Fix |
|---|---|---|
| Storing "approved" as a flag with no hash | Edits after approval go live unreviewed | Store the digest at approval time |
| Hashing a different form of the text at publish (for example, after trimming whitespace) | False rejections or missed changes | Hash the exact same string in both places |
| Letting the publisher edit text "just a little" | The approval no longer applies | Send the edit back through review |
| Counting the author as an approver | Separation of duties is lost | Filter the author out, as `can_publish` does |
What to do when the check fails
A failed publish is not an emergency. It means the control worked. Return the item to review, show the reviewer what changed since approval, and approve the new version. The old approval stays in the log, and the new one replaces the stored digest.
Exercise
An editor approves an article. Before it is published, a teammate fixes a typo directly in the stored text without going through review. What happens when someone calls publish, and why?
Show answer
publish raises ValueError("text differs from the approved version"). The stored approved_digest was computed from the original text, so the hash of the corrected text no longer matches. The item must go back through review, and be approved again, before it can be published.