tempest_fastapi_sdk.reviews¶
Comentários e avaliações de 0 a 5 estrelas sobre qualquer alvo.
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.
|
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; |
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 |
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 ( |
count |
int
|
Number of ratings. |
distribution |
dict[int, int]
|
Count of ratings per star value,
keyed |
RatingCreateSchema
¶
Bases: BaseSchema
Payload to rate a target.
Attributes:
| Name | Type | Description |
|---|---|---|
stars |
int
|
The score, an integer in |
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 |
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. |
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
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
dict[str, Any]: |
dict[str, Any]
|
class: |
dict[str, Any]
|
|
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 |
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 |
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; |
RatingAggregateSchema
|
and every distribution bucket |
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
|
|
'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
|
|
'ratings'
|
class_name
|
str
|
Python class name. |
'RatingModel'
|
Returns:
| Type | Description |
|---|---|
type[BaseRatingModel]
|
type[BaseRatingModel]: A concrete mapped class with a unique |
type[BaseRatingModel]
|
|
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: |
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: |
required |
prefix
|
str
|
URL prefix. Defaults to |
'/api/reviews'
|
tags
|
list[str] | None
|
OpenAPI tags. Defaults to |
None
|
docs_locale
|
str | None
|
Language of the OpenAPI |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
APIRouter |
APIRouter
|
Ready to mount with |