1
1
from datetime import datetime
2
2
from typing import List , Optional
3
3
4
+ from pydantic import root_validator
4
5
from sqlmodel import Column , DateTime , Field , Relationship , SQLModel
5
6
6
- from ._custom_types import PydanticTimezone , SATimezone
7
+ from ._custom_types import (
8
+ EXTENDED_JSON_ENCODERS ,
9
+ PydanticTimezone ,
10
+ SATimezone ,
11
+ create_timestamp_validator ,
12
+ tz_timestamp_reader ,
13
+ )
7
14
15
+ #
16
+ # Payment
17
+ #
8
18
9
- class Payment (SQLModel , table = True ):
10
- __tablename__ = "payment"
11
- id : Optional [int ] = Field (primary_key = True , nullable = False )
12
- total : float
19
+
20
+ class PaymentBase (SQLModel ):
13
21
timestamp : datetime = Field (
14
- sa_column = Column (DateTime (timezone = True )), nullable = False
22
+ sa_column = Column (DateTime (timezone = True ), nullable = False ),
23
+ nullable = False ,
24
+ title = "Local timestamp, or timezone-aware timestamp" ,
25
+ )
26
+ timezone : PydanticTimezone = Field (
27
+ sa_column = Column (SATimezone (), nullable = False ), nullable = False
15
28
)
16
- timezone : PydanticTimezone = Field (sa_column = Column (SATimezone ()), nullable = False )
17
29
description : str
30
+
31
+
32
+ class Payment (PaymentBase , table = True ):
33
+ __tablename__ = "payment"
34
+ id : Optional [int ] = Field (primary_key = True , nullable = False )
35
+ total : float
18
36
transactions : List ["Transaction" ] = Relationship (back_populates = "payment" )
37
+ entries : List ["PaymentEntry" ] = Relationship (back_populates = "payment" )
38
+
39
+
40
+ class PaymentCreate (PaymentBase ):
41
+ total : Optional [float ]
42
+
43
+ @root_validator
44
+ def verify_timezone (cls , values ):
45
+ return create_timestamp_validator (values )
46
+
47
+
48
+ class PaymentRead (PaymentBase ):
49
+ id : int
50
+ total : float
51
+
52
+ @root_validator
53
+ def convert_timezone (cls , values ):
54
+ return tz_timestamp_reader (values )
55
+
56
+ class Config :
57
+ json_encoders = EXTENDED_JSON_ENCODERS
58
+
59
+
60
+ #
61
+ # PaymentEntry
62
+ #
19
63
20
64
21
65
class PaymentEntryBase (SQLModel ):
@@ -24,14 +68,14 @@ class PaymentEntryBase(SQLModel):
24
68
amount : float
25
69
quantity : int
26
70
description : str
27
- payment : Payment = Relationship (back_populates = "entries" )
28
- category : "Category" = Relationship (back_populates = "entries" )
29
71
30
72
31
73
class PaymentEntry (PaymentEntryBase , table = True ):
32
74
__tablename__ = "payment_entry"
33
75
id : Optional [int ] = Field (primary_key = True , nullable = False )
34
76
payment_id : int = Field (foreign_key = "payment.id" , nullable = False )
77
+ payment : Payment = Relationship (back_populates = "entries" )
78
+ category : "Category" = Relationship (back_populates = "entries" )
35
79
36
80
37
81
class PaymentEntryCreate (PaymentEntryBase ):
@@ -43,19 +87,35 @@ class PaymentEntryRead(PaymentEntryBase):
43
87
payment_id : int
44
88
45
89
90
+ #
91
+ # Transaction
92
+ #
93
+
94
+
46
95
class Transaction (SQLModel , table = True ):
47
96
48
97
__tablename__ = "transaction"
49
98
account_id : int = Field (primary_key = True , foreign_key = "account.id" , nullable = False )
50
99
payment_id : int = Field (primary_key = True , foreign_key = "payment.id" , nullable = False )
51
100
amount : float
52
101
timestamp : datetime = Field (
53
- sa_column = Column (DateTime (timezone = True )), nullable = False
102
+ sa_column = Column (DateTime (timezone = True ), nullable = False ), nullable = False
103
+ )
104
+ timezone : PydanticTimezone = Field (
105
+ sa_column = Column (SATimezone (), nullable = False ), nullable = False
54
106
)
55
- timezone : PydanticTimezone = Field (sa_column = Column (SATimezone ()), nullable = False )
56
107
account : "Account" = Relationship (back_populates = "transactions" )
57
108
payment : Payment = Relationship (back_populates = "transactions" )
58
109
59
110
111
+ #
112
+ # Relationship Models
113
+ #
114
+
115
+
116
+ class PaymentReadWithEntries (PaymentRead ):
117
+ entries : List [PaymentEntryRead ] = []
118
+
119
+
60
120
from .account import Account
61
121
from .category import Category
0 commit comments