Ir para o conteúdo

tempest_fastapi_sdk.reviews

Comentários e avaliações de 0 a 5 estrelas sobre qualquer alvo.

« Tempo real e mensageria


reviews

Reviews module — comments and 0-to-5-star ratings on any target.

Reusable building blocks over the SDK primitives (BaseModel / BaseRepository / pagination / validated fields): polymorphic abstract tables + make_* factories, a :class:ReviewService (comment, rate with one vote per user, aggregate), and an opt-in :func:make_reviews_router.

BaseCommentModel

Bases: BaseModel

Abstract comment on a polymorphic target, with optional threading.

Attributes:

Name Type Description
target_type str

The kind of thing commented on (e.g. "product").

target_id UUID

The id of the target within its type.

author_id UUID

FK to the comment author (set by subclass).

body str

The comment text.

parent_id UUID | None

Optional parent comment id for threaded replies; None for a top-level comment.

BaseRatingModel

Bases: BaseModel

Abstract 0-to-5-star rating of a polymorphic target, one per user.

A (target_type, target_id, user_id) triple is unique — a user rates a target once; re-rating updates the existing row (see :meth:~tempest_fastapi_sdk.reviews.ReviewService.rate).

Attributes:

Name Type Description
target_type str

The kind of thing rated.

target_id UUID

The id of the target within its type.

user_id UUID

FK to the rating user (set by subclass).

stars int

The score, an integer in 0..5.

CommentCreateSchema

Bases: BaseSchema

Payload to post a comment.

Attributes:

Name Type Description
body str

The comment text (non-empty).

parent_id UUID | None

Optional parent comment for a reply.

CommentResponseSchema

Bases: BaseSchema

A comment as returned to clients.

Attributes:

Name Type Description
id UUID

The comment id.

target_type str

The kind of target.

target_id UUID

The target id.

author_id UUID

The comment author.

body str

The comment text.

parent_id UUID | None

Parent comment id, if a reply.

created_at datetime

When the comment was posted.

RatingAggregateSchema

Bases: BaseSchema

Aggregated rating statistics for one target.

Attributes:

Name Type Description
target_type str

The kind of target.

target_id UUID

The target id.

average float

Mean star score (0.0 when there are no ratings).

count int

Number of ratings.

distribution dict[int, int]

Count of ratings per star value, keyed 0..5 (missing values default to 0).

RatingCreateSchema

Bases: BaseSchema

Payload to rate a target.

Attributes:

Name Type Description
stars int

The score, an integer in 0..5.

RatingResponseSchema

Bases: BaseSchema

A rating as returned to clients.

Attributes:

Name Type Description
id UUID

The rating id.

target_type str

The kind of target.

target_id UUID

The target id.

user_id UUID

The rating user.

stars int

The score in 0..5.

created_at datetime

When the rating was created.

ReviewService

ReviewService(*, comments: BaseRepository[Any], ratings: BaseRepository[Any])

Post comments and manage per-user star ratings on any target.

Attributes:

Name Type Description
comments BaseRepository[Any]

Repository for the comment table.

ratings BaseRepository[Any]

Repository for the rating table.

Initialize the service.

Parameters:

Name Type Description Default
comments BaseRepository[Any]

Comment repository.

required
ratings BaseRepository[Any]

Rating repository.

required

add_comment async

add_comment(
    target_type: str,
    target_id: UUID,
    author_id: UUID,
    body: str,
    *,
    parent_id: UUID | None = None,
) -> CommentResponseSchema

Post a comment on a target.

Parameters:

Name Type Description Default
target_type str

The kind of target (e.g. "product").

required
target_id UUID

The target id.

required
author_id UUID

The commenting user.

required
body str

The comment text.

required
parent_id UUID | None

Optional parent comment for a reply.

None

Returns:

Name Type Description
CommentResponseSchema CommentResponseSchema

The persisted comment.

list_comments async

list_comments(
    target_type: str,
    target_id: UUID,
    *,
    page: int = 1,
    page_size: int = 20,
    ascending: bool = True,
) -> dict[str, Any]

Return an offset page of a target's comments.

Parameters:

Name Type Description Default
target_type str

The kind of target.

required
target_id UUID

The target id.

required
page int

1-indexed page number.

1
page_size int

Comments per page.

20
ascending bool

Oldest-first when True.

True

Returns:

Type Description
dict[str, Any]

dict[str, Any]: items (mapped

dict[str, Any]

class:CommentResponseSchema), total, page,

dict[str, Any]

size and pages.

rate async

rate(
    target_type: str, target_id: UUID, user_id: UUID, stars: int
) -> RatingResponseSchema

Set a user's star rating for a target (one vote per user).

Upserts: if the user already rated the target, the existing row's stars is updated; otherwise a new row is created.

Parameters:

Name Type Description Default
target_type str

The kind of target.

required
target_id UUID

The target id.

required
user_id UUID

The rating user.

required
stars int

The score in 0..5.

required

Returns:

Name Type Description
RatingResponseSchema RatingResponseSchema

The created or updated rating.

get_user_rating async

get_user_rating(
    target_type: str, target_id: UUID, user_id: UUID
) -> RatingResponseSchema | None

Return a user's rating for a target, or None.

Parameters:

Name Type Description Default
target_type str

The kind of target.

required
target_id UUID

The target id.

required
user_id UUID

The user whose rating to fetch.

required

Returns:

Type Description
RatingResponseSchema | None

RatingResponseSchema | None: The rating, or None when the

RatingResponseSchema | None

user has not rated the target.

aggregate async

aggregate(target_type: str, target_id: UUID) -> RatingAggregateSchema

Return average, count and per-star distribution for a target.

Parameters:

Name Type Description Default
target_type str

The kind of target.

required
target_id UUID

The target id.

required

Returns:

Name Type Description
RatingAggregateSchema RatingAggregateSchema

The aggregate; average is 0.0

RatingAggregateSchema

and every distribution bucket 0 when there are no ratings.

make_comment_model

make_comment_model(
    *,
    user_table: str = "users",
    tablename: str = "comments",
    class_name: str = "CommentModel",
) -> type[BaseCommentModel]

Build a concrete CommentModel subclass at runtime.

Parameters:

Name Type Description Default
user_table str

Table name of the concrete user model the author FK references.

'users'
tablename str

__tablename__ for the generated class.

'comments'
class_name str

Python class name.

'CommentModel'

Returns:

Type Description
type[BaseCommentModel]

type[BaseCommentModel]: A concrete mapped class.

make_rating_model

make_rating_model(
    *,
    user_table: str = "users",
    tablename: str = "ratings",
    class_name: str = "RatingModel",
) -> type[BaseRatingModel]

Build a concrete RatingModel subclass at runtime.

Parameters:

Name Type Description Default
user_table str

Table name of the concrete user model the FK references.

'users'
tablename str

__tablename__ for the generated class.

'ratings'
class_name str

Python class name.

'RatingModel'

Returns:

Type Description
type[BaseRatingModel]

type[BaseRatingModel]: A concrete mapped class with a unique

type[BaseRatingModel]

(target_type, target_id, user_id) constraint.

make_reviews_router

make_reviews_router(
    *,
    service_factory: Callable[[AsyncSession], ReviewService],
    session_factory: Callable[..., AsyncIterator[AsyncSession]],
    current_user_id: Callable[..., Any],
    prefix: str = "/api/reviews",
    tags: list[str] | None = None,
    docs_locale: str | None = None,
) -> APIRouter

Build the reviews router.

Endpoints (writes require authentication via current_user_id):

  • POST {prefix}/{target_type}/{target_id}/comments -> post a comment (author is the caller).
  • GET {prefix}/{target_type}/{target_id}/comments -> page the comments.
  • POST {prefix}/{target_type}/{target_id}/rating -> set the caller's 0-5 star rating (upsert).
  • GET {prefix}/{target_type}/{target_id} -> the rating aggregate (average, count, per-star distribution).

Parameters:

Name Type Description Default
service_factory Callable[[AsyncSession], ReviewService]

Builds a request-scoped :class:ReviewService from the session.

required
session_factory Callable[..., AsyncIterator[AsyncSession]]

Yields a request-scoped DB session.

required
current_user_id Callable[..., Any]

FastAPI dependency resolving the authenticated user's :class:~uuid.UUID.

required
prefix str

URL prefix. Defaults to "/api/reviews".

'/api/reviews'
tags list[str] | None

OpenAPI tags. Defaults to ["reviews"].

None
docs_locale str | None

Language of the OpenAPI summary and description text — "en-US" or "pt-BR". None (default) reads API_DOCS_LOCALE through :class:~tempest_fastapi_sdk.settings.ServerSettings, whose default is "en-US".

None

Returns:

Name Type Description
APIRouter APIRouter

Ready to mount with app.include_router.