Problem statement
In this Django project, implement the rule that only editors decide what goes live. Gate publish_article and unpublish_article in app/views.py on the app.publish_article permission, and draw the publish and unpublish controls in app/templates/app/article_detail.html only for a visitor who holds it. This closes two routes whose whole check is "is anybody signed in", and stops the page advertising an action it is going to refuse.
Requirements
Implement:
- publish_article in app/views.py
- unpublish_article in app/views.py
- the controls in app/templates/app/article_detail.html
The following files are already implemented and provided: app/models.py, app/urls.py, the article list template, the login template and the set_published(slug, state) helper, which finds the article, writes is_published and redirects to the article's own page. Article holds title, slug (unique), body, is_published, an author pointing at a user, and a get_absolute_url(). Its Meta declares permissions = [("publish_article", "Can publish article")], so the permission row already exists and the name every check uses is app.publish_article. A read-only data migration, app/migrations/0002_editors_group.py, creates a group named editors and puts that permission on it. You create neither the permission nor the group. LOGIN_URL is /accounts/login/, and perms is in every template context because the auth context processor is installed.
Keep the view names publish_article and unpublish_article that app/urls.py imports, and keep set_published as the thing that writes the state.
1. POST /articles/<slug>/publish/ and POST /articles/<slug>/unpublish/
- Purpose: Publish a draft article, or pull a live one down.
- Authentication: Required.
- Authorization: app.publish_article, held directly or through the editors group.
- Response:
- 302 Found: to the article's own page for a permitted user, with is_published changed in the database.
- 403 Forbidden: for a signed-in user without the permission, with is_published left exactly as it was.
- 302 Found: to /accounts/login/ with next= carrying the route that was asked for, for an anonymous visitor.
- A direct grant and a group grant answer identically, because the default authentication backend unions the two sets.
- Keeping the two denials apart is what the order of the checks is for: the signed-in check runs first and sends a stranger to LOGIN_URL with next=, and only then is a refusal raised rather than redirected.
2. GET /articles/<slug>/ - the controls
- Purpose: Show the article, with the controls the visitor is allowed to use.
- Authentication: Not required.
- Response:
- 200 OK: the article page for everybody. The Publish form appears on a draft and the Unpublish form on a live article, both only for a visitor holding app.publish_article. A signed-in user without it and an anonymous visitor get the same page with neither control on it.
- Keep the four control ids publish-form, publish-button, unpublish-form and unpublish-button, and keep article-title on the heading and article-state on the state line: each control is found by its id, and the heading shows that the page itself rendered.
3. Nothing to redeploy
- Adding somebody to the editors group grants them publishing immediately, with no change to any file, and reveals the controls on their next page load. This follows from the two requirements above rather than being extra work.
Notes
- Check the permission app.publish_article. Never substitute a username, a group name or user_permissions for it: a group name lies the moment the group is renamed, and user_permissions counts only a direct grant, so it refuses every member of the group.
- Hiding a control is not the gate, since a hidden form is still a URL anyone can POST to, but leaving it visible invites a request you are going to refuse.
- Permissions are cached per user instance, so a grant made during a request is visible after the user is re-read.
