Skip to main content

stapel-reviews

CI coverage pypi downloads python license llms.txt

Target-generic reviews and ratings: an author rates and reviews an opaque host-defined target (target_type + target_key), driven by a per-target-type policy registry (who may review, pre/post moderation, one-review-per-author, owner responses) whose authority questions are answered by host comm callbacks. The module owns the per-target aggregate (avg/count over published reviews) and emits a generic visibility-change fact carrying it, so a host catalog maintains its own rating projection without calling back.

Part of the Stapel framework — composable Django apps that deploy as a monolith or as microservices without changing module code.

Install

pip install stapel-reviews

At a glance

Fact Value
Version 0.8.0
Python >=3.11 (3.11, 3.12, 3.13, 3.14)
HTTP operations 6
Config axes 2
Usage surface 21
Extension points 11
Error codes 56
Fleet dependencies stapel-auth (optional) · stapel-core

Documentation

OpenAPI · capabilities.json · llms.txt (for agents)

What this is

A generic review core — Review (author + rating + body about an opaque target) and Response (the target owner's reply) — driven entirely by a per-target-type policy registry. The module ships knowing nothing about what gets reviewed: a host registers its target types (a seller, a listing, a driver, a course), each with a policy, and answers the domain questions — "may this author review?", "who owns this target?" — through comm Function callbacks, so the module never imports a host model.

Quick start

INSTALLED_APPS = [
    # ...
    "stapel_reviews",
]

# urls.py
path("reviews/", include("stapel_reviews.urls"))

Concepts

  • Target — opaque: target_type (a key the host registered) + target_key (an opaque host string — a UUID, a slug, a composite). No FK to any host model; the module is domain-blind.
  • Policy — per target type: who may review (can_review comm callback), pre/post moderation, one-review-per-author, whether owner responses are allowed (allow_response), who may moderate (can_moderate comm callback), who may respond (can_respond, else the target owner), self-review (allow_self_review, default off), and — optionally — who owns a target (owner_key_for).
  • Aggregate — the module owns avg/count over published reviews per target, and emits a generic fact carrying it on every visibility change, so a host catalog maintains its own rating projection (§10) without calling back.
  • Owner key — optional, denormalised: the answer of the type's owner_key_for resolver, stamped on the review when it is written, so the module can also aggregate everything one owner owns (a seller-wide rating) while still knowing nothing about what a listing or a seller is.
STAPEL_REVIEWS = {
    "TARGET_TYPES": {
        "seller": {
            "can_review": "marketplace.buyer_of_seller",   # host comm Function
            "can_respond": "marketplace.is_seller_owner",
            "moderation": "post",
            "one_per_author": True,
            "allow_response": True,
        },
        "listing": {
            "moderation": "pre",
            # Optional: who owns this target. A callable, or a comm Function
            # name for a policy that has to stay JSON-shaped. Registering none
            # leaves owner_key empty and changes nothing else.
            "owner_key_for": lambda target_key: seller_id_of(target_key),
        },
    },
}
from stapel_reviews import services

review = services.create_review(
    target_type="seller", target_key="s-42", author=user, rating=5, body="great",
)
services.moderate_review(review, actor=owner, action="hide", reason="spam")
services.respond(review, author=owner, body="thanks for the feedback")
agg = services.aggregate("seller", "s-42")   # Aggregate(avg=..., count=...)

# Everything one owner owns, batched (the seller-wide rating):
services.aggregates_by_owner_keys(["s-42", "s-43"])  # {"s-42": {"avg": .., "count": ..}}

Settings

All configuration lives in the STAPEL_REVIEWS namespace (dict setting, flat setting, or env var — resolved lazily):

Key Default Meaning
TARGET_TYPES {} The target-type registry {type: policy}, merged over the (empty) built-ins; None removes a type
MODERATION_DEFAULT "post" Default moderation mode (post/pre) for types that don't override it
RESPONSES True Whether owner responses are allowed by default
RATING_MIN 1 Inclusive minimum rating
RATING_MAX 5 Inclusive maximum rating
MODERATION_TARGET_TYPE "review" The target_type on an incoming moderation.completed verdict that means "this is about a review"

comm surface

Kind Name Contract
Emit reviews.review.published A review became visible — carries {aggregate: {avg, count}} for the host projection
Emit reviews.review.hidden A review left the visible set — carries the updated aggregate
Function reviews.aggregate {target_type, target_key} -> {avg, count}
Function reviews.aggregates_by_keys {keys, target_type?} -> {key: {avg, count}} — a Projection's live_query
Function reviews.aggregates_by_owner_keys {owner_keys, target_type?} -> {owner_key: {avg, count}} — the owner-wide rating
Function reviews.aggregates_export {cursor?, limit?} -> {rows, cursor, total} — a Projection's source_of_truth
Function reviews.moderation_content {review_id} -> {text, title, language, media, author_id, url, …}
Consume moderation.completed {target_type, target_key, decision, …} — a platform verdict, applied to the review as the system actor
Callback (host) policy can_review {author_id, target_type, target_key} -> bool — the host answers
Callback (host) policy can_moderate {actor_id, target_type, target_key} -> bool — hide/publish, include=all
Callback (host) policy can_respond {actor_id, target_type, target_key, owner_key} -> bool — owner reply; unset = the target owner
Function reviews.owner_is_target {target_type, target_key} -> {owner_key: target_key} — a ready owner_key_for for a target that is its owner
Callback (host) policy owner_key_for target_key -> str | None (callable), or a comm Function {target_type, target_key} -> owner key — optional

Host rating projection

The two batch Functions are the halves a host Projection over reviews is declared against — one keyed read for live traffic, one snapshot for rebuild:

class ListingReviewSummaryProjection(Projection):
    consumes = ("reviews.review.published", "reviews.review.hidden")
    source_key = "target_key"
    live_query = "reviews.aggregates_by_keys"      # local mode reads through it
    source_of_truth = "reviews.aggregates_export"  # rebuild() / drift_check()

Owner-wide ratings

A marketplace needs the rating of a seller, not only of each listing — and the module must not learn what a listing is to produce it. The seam is one optional resolver and one denormalised column:

STAPEL_REVIEWS = {
    "TARGET_TYPES": {
        "listing": {"owner_key_for": "catalog.owner_of_listing"},  # or a callable
    },
}

The resolver is asked once, when the review is written, and its answer is stored on Review.owner_key. Reads go through reviews.aggregates_by_owner_keys or POST /reviews/api/v1/reviews/aggregates/by-owner (public, up to 100 owner keys per call), which return {owner_key: {avg, count}} over published reviews with the same rounding as reviews.aggregate.

The same column answers the seller page's rows, not only its number: the list endpoint takes owner_key instead of the target pair —

GET /reviews/api/v1/reviews?owner_key=s-42[&target_type=listing]

— every review of everything that owner owns, newest first, same visibility rule, same anchor pagination, same item shape. Exactly one addressing per request: naming both a target and an owner is error.400.reviews_ambiguous_addressing, naming neither is error.400.reviews_unknown_target_type as before. Reviews written before the resolver was registered carry an empty owner key — stamp them once with:

python manage.py reviews_backfill_owner_keys [--target-type listing] [--dry-run]

A host that registers no resolver stamps nothing, and the owner aggregate simply answers {}.

Moderation verdicts

An external moderation module owns the decision; this module owns applying it. When a case about a review resolves, moderation.completed arrives and the review is hidden (rejected) or published (approved) — needs_review and dismissed deliberately move nothing. The verdict is applied as services.SYSTEM_ACTOR, the one actor that gets past the fail-closed can_moderate gate: authorization already happened where the verdict was made, and asking a target type with no can_moderate callback would deny the platform its own decision. Redelivery is a no-op — idempotency is by state, and no table of processed event ids is kept.

Extension points

See MODULE.md — the agent-facing map of every fork-free seam (the TARGET_TYPES registry and its policy callbacks, the projection emits, the aggregate Function, serializer seams, settings).

Development

pip install -e . && pip install pytest pytest-django ruff
./setup-hooks.sh
pytest tests/

License

MIT — see LICENSE.


This page is assembled by stapel-readme from docs/readme.md plus the contract artifacts in docs/. Edit the prose in docs/readme.md; the badges, facts and links above and below it are generated — do not hand-edit README.md.

Metadata

Release files for stapel-reviews 0.8.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for stapel-reviews 0.8.0
File Size Uploaded Provenance Actions
stapel_reviews-0.8.0.tar.gz 109.1 kB With provenance Details for stapel_reviews-0.8.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for stapel-reviews 0.8.0
File Interpreter ABI Platform Actions
stapel_reviews-0.8.0-py3-none-any.whl Python 3 none any Details for stapel_reviews-0.8.0-py3-none-any.whl

Total release size: 195.7 kB

Release files / stapel_reviews-0.8.0.tar.gz

Download URL stapel_reviews-0.8.0.tar.gz
Size 109.1 kB
Tags Source
SHA-256 checksum
How to use checksums
6a26b4e296e18edd060810a1a380ddcac3c0512966a369780baa74a841f4e825
BLAKE2b-256 checksum
How to use checksums
b9b193b3f9055a184cf44dfa75b071964c692bef822c281fae51ee612124db2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / stapel_reviews-0.8.0-py3-none-any.whl

Download URL stapel_reviews-0.8.0-py3-none-any.whl
Size 86.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0bfedeaeb853b2f457ec4b9d7801c3e202fa7123b3021de110607f3dc5a38eb
BLAKE2b-256 checksum
How to use checksums
0d35456f8b93331cc5c1aaccbae6201588082602849f3cc896fa4634bc449fbe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Security for stapel-reviews 0.8.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for stapel_reviews-0.8.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page