DTOs¶
The models/dtos subpackage contains Pydantic BaseModel data transfer objects used for API request/response shapes,
pagination, sorting, search, and email payloads.
Base DTOs¶
Base DTO classes providing common fields and validators inherited by domain-specific DTOs.
archipy.models.dtos.base_dtos.BaseDTO ¶
Bases: BaseModel
Base Data Transfer Object class.
This class extends Pydantic's BaseModel to provide common configuration for all DTOs in the application.
Source code in archipy/models/dtos/base_dtos.py
archipy.models.dtos.base_dtos.BaseDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
options: show_root_toc_entry: false heading_level: 3
Base Protobuf DTO¶
Base DTO class for Protobuf-backed data transfer objects, bridging gRPC and Pydantic models.
archipy.models.dtos.base_protobuf_dto.PROTOBUF_AVAILABLE
module-attribute
¶
archipy.models.dtos.base_protobuf_dto.BaseProtobufDTO ¶
Bases: BaseDTO
A base DTO that can be converted to and from a Protobuf message.
Requires 'google-protobuf' to be installed.
Source code in archipy/models/dtos/base_protobuf_dto.py
archipy.models.dtos.base_protobuf_dto.BaseProtobufDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.models.dtos.base_protobuf_dto.BaseProtobufDTO.from_proto
classmethod
¶
Converts a Protobuf message into a Pydantic DTO instance.
Source code in archipy/models/dtos/base_protobuf_dto.py
archipy.models.dtos.base_protobuf_dto.BaseProtobufDTO.to_proto ¶
Converts the Pydantic DTO instance into a Protobuf message.
Source code in archipy/models/dtos/base_protobuf_dto.py
options: show_root_toc_entry: false heading_level: 3
Pagination DTO¶
DTOs for pagination input and output, including page number, page size, and total count fields.
archipy.models.dtos.pagination_dto.PaginationDTO ¶
Bases: BaseDTO
Data Transfer Object for pagination parameters.
This DTO encapsulates pagination information for database queries and API responses, providing a standard way to specify which subset of results to retrieve.
Attributes:
| Name | Type | Description |
|---|---|---|
page |
int
|
The current page number (1-based indexing) |
page_size |
int
|
Number of items per page |
offset |
int
|
Calculated offset for database queries based on page and page_size |
Examples:
>>> from archipy.models.dtos.pagination_dto import PaginationDTO
>>>
>>> # Default pagination (page 1, 10 items per page)
>>> pagination = PaginationDTO()
>>>
>>> # Custom pagination
>>> pagination = PaginationDTO(page=2, page_size=25)
>>> print(pagination.offset) # Access offset as a property
25
>>>
>>> # Using with a database query
>>> def get_users(pagination: PaginationDTO):
... query = select(User).offset(pagination.offset).limit(pagination.page_size)
... return db.execute(query).scalars().all()
Source code in archipy/models/dtos/pagination_dto.py
archipy.models.dtos.pagination_dto.PaginationDTO.page
class-attribute
instance-attribute
¶
archipy.models.dtos.pagination_dto.PaginationDTO.page_size
class-attribute
instance-attribute
¶
archipy.models.dtos.pagination_dto.PaginationDTO.MAX_ITEMS
class-attribute
instance-attribute
¶
archipy.models.dtos.pagination_dto.PaginationDTO.offset
property
¶
Calculate the offset for database queries.
This property calculates how many records to skip based on the current page and page size.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
The number of records to skip |
Examples:
archipy.models.dtos.pagination_dto.PaginationDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.models.dtos.pagination_dto.PaginationDTO.validate_pagination ¶
Validate pagination limits to prevent excessive resource usage.
Ensures that the requested number of items (page * page_size) doesn't exceed the maximum allowed limit.
Returns:
| Type | Description |
|---|---|
Self
|
The validated model instance if valid. |
Raises:
| Type | Description |
|---|---|
OutOfRangeError
|
If the total requested items exceeds MAX_ITEMS. |
Source code in archipy/models/dtos/pagination_dto.py
options: show_root_toc_entry: false heading_level: 3
Sort DTO¶
DTOs for expressing sort order in list/search requests.
archipy.models.dtos.sort_dto.SortDTO ¶
Bases: BaseModel
Data Transfer Object for sorting parameters.
This DTO encapsulates sorting information for database queries and API responses, providing a standard way to specify how results should be ordered.
Attributes:
| Name | Type | Description |
|---|---|---|
column |
T | str
|
The name or enum value of the column to sort by |
order |
str
|
The sort direction - "ASC" for ascending, "DESC" for descending |
Examples:
>>> from archipy.models.dtos.sort_dto import SortDTO
>>> from archipy.models.types.sort_order_type import SortOrderType
>>>
>>> # Sort by name in ascending order
>>> sort = SortDTO(column="name", order=SortOrderType.ASCENDING)
>>>
>>> # Sort by creation date in descending order (newest first)
>>> sort = SortDTO(column="created_at", order="DESCENDING")
>>>
>>> # Using with a database query
>>> def get_sorted_users(sort: SortDTO = SortDTO.default()):
... query = select(User)
... if sort.order == SortOrderType.ASCENDING:
... query = query.order_by(getattr(User, sort.column).asc())
... else:
... query = query.order_by(getattr(User, sort.column).desc())
... return db.execute(query).scalars().all()
>>>
>>> # Using with enum column types
>>> from enum import Enum
>>> class UserColumns(Enum):
... ID = "id"
... NAME = "name"
... EMAIL = "email"
... CREATED_AT = "created_at"
>>>
>>> # Create a sort configuration with enum
>>> sort = SortDTO[UserColumns](column=UserColumns.NAME, order=SortOrderType.ASCENDING)
Source code in archipy/models/dtos/sort_dto.py
archipy.models.dtos.sort_dto.SortDTO.column
class-attribute
instance-attribute
¶
archipy.models.dtos.sort_dto.SortDTO.order
class-attribute
instance-attribute
¶
order: SortOrderType = Field(
default=DESCENDING,
description="Sort order (ASCENDING or DESCENDING)",
)
archipy.models.dtos.sort_dto.SortDTO.default
classmethod
¶
Create a default sort configuration.
Returns a sort configuration that orders by created_at in descending order (newest first), which is a common default sorting behavior.
Returns:
| Name | Type | Description |
|---|---|---|
SortDTO |
SortDTO
|
A default sort configuration |
Examples:
>>> default_sort = SortDTO.default()
>>> print(f"Sort by {default_sort.column} {default_sort.order}")
Sort by created_at DESCENDING
Source code in archipy/models/dtos/sort_dto.py
options: show_root_toc_entry: false heading_level: 3
Search Input DTO¶
DTO for structured search input combining filter, sort, and pagination parameters.
archipy.models.dtos.search_input_dto.SearchInputDTO ¶
Bases: BaseModel
Data Transfer Object for search inputs with pagination and sorting.
This DTO encapsulates search parameters for database queries and API responses, providing a standard way to handle pagination and sorting.
Class Type Parameters:
| Name | Bound or Constraints | Description | Default |
|---|---|---|---|
T
|
The type for sort column (usually an Enum with column names). |
required |
Source code in archipy/models/dtos/search_input_dto.py
options: show_root_toc_entry: false heading_level: 3
Range DTOs¶
DTOs for expressing numeric and date range filters in queries.
archipy.models.dtos.range_dtos.Comparable ¶
Bases: Protocol
Protocol for types that support comparison operators.
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.BaseRangeDTO ¶
Bases: BaseDTO
Base Data Transfer Object for range queries.
Encapsulates a range of values with from_ and to fields. Provides validation to ensure range integrity.
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.BaseRangeDTO.from_
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.BaseRangeDTO.to
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.BaseRangeDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.models.dtos.range_dtos.BaseRangeDTO.validate_range ¶
Validate that from_ is less than or equal to to when both are provided.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The validated model instance. |
Raises:
| Type | Description |
|---|---|
OutOfRangeError
|
If from_ is greater than to. |
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.DecimalRangeDTO ¶
Bases: BaseRangeDTO[Decimal]
Data Transfer Object for decimal range queries.
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.DecimalRangeDTO.from_
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.DecimalRangeDTO.to
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.DecimalRangeDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.models.dtos.range_dtos.DecimalRangeDTO.convert_to_decimal
classmethod
¶
Convert input values to Decimal type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Decimal | str | None
|
The value to convert (None, string, or Decimal). |
required |
Returns:
| Type | Description |
|---|---|
Decimal | None
|
Decimal | None: The converted Decimal value or None. |
Raises:
| Type | Description |
|---|---|
InvalidArgumentError
|
If the value cannot be converted to Decimal. |
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.DecimalRangeDTO.validate_range ¶
Validate that from_ is less than or equal to to when both are provided.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The validated model instance. |
Raises:
| Type | Description |
|---|---|
OutOfRangeError
|
If from_ is greater than to. |
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.IntegerRangeDTO ¶
Bases: BaseRangeDTO[int]
Data Transfer Object for integer range queries.
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.IntegerRangeDTO.from_
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.IntegerRangeDTO.to
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.IntegerRangeDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.models.dtos.range_dtos.IntegerRangeDTO.validate_range ¶
Validate that from_ is less than or equal to to when both are provided.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The validated model instance. |
Raises:
| Type | Description |
|---|---|
OutOfRangeError
|
If from_ is greater than to. |
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.DateRangeDTO ¶
Bases: BaseRangeDTO[date]
Data Transfer Object for date range queries.
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.DateRangeDTO.from_
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.DateRangeDTO.to
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.DateRangeDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.models.dtos.range_dtos.DateRangeDTO.validate_range ¶
Validate that from_ is less than or equal to to when both are provided.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The validated model instance. |
Raises:
| Type | Description |
|---|---|
OutOfRangeError
|
If from_ is greater than to. |
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.DatetimeRangeDTO ¶
Bases: BaseRangeDTO[datetime]
Data Transfer Object for datetime range queries.
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.DatetimeRangeDTO.from_
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.DatetimeRangeDTO.to
class-attribute
instance-attribute
¶
archipy.models.dtos.range_dtos.DatetimeRangeDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.models.dtos.range_dtos.DatetimeRangeDTO.validate_range ¶
Validate that from_ is less than or equal to to when both are provided.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The validated model instance. |
Raises:
| Type | Description |
|---|---|
OutOfRangeError
|
If from_ is greater than to. |
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.DatetimeIntervalRangeDTO ¶
Bases: BaseRangeDTO[datetime]
Data Transfer Object for datetime range queries with interval.
Rejects requests if the number of intervals exceeds MAX_ITEMS or if interval-specific range size or 'to' age constraints are violated.
Source code in archipy/models/dtos/range_dtos.py
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
archipy.models.dtos.range_dtos.DatetimeIntervalRangeDTO.interval
instance-attribute
¶
archipy.models.dtos.range_dtos.DatetimeIntervalRangeDTO.MAX_ITEMS
class-attribute
¶
archipy.models.dtos.range_dtos.DatetimeIntervalRangeDTO.RANGE_SIZE_LIMITS
class-attribute
¶
RANGE_SIZE_LIMITS: dict[TimeIntervalUnitType, timedelta] = {
SECONDS: timedelta(days=2),
MINUTES: timedelta(days=7),
HOURS: timedelta(days=30),
DAYS: timedelta(days=365),
WEEKS: timedelta(days=365 * 2),
MONTHS: timedelta(days=365 * 5),
YEAR: timedelta(days=365 * 10),
}
archipy.models.dtos.range_dtos.DatetimeIntervalRangeDTO.TO_AGE_LIMITS
class-attribute
¶
TO_AGE_LIMITS: dict[TimeIntervalUnitType, timedelta] = {
SECONDS: timedelta(days=2),
MINUTES: timedelta(days=7),
HOURS: timedelta(days=30),
DAYS: timedelta(days=365 * 5),
WEEKS: timedelta(days=365 * 10),
MONTHS: timedelta(days=365 * 20),
YEAR: timedelta(days=365 * 50),
}
archipy.models.dtos.range_dtos.DatetimeIntervalRangeDTO.INTERVAL_TO_TIMEDELTA
class-attribute
¶
INTERVAL_TO_TIMEDELTA: dict[
TimeIntervalUnitType, timedelta
] = {
SECONDS: timedelta(seconds=1),
MINUTES: timedelta(minutes=1),
HOURS: timedelta(hours=1),
DAYS: timedelta(days=1),
WEEKS: timedelta(weeks=1),
MONTHS: timedelta(days=30),
YEAR: timedelta(days=365),
}
archipy.models.dtos.range_dtos.DatetimeIntervalRangeDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.models.dtos.range_dtos.DatetimeIntervalRangeDTO.validate_interval_constraints ¶
Validate interval based on range size, 'to' field age, and max intervals.
- Each interval has specific range size and 'to' age limits.
- Rejects if the number of intervals exceeds MAX_ITEMS.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The validated model instance. |
Raises:
| Type | Description |
|---|---|
OutOfRangeError
|
If interval constraints are violated or number of intervals > MAX_ITEMS. |
Source code in archipy/models/dtos/range_dtos.py
archipy.models.dtos.range_dtos.DatetimeIntervalRangeDTO.validate_range ¶
Validate that from_ is less than or equal to to when both are provided.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The validated model instance. |
Raises:
| Type | Description |
|---|---|
OutOfRangeError
|
If from_ is greater than to. |
Source code in archipy/models/dtos/range_dtos.py
options: show_root_toc_entry: false heading_level: 3
Email DTOs¶
DTOs for composing email messages including recipients, subject, body, and attachments.
archipy.models.dtos.email_dtos.EmailAttachmentDTO ¶
Bases: BaseDTO
Pydantic model for email attachments.
Source code in archipy/models/dtos/email_dtos.py
archipy.models.dtos.email_dtos.EmailAttachmentDTO.content
instance-attribute
¶
archipy.models.dtos.email_dtos.EmailAttachmentDTO.content_type
class-attribute
instance-attribute
¶
archipy.models.dtos.email_dtos.EmailAttachmentDTO.content_disposition
class-attribute
instance-attribute
¶
archipy.models.dtos.email_dtos.EmailAttachmentDTO.content_id
class-attribute
instance-attribute
¶
archipy.models.dtos.email_dtos.EmailAttachmentDTO.attachment_type
instance-attribute
¶
archipy.models.dtos.email_dtos.EmailAttachmentDTO.model_config
class-attribute
instance-attribute
¶
model_config = ConfigDict(
extra="ignore",
validate_default=True,
from_attributes=True,
frozen=True,
str_strip_whitespace=True,
arbitrary_types_allowed=True,
)
archipy.models.dtos.email_dtos.EmailAttachmentDTO.validate_attachment ¶
Validate and normalize attachment fields.
This validator performs three operations: 1. Sets content_type based on filename extension if not provided 2. Validates that attachment size does not exceed maximum allowed size 3. Ensures content_id is properly formatted with angle brackets
Returns:
| Type | Description |
|---|---|
Self
|
The validated model instance |
Raises:
| Type | Description |
|---|---|
ValueError
|
If attachment size exceeds maximum allowed size |
Source code in archipy/models/dtos/email_dtos.py
options: show_root_toc_entry: false heading_level: 3
FastAPI Exception Response DTO¶
DTO representing the standardized error response body returned by FastAPI exception handlers.
archipy.models.dtos.fastapi_exception_response_dto.FastAPIErrorResponseDTO ¶
Standardized error response model for OpenAPI documentation.
Source code in archipy/models/dtos/fastapi_exception_response_dto.py
archipy.models.dtos.fastapi_exception_response_dto.FastAPIErrorResponseDTO.status_code
instance-attribute
¶
archipy.models.dtos.fastapi_exception_response_dto.FastAPIErrorResponseDTO.model
instance-attribute
¶
model = {
"description": message_en,
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"error": {
"type": "string",
"example": code,
"description": "Error code identifier",
},
"detail": {
"type": "object",
"properties": detail_properties,
"required": [
"code",
"message_en",
"message_fa",
"http_status",
],
"additionalProperties": False,
"description": "Detailed error information",
},
},
}
}
},
}
archipy.models.dtos.fastapi_exception_response_dto.ValidationErrorResponseDTO ¶
Bases: FastAPIErrorResponseDTO
Specific response model for validation errors.
Source code in archipy/models/dtos/fastapi_exception_response_dto.py
archipy.models.dtos.fastapi_exception_response_dto.ValidationErrorResponseDTO.status_code
instance-attribute
¶
archipy.models.dtos.fastapi_exception_response_dto.ValidationErrorResponseDTO.model
instance-attribute
¶
model = {
"description": "Validation Error",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"error": {
"type": "string",
"example": "VALIDATION_ERROR",
"description": "Error code identifier",
},
"detail": {
"type": "array",
"items": {
"type": "object",
"properties": {
"field": {
"type": "string",
"example": "email",
"description": "Field name that failed validation",
},
"message": {
"type": "string",
"example": "Invalid email format",
"description": "Validation error message",
},
"value": {
"type": "string",
"example": "invalid@email",
"description": "Invalid value that caused the error",
},
},
},
"example": [
{
"field": "email",
"message": "Invalid email format",
"value": "invalid@email",
},
{
"field": "password",
"message": "Password must be at least 8 characters",
"value": "123",
},
],
},
},
}
}
},
}
options: show_root_toc_entry: false heading_level: 3