4
4
from typing import Any , Dict
5
5
6
6
from sqlalchemy import (
7
- Boolean , Column , DateTime , DDL , event , Float , ForeignKey , Index , Integer ,
8
- JSON , String , Time , UniqueConstraint )
7
+ Boolean ,
8
+ Column ,
9
+ DateTime ,
10
+ DDL ,
11
+ event ,
12
+ Float ,
13
+ ForeignKey ,
14
+ Index ,
15
+ Integer ,
16
+ JSON ,
17
+ String ,
18
+ Time ,
19
+ UniqueConstraint ,
20
+ )
9
21
from sqlalchemy .dialects .postgresql import TSVECTOR
10
22
from sqlalchemy .exc import IntegrityError , SQLAlchemyError
11
- from sqlalchemy .ext .declarative .api import declarative_base
23
+ from sqlalchemy .ext .declarative .api import declarative_base , DeclarativeMeta
12
24
from sqlalchemy .orm import relationship , Session
13
25
from sqlalchemy .sql .schema import CheckConstraint
14
26
15
27
from app .config import PSQL_ENVIRONMENT
16
28
from app .dependencies import logger
17
29
import app .routers .salary .config as SalaryConfig
18
30
19
- Base = declarative_base ()
31
+ Base : DeclarativeMeta = declarative_base ()
20
32
21
33
22
34
class User (Base ):
@@ -38,28 +50,36 @@ class User(Base):
38
50
availability = Column (Boolean , default = True , nullable = False )
39
51
40
52
owned_events = relationship (
41
- "Event" , cascade = "all, delete" , back_populates = "owner" ,
53
+ "Event" ,
54
+ cascade = "all, delete" ,
55
+ back_populates = "owner" ,
42
56
)
43
57
events = relationship (
44
- "UserEvent" , cascade = "all, delete" , back_populates = "participants" ,
58
+ "UserEvent" ,
59
+ cascade = "all, delete" ,
60
+ back_populates = "participants" ,
45
61
)
46
62
salary_settings = relationship (
47
- "SalarySettings" , cascade = "all, delete" , back_populates = "user" ,
63
+ "SalarySettings" ,
64
+ cascade = "all, delete" ,
65
+ back_populates = "user" ,
48
66
)
49
67
comments = relationship ("Comment" , back_populates = "user" )
50
68
51
69
oauth_credentials = relationship (
52
- "OAuthCredentials" , cascade = "all, delete" , back_populates = "owner" ,
53
- uselist = False )
70
+ "OAuthCredentials" ,
71
+ cascade = "all, delete" ,
72
+ back_populates = "owner" ,
73
+ uselist = False ,
74
+ )
54
75
55
76
def __repr__ (self ):
56
- return f' <User { self .id } >'
77
+ return f" <User { self .id } >"
57
78
58
79
@staticmethod
59
80
async def get_by_username (db : Session , username : str ) -> User :
60
81
"""query database for a user by unique username"""
61
- return db .query (User ).filter (
62
- User .username == username ).first ()
82
+ return db .query (User ).filter (User .username == username ).first ()
63
83
64
84
65
85
class Event (Base ):
@@ -70,7 +90,7 @@ class Event(Base):
70
90
start = Column (DateTime , nullable = False )
71
91
end = Column (DateTime , nullable = False )
72
92
content = Column (String )
73
- location = Column (String )
93
+ location = Column (String , nullable = True )
74
94
is_google_event = Column (Boolean , default = False )
75
95
vc_link = Column (String )
76
96
color = Column (String , nullable = True )
@@ -85,37 +105,35 @@ class Event(Base):
85
105
86
106
owner = relationship ("User" , back_populates = "owned_events" )
87
107
participants = relationship (
88
- "UserEvent" , cascade = "all, delete" , back_populates = "events" ,
108
+ "UserEvent" ,
109
+ cascade = "all, delete" ,
110
+ back_populates = "events" ,
89
111
)
90
112
comments = relationship ("Comment" , back_populates = "event" )
91
113
92
114
# PostgreSQL
93
115
if PSQL_ENVIRONMENT :
94
116
events_tsv = Column (TSVECTOR )
95
- __table_args__ = (Index (
96
- 'events_tsv_idx' ,
97
- 'events_tsv' ,
98
- postgresql_using = 'gin' ),
117
+ __table_args__ = (
118
+ Index ("events_tsv_idx" , "events_tsv" , postgresql_using = "gin" ),
99
119
)
100
120
101
121
def __repr__ (self ):
102
- return f' <Event { self .id } >'
122
+ return f" <Event { self .id } >"
103
123
104
124
105
125
class UserEvent (Base ):
106
126
__tablename__ = "user_event"
107
127
108
128
id = Column (Integer , primary_key = True , index = True )
109
- user_id = Column ('user_id' , Integer , ForeignKey (
110
- 'users.id' ), nullable = False )
111
- event_id = Column ('event_id' , Integer , ForeignKey (
112
- 'events.id' ), nullable = False )
129
+ user_id = Column ("user_id" , Integer , ForeignKey ("users.id" ))
130
+ event_id = Column ("event_id" , Integer , ForeignKey ("events.id" ))
113
131
114
132
events = relationship ("Event" , back_populates = "participants" )
115
133
participants = relationship ("User" , back_populates = "events" )
116
134
117
135
def __repr__ (self ):
118
- return f' <UserEvent ({ self .participants } , { self .events } )>'
136
+ return f" <UserEvent ({ self .participants } , { self .events } )>"
119
137
120
138
121
139
class Language (Base ):
@@ -128,17 +146,19 @@ class Language(Base):
128
146
class Category (Base ):
129
147
__tablename__ = "categories"
130
148
131
- __table_args__ = (
132
- UniqueConstraint ('user_id' , 'name' , 'color' ),
133
- )
149
+ __table_args__ = (UniqueConstraint ("user_id" , "name" , "color" ),)
134
150
id = Column (Integer , primary_key = True , index = True )
135
151
name = Column (String , nullable = False )
136
152
color = Column (String , nullable = False )
137
153
user_id = Column (Integer , ForeignKey ("users.id" ), nullable = False )
138
154
139
155
@staticmethod
140
- def create (db_session : Session , name : str , color : str ,
141
- user_id : int ) -> Category :
156
+ def create (
157
+ db_session : Session ,
158
+ name : str ,
159
+ color : str ,
160
+ user_id : int ,
161
+ ) -> Category :
142
162
try :
143
163
category = Category (name = name , color = color , user_id = user_id )
144
164
db_session .add (category )
@@ -155,7 +175,7 @@ def to_dict(self) -> Dict[str, Any]:
155
175
return {c .name : getattr (self , c .name ) for c in self .__table__ .columns }
156
176
157
177
def __repr__ (self ) -> str :
158
- return f' <Category { self .id } { self .name } { self .color } >'
178
+ return f" <Category { self .id } { self .name } { self .color } >"
159
179
160
180
161
181
class PSQLEnvironmentError (Exception ):
@@ -164,17 +184,19 @@ class PSQLEnvironmentError(Exception):
164
184
165
185
# PostgreSQL
166
186
if PSQL_ENVIRONMENT :
167
- trigger_snippet = DDL ("""
187
+ trigger_snippet = DDL (
188
+ """
168
189
CREATE TRIGGER ix_events_tsv_update BEFORE INSERT OR UPDATE
169
190
ON events
170
191
FOR EACH ROW EXECUTE PROCEDURE
171
192
tsvector_update_trigger(events_tsv,'pg_catalog.english','title','content')
172
- """ )
193
+ """ ,
194
+ )
173
195
174
196
event .listen (
175
197
Event .__table__ ,
176
- ' after_create' ,
177
- trigger_snippet .execute_if (dialect = ' postgresql' )
198
+ " after_create" ,
199
+ trigger_snippet .execute_if (dialect = " postgresql" ),
178
200
)
179
201
180
202
@@ -191,11 +213,7 @@ class Invitation(Base):
191
213
event = relationship ("Event" )
192
214
193
215
def __repr__ (self ):
194
- return (
195
- f'<Invitation '
196
- f'({ self .event .owner } '
197
- f'to { self .recipient } )>'
198
- )
216
+ return f"<Invitation " f"({ self .event .owner } " f"to { self .recipient } )>"
199
217
200
218
201
219
class OAuthCredentials (Base ):
@@ -220,60 +238,87 @@ class SalarySettings(Base):
220
238
__tablename__ = "salary_settings"
221
239
222
240
user_id = Column (
223
- Integer , ForeignKey ("users.id" ), primary_key = True ,
241
+ Integer ,
242
+ ForeignKey ("users.id" ),
243
+ primary_key = True ,
224
244
)
225
245
# category_id = Column(
226
246
# Integer, ForeignKey("categories.id"), primary_key=True,
227
247
# )
228
248
category_id = Column (
229
- Integer , primary_key = True ,
249
+ Integer ,
250
+ primary_key = True ,
230
251
)
231
252
wage = Column (
232
- Float , nullable = False , default = SalaryConfig .MINIMUM_WAGE ,
253
+ Float ,
254
+ nullable = False ,
255
+ default = SalaryConfig .MINIMUM_WAGE ,
233
256
)
234
257
off_day = Column (
235
- Integer , CheckConstraint ("0<=off_day<=6" ), nullable = False ,
258
+ Integer ,
259
+ CheckConstraint ("0<=off_day<=6" ),
260
+ nullable = False ,
236
261
default = SalaryConfig .SATURDAY ,
237
262
)
238
263
# holiday_category_id = Column(
239
264
# Integer, ForeignKey("holiday_categories.id"), nullable=False,
240
265
# default=SalaryConfig.ISRAELI_JEWISH,
241
266
# )
242
267
holiday_category_id = Column (
243
- Integer , nullable = False ,
268
+ Integer ,
269
+ nullable = False ,
244
270
default = SalaryConfig .ISRAELI_JEWISH ,
245
271
)
246
272
regular_hour_basis = Column (
247
- Float , nullable = False , default = SalaryConfig .REGULAR_HOUR_BASIS ,
273
+ Float ,
274
+ nullable = False ,
275
+ default = SalaryConfig .REGULAR_HOUR_BASIS ,
248
276
)
249
277
night_hour_basis = Column (
250
- Float , nullable = False , default = SalaryConfig .NIGHT_HOUR_BASIS ,
278
+ Float ,
279
+ nullable = False ,
280
+ default = SalaryConfig .NIGHT_HOUR_BASIS ,
251
281
)
252
282
night_start = Column (
253
- Time , nullable = False , default = SalaryConfig .NIGHT_START ,
283
+ Time ,
284
+ nullable = False ,
285
+ default = SalaryConfig .NIGHT_START ,
254
286
)
255
287
night_end = Column (
256
- Time , nullable = False , default = SalaryConfig .NIGHT_END ,
288
+ Time ,
289
+ nullable = False ,
290
+ default = SalaryConfig .NIGHT_END ,
257
291
)
258
292
night_min_len = Column (
259
- Time , nullable = False , default = SalaryConfig .NIGHT_MIN_LEN ,
293
+ Time ,
294
+ nullable = False ,
295
+ default = SalaryConfig .NIGHT_MIN_LEN ,
260
296
)
261
297
first_overtime_amount = Column (
262
- Float , nullable = False , default = SalaryConfig .FIRST_OVERTIME_AMOUNT ,
298
+ Float ,
299
+ nullable = False ,
300
+ default = SalaryConfig .FIRST_OVERTIME_AMOUNT ,
263
301
)
264
302
first_overtime_pay = Column (
265
- Float , nullable = False , default = SalaryConfig .FIRST_OVERTIME_PAY ,
303
+ Float ,
304
+ nullable = False ,
305
+ default = SalaryConfig .FIRST_OVERTIME_PAY ,
266
306
)
267
307
second_overtime_pay = Column (
268
- Float , nullable = False , default = SalaryConfig .SECOND_OVERTIME_PAY ,
308
+ Float ,
309
+ nullable = False ,
310
+ default = SalaryConfig .SECOND_OVERTIME_PAY ,
269
311
)
270
312
week_working_hours = Column (
271
- Float , nullable = False , default = SalaryConfig .WEEK_WORKING_HOURS ,
313
+ Float ,
314
+ nullable = False ,
315
+ default = SalaryConfig .WEEK_WORKING_HOURS ,
272
316
)
273
317
daily_transport = Column (
274
- Float , CheckConstraint (
275
- f"daily_transport<={ SalaryConfig .MAXIMUM_TRANSPORT } " ),
276
- nullable = False , default = SalaryConfig .STANDARD_TRANSPORT ,
318
+ Float ,
319
+ CheckConstraint (f"daily_transport<={ SalaryConfig .MAXIMUM_TRANSPORT } " ),
320
+ nullable = False ,
321
+ default = SalaryConfig .STANDARD_TRANSPORT ,
277
322
)
278
323
279
324
user = relationship ("User" , back_populates = "salary_settings" )
@@ -283,7 +328,7 @@ class SalarySettings(Base):
283
328
# back_populates="salary_settings")
284
329
285
330
def __repr__ (self ):
286
- return f' <SalarySettings ({ self .user_id } , { self .category_id } )>'
331
+ return f" <SalarySettings ({ self .user_id } , { self .category_id } )>"
287
332
288
333
289
334
class WikipediaEvents (Base ):
@@ -317,7 +362,7 @@ class Comment(Base):
317
362
event = relationship ("Event" , back_populates = "comments" )
318
363
319
364
def __repr__ (self ):
320
- return f' <Comment { self .id } >'
365
+ return f" <Comment { self .id } >"
321
366
322
367
323
368
class Zodiac (Base ):
@@ -332,10 +377,10 @@ class Zodiac(Base):
332
377
333
378
def __repr__ (self ):
334
379
return (
335
- f' <Zodiac '
336
- f' { self .name } '
337
- f' { self .start_day_in_month } /{ self .start_month } -'
338
- f' { self .end_day_in_month } /{ self .end_month } >'
380
+ f" <Zodiac "
381
+ f" { self .name } "
382
+ f" { self .start_day_in_month } /{ self .start_month } -"
383
+ f" { self .end_day_in_month } /{ self .end_month } >"
339
384
)
340
385
341
386
@@ -346,7 +391,9 @@ def __repr__(self):
346
391
def insert_data (target , session : Session , ** kw ):
347
392
session .execute (
348
393
target .insert (),
349
- {'id' : 1 , 'name' : 'English' }, {'id' : 2 , 'name' : 'עברית' })
394
+ {"id" : 1 , "name" : "English" },
395
+ {"id" : 2 , "name" : "עברית" },
396
+ )
350
397
351
398
352
- event .listen (Language .__table__ , ' after_create' , insert_data )
399
+ event .listen (Language .__table__ , " after_create" , insert_data )
0 commit comments