1
1
from datetime import date
2
- from typing import Any , Iterator , Dict
2
+ from pathlib import Path
3
+ from typing import Any , Iterator , Dict , TypeVar
3
4
4
5
from faker import Faker
5
6
from fastapi import Depends , FastAPI , Request
8
9
from sqlmodel import Field , Session , SQLModel , create_engine , select
9
10
10
11
from fastapi_pagination import pagination_ctx , resolve_params
11
- from fastapi_pagination .cursor import CursorPage
12
+ from fastapi_pagination .cursor import CursorPage as BaseCursorPage
13
+ from fastapi_pagination .customization import CustomizedPage , UseIncludeTotal , UseParamsFields
12
14
from fastapi_pagination .ext .sqlmodel import paginate
13
15
16
+ ROOT = Path (__file__ ).parent
17
+
14
18
fake = Faker ()
15
19
16
20
app = FastAPI ()
17
- templates = Jinja2Templates (directory = "templates" )
21
+ templates = Jinja2Templates (directory = ROOT / "templates" )
18
22
19
23
engine = create_engine ("sqlite:///.db" )
20
24
21
- CursorPage = CursorPage .with_custom_options (size = 10 )
25
+
26
+ T = TypeVar ("T" )
27
+
28
+ CursorPage = CustomizedPage [
29
+ BaseCursorPage [T ],
30
+ UseIncludeTotal (True ),
31
+ UseParamsFields (size = 10 ),
32
+ ]
22
33
23
34
24
35
class User (SQLModel , table = True ):
@@ -40,7 +51,7 @@ def get_db() -> Iterator[Session]:
40
51
def user_data (id_ : int ) -> Dict [str , Any ]:
41
52
return {
42
53
"id" : id_ + 1 ,
43
- "profile_pic" : f"https://avatars .dicebear.com/api /croodles/{ id_ } .svg " ,
54
+ "profile_pic" : f"https://api .dicebear.com/8.x /croodles/svg?seed= { id_ + 1 } " ,
44
55
"first_name" : fake .first_name (),
45
56
"last_name" : fake .last_name (),
46
57
"email" : fake .email (),
@@ -53,8 +64,10 @@ def on_startup():
53
64
User .metadata .drop_all (engine )
54
65
User .metadata .create_all (engine )
55
66
67
+ total = fake .pyint (100 , 200 )
68
+
56
69
with Session (engine ) as session :
57
- session .add_all ([User (** user_data (i )) for i in range (100 )])
70
+ session .add_all ([User (** user_data (i )) for i in range (total )])
58
71
session .commit ()
59
72
60
73
0 commit comments