R1.5 Preference categories and double opt-in - #19
Merged
Conversation
Canonical category vocabulary (newsletter, events, courses, product, transactional) stored as CategoryPreference tags. Subscribe and unsubscribe accept an optional category to enable or disable the canonical preference; transactional cannot be opted out. A transactional send tagged with the transactional category is never suppressed, and suppressed category sends report suppressed: true in the 409 payload. Double opt-in flows through POST /api/subscriptions/request-verification and POST /api/subscriptions/confirm using stateless signed expiring tokens; the verification message goes through a client-owned transactional template with a confirm_url built from SUBSCRIPTION_CONFIRM_BASE_URL. No new migrations.
…ories # Conflicts: # mailing/services/transactional.py # mailing/views.py
This was referenced Sep 8, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements R1.5 (preference categories and double opt-in) from the community-base unification plan.
What changed
mailing/services/categories.py:newsletter,events,courses,product,transactional. A canonical name is stored as theCategoryPreferencetag, so send-time suppression works through the same tag path as free-formcategory_tagvalues. Thetransactionalcategory is always on.POST /api/subscriptions/subscribeandPOST /api/subscriptions/unsubscribeaccept an optionalcategory. Subscribe enables the canonical preference, unsubscribe disables it (updated_reason records the reason orsubscribe/unsubscribe). Unknown values are rejected with400{"category": "unknown"}; opting out oftransactionalis rejected with400{"category": "transactional_cannot_be_disabled"}. Reading: the endpoints keep their existing subscription semantics and the category update is applied on top; requests withoutcategorybehave exactly as before.POST /api/subscriptions/request-verification(scope +categorynottransactional+template_keyowned by the client, fails closed with404{"template_key": "not_found"}when missing, inactive, or foreign) enqueues one verification message through that template. Context carriesconfirm_url(built from the newSUBSCRIPTION_CONFIRM_BASE_URLsetting, defaulting toPUBLIC_BASE_URL),verification_token, andcategory. The token is signed and expiring (48h,django.core.signing.TimestampSigner, salted) and carries only ids plus the category name, so no raw email appears in URLs, tokens, or logs.POST /api/subscriptions/confirmvalidates signature, expiry, and that the token was issued for the authenticated client, enables the preference (creating it when needed), and returns the resulting preference state; it is idempotent. Any invalid token is a single400{"token": "invalid"}.category_tag: "transactional"is never suppressed by the category check (even for globally unsubscribed contacts); all other suppressed sends now include"suppressed": truein the 409 error payload next to the existingcode/message/reasonfields.API_DOC_PATHS,OPENAPI_SPECpaths and schemas,route_path_map, and the staff endpoint reference updated together for the two new endpoints;categorydocumented on the subscribe/unsubscribe request schemas.docs/api.md: canonical category table,categoryparameter semantics, the double opt-in flow, and the suppressed send response.origin/main(R1.3) mid-flight; conflicts were two import blocks inmailing/services/transactional.pyandmailing/views.py. The branch adds no migrations; the only migration in the merged tree is R1.3's0026.Verification
uv run --frozen ruff check .-> pass:uv run --frozen pytest -q(full suite, after merging R1.3) -> pass:uv run --frozen python manage.py check->System check identified no issues (0 silenced).uv run --frozen python manage.py makemigrations --check --dry-run->No changes detected.New tests cover: subscribe/unsubscribe with and without
category, backwards compatibility (no preference rows withoutcategory), unknown category rejected without partial mutation,transactionalopt-out rejected, subscribe acceptingcategory: "transactional", scope isolation (another client/audience preference untouched), canonical-category suppressed send reportssuppressed: true, send withcategory_tag: "transactional"queued despite a globally unsubscribed contact, double opt-in happy path (request -> token -> confirm -> enabled, idempotent repeat), expired token rejected, tampered token rejected, confirm scoped to the issuing client, and request-verification failing closed for transactional/unknown categories and missing, unknown, foreign, inactive, or context-starved templates. The confirm URL in tests is asserted to contain no raw email.Not run here, needs:
Sandbox verification from the issue, requiring this branch deployed to the sandbox:
category=eventseventscategory=events->suppressedcategory=transactional->queued