1- from datetime import date , datetime
2- from decimal import Decimal
31from enum import Enum
4- from typing import ClassVar , Generic , Self , TypeVar
2+ from typing import TypeVar
53
6- from pydantic import BaseModel , ConfigDict , Field , PositiveInt , StrictInt , field_validator , model_validator
7-
8- from archipy .models .types import SortOrderType
4+ from pydantic import BaseModel , ConfigDict
95
106# Generic types
117T = TypeVar ('T' , bound = Enum )
@@ -20,87 +16,3 @@ class BaseDTO(BaseModel):
2016 str_strip_whitespace = True ,
2117 arbitrary_types_allowed = True ,
2218 )
23-
24-
25- class SortDTO (BaseModel , Generic [T ]):
26- column : T | str
27- order : SortOrderType = SortOrderType .DESCENDING
28-
29- @classmethod
30- def default (cls ) -> Self :
31- return cls (column = "created_at" , order = SortOrderType .DESCENDING )
32-
33-
34- class PaginationDTO (BaseDTO ):
35- page : PositiveInt = Field (default = 1 , ge = 1 )
36- page_size : PositiveInt = Field (default = 10 , le = 100 )
37-
38- MAX_ITEMS : ClassVar = 10000
39-
40- @model_validator (mode = "after" )
41- def validate_pagination (cls , model : Self ) -> Self :
42- total_items = model .page * model .page_size
43- if total_items > cls .MAX_ITEMS :
44- raise ValueError (
45- f"Pagination limit exceeded. "
46- f"Requested { total_items } items, but the maximum is { cls .MAX_ITEMS } . "
47- f"Try reducing page size or requesting a lower page number." ,
48- )
49- return model
50-
51-
52- class SearchInputDTO (BaseModel , Generic [T ]):
53- pagination : PaginationDTO | None = None
54- sort_info : SortDTO [T ] | None = None
55-
56-
57- class RangeDTO (BaseDTO ):
58- from_ : Decimal | None = None
59- to : Decimal | None = None
60-
61- @field_validator ("from_" , "to" , mode = "before" )
62- def convert_to (cls , value : Decimal | str | None ) -> Decimal | None :
63- if value is None :
64- return value
65- if not (isinstance (value , Decimal | str )):
66- raise ValueError ("Decimal input should be str or decimal." )
67- return Decimal (value )
68-
69- @model_validator (mode = "after" )
70- def validate_range (cls , model : Self ) -> Self :
71- if model .from_ and model .to and model .from_ >= model .to :
72- raise ValueError ("from_ can`t be bigger than to" )
73- return model
74-
75-
76- class IntegerRangeDTO (BaseDTO ):
77- from_ : StrictInt | None = None
78- to : StrictInt | None = None
79-
80- @model_validator (mode = "after" )
81- def validate_range (cls , model : Self ) -> Self :
82- if model .from_ and model .to and model .from_ > model .to :
83- raise ValueError ("from_ can`t be bigger than to" )
84- return model
85-
86-
87- class DateRangeDTO (BaseDTO ):
88- from_ : date | None = None
89- to : date | None = None
90-
91- @model_validator (mode = "after" )
92- def validate_range (cls , model : Self ) -> Self :
93- if model .from_ and model .to and model .from_ > model .to :
94- raise ValueError ("from_ can`t be bigger than to" )
95- return model
96-
97-
98- class DatetimeRangeDTO (BaseDTO ):
99- from_ : datetime | None = None
100- to : datetime | None = None
101-
102- @model_validator (mode = "after" )
103- def validate_range (cls , model : Self ) -> Self :
104- if model .from_ and model .to and model .from_ > model .to :
105- raise ValueError ("from_ can`t be bigger than to" )
106- return model
0 commit comments