-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
214 lines (176 loc) · 8.75 KB
/
db.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
"""Database common classes and methods
"""
from __future__ import annotations
from datetime import datetime
from enum import Enum
from sqlalchemy import JSON, Boolean, DateTime
from sqlalchemy import Enum as DbEnum
from sqlalchemy import ForeignKey, Integer, String, Text, and_, or_, true
from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
from sqlalchemy.orm import Mapped, mapped_column, relationship
from .base import ApproveStatuses, BaseDbModel
class Credentials(BaseDbModel):
"""User credentials"""
id: Mapped[int] = mapped_column(Integer, primary_key=True)
group: Mapped[str] = mapped_column(String, nullable=False)
email: Mapped[str] = mapped_column(String, nullable=False)
scope: Mapped[JSON] = mapped_column(JSON, nullable=False)
token: Mapped[JSON] = mapped_column(JSON, nullable=False)
create_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow)
update_ts: Mapped[datetime] = mapped_column(
DateTime, nullable=False, default=datetime.utcnow, onupdate=datetime.utcnow
)
class Direction(str, Enum):
NORTH: str = "North"
SOUTH: str = "South"
class Room(BaseDbModel):
name: Mapped[str] = mapped_column(String, nullable=False, unique=True)
direction: Mapped[Direction] = mapped_column(DbEnum(Direction, native_enum=False), nullable=True)
building: Mapped[str] = mapped_column(String, nullable=True)
building_url: Mapped[str] = mapped_column(String, nullable=True)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
events: Mapped[list[Event]] = relationship(
"Event",
back_populates="room",
secondary="events_rooms",
secondaryjoin="and_(Event.id==EventsRooms.event_id, not_(Event.is_deleted))",
order_by="(Event.start_ts)",
)
class Lecturer(BaseDbModel):
first_name: Mapped[str] = mapped_column(String, nullable=False)
middle_name: Mapped[str] = mapped_column(String, nullable=False)
last_name: Mapped[str] = mapped_column(String, nullable=False)
avatar_id: Mapped[int] = mapped_column(Integer, ForeignKey("photo.id"), nullable=True)
description: Mapped[str] = mapped_column(Text, nullable=True)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
avatar: Mapped[Photo] = relationship(
"Photo",
foreign_keys="Lecturer.avatar_id",
backref="is_avatar_for",
primaryjoin="and_(Lecturer.avatar_id==Photo.id, not_(Photo.is_deleted))",
)
photos: Mapped[list[Photo]] = relationship(
"Photo",
back_populates="lecturer",
foreign_keys="Photo.lecturer_id",
order_by="Photo.id",
primaryjoin="and_(Lecturer.id==Photo.lecturer_id, not_(Photo.is_deleted), Photo.approve_status=='APPROVED')",
)
events: Mapped[list[Event]] = relationship(
"Event",
secondary="events_lecturers",
order_by="(Event.start_ts)",
back_populates="lecturer",
secondaryjoin="and_(Event.id==EventsLecturers.event_id, not_(Event.is_deleted))",
)
comments: Mapped[list[CommentLecturer]] = relationship(
"CommentLecturer",
back_populates="lecturer",
foreign_keys="CommentLecturer.lecturer_id",
primaryjoin="and_(Lecturer.id==CommentLecturer.lecturer_id, not_(CommentLecturer.is_deleted), CommentLecturer.approve_status=='APPROVED')",
)
@hybrid_method
def search(self, query: str) -> bool:
response = true
query = query.split(' ')
for q in query:
response = and_(
response, or_(self.first_name.contains(q), self.middle_name.contains(q), self.last_name.contains(q))
)
return response
@hybrid_property
def last_photo(self) -> Photo | None:
return self.photos[-1] if len(self.photos) else None
class Group(BaseDbModel):
name: Mapped[str] = mapped_column(String, nullable=False)
number: Mapped[str] = mapped_column(String, nullable=False, unique=True)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
events: Mapped[list[Event]] = relationship(
"Event",
order_by="(Event.start_ts)",
secondary="events_groups",
back_populates="group",
secondaryjoin="and_(Event.id==EventsGroups.event_id, not_(Event.is_deleted))",
)
class Event(BaseDbModel):
name: Mapped[str] = mapped_column(String, nullable=False)
start_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False)
end_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
room: Mapped[list[Room]] = relationship(
"Room",
back_populates="events",
secondary="events_rooms",
secondaryjoin="and_(Room.id==EventsRooms.room_id, not_(Room.is_deleted))",
)
group: Mapped[list[Group]] = relationship(
"Group",
back_populates="events",
secondary="events_groups",
secondaryjoin="and_(Group.id==EventsGroups.group_id, not_(Group.is_deleted))",
)
lecturer: Mapped[list[Lecturer]] = relationship(
"Lecturer",
back_populates="events",
secondary="events_lecturers",
secondaryjoin="and_(Lecturer.id==EventsLecturers.lecturer_id, not_(Lecturer.is_deleted))",
)
comments: Mapped[list[CommentEvent]] = relationship(
"CommentEvent",
foreign_keys="CommentEvent.event_id",
back_populates="event",
primaryjoin="and_(Event.id==CommentEvent.event_id, not_(CommentEvent.is_deleted), CommentEvent.approve_status=='APPROVED')",
)
class EventsLecturers(BaseDbModel):
event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id"), nullable=False)
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"), nullable=False)
class EventsRooms(BaseDbModel):
event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id"), nullable=False)
room_id: Mapped[int] = mapped_column(Integer, ForeignKey("room.id"), nullable=False)
class EventsGroups(BaseDbModel):
event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id"), nullable=False)
group_id: Mapped[int] = mapped_column(Integer, ForeignKey("group.id"), nullable=False)
class Photo(BaseDbModel):
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"), nullable=False)
link: Mapped[str] = mapped_column(String, unique=True, nullable=False)
approve_status: Mapped[ApproveStatuses] = mapped_column(DbEnum(ApproveStatuses, native_enum=False), nullable=False)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
lecturer: Mapped[Lecturer] = relationship(
"Lecturer",
back_populates="photos",
foreign_keys="Photo.lecturer_id",
order_by="Lecturer.id",
primaryjoin="and_(Lecturer.id==Photo.lecturer_id, not_(Lecturer.is_deleted))",
)
class CommentLecturer(BaseDbModel):
lecturer_id: Mapped[int] = mapped_column(Integer, ForeignKey("lecturer.id"), nullable=False)
author_name: Mapped[str] = mapped_column(String, nullable=False)
text: Mapped[str] = mapped_column(String, nullable=False)
approve_status: Mapped[ApproveStatuses] = mapped_column(DbEnum(ApproveStatuses, native_enum=False), nullable=False)
create_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow())
update_ts: Mapped[datetime] = mapped_column(
DateTime, nullable=False, default=datetime.utcnow(), onupdate=datetime.utcnow()
)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
lecturer: Mapped[Lecturer] = relationship(
"Lecturer",
back_populates="comments",
foreign_keys="CommentLecturer.lecturer_id",
primaryjoin="and_(Lecturer.id==CommentLecturer.lecturer_id, not_(Lecturer.is_deleted))",
)
class CommentEvent(BaseDbModel):
event_id: Mapped[int] = mapped_column(Integer, ForeignKey("event.id"), nullable=False)
author_name: Mapped[str] = mapped_column(String, nullable=False)
text: Mapped[str] = mapped_column(String, nullable=False)
approve_status: Mapped[ApproveStatuses] = mapped_column(DbEnum(ApproveStatuses, native_enum=False), nullable=False)
create_ts: Mapped[datetime] = mapped_column(DateTime, nullable=False, default=datetime.utcnow())
update_ts: Mapped[datetime] = mapped_column(
DateTime, nullable=False, default=datetime.utcnow(), onupdate=datetime.utcnow()
)
is_deleted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False)
event: Mapped[Event] = relationship(
"Event",
back_populates="comments",
foreign_keys="CommentEvent.event_id",
primaryjoin="and_(Event.id==CommentEvent.event_id, not_(Event.is_deleted))",
)