-
Notifications
You must be signed in to change notification settings - Fork 0
/
db_interactions.py
281 lines (223 loc) · 7.41 KB
/
db_interactions.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
from typing import Optional, Literal
from db_definitions import Topic, Thread, Post, User, Base, DBSession
import schema
from humps import camelize
from datetime import datetime, timedelta
import os
from utils import make_password, make_token
from sqlalchemy.orm import Session
ROOT_PASSWORD = os.environ['ROOT_PASSWORD'] if 'ROOT_PASSWORD' in os.environ else 'toor'
def init_db():
session = DBSession()
engine = session.get_bind()
Base.metadata.create_all(engine)
root_exists = session.query(User).filter(User.id == 0).count()
if not root_exists:
hash, salt = make_password(ROOT_PASSWORD)
root_user = User(
id=0,
name='root',
password_hash=hash,
password_salt=salt
)
session.add(root_user)
home_exists = session.query(Topic).filter(Topic.id == 0).count()
if not home_exists:
home_topic = Topic(
id=0,
title='Home',
user_id=0
)
session.add(home_topic)
session.commit()
def get_topic(session: Session, id: int) -> Optional[schema.Topic]:
topic = session.query(Topic).get(id)
if topic is None:
return None
path = get_topic_path(topic)
title = topic.title
topics = (
schema.TopicData(
**camelize(
schema.DBTopic.from_orm(sub_topic).dict()
),
link=sub_topic.id,
numTopics=len(sub_topic.children_topics),
numThreads=len(sub_topic.children_threads),
user=schema.User.from_orm(sub_topic.user)
)
for sub_topic in topic.children_topics
)
threads = (
schema.ThreadData(
**camelize(
schema.DBThread.from_orm(sub_thread).dict()
),
link=sub_thread.id,
numPosts=len(sub_thread.children_posts),
user=schema.User.from_orm(sub_thread.user)
)
for sub_thread in topic.children_threads
)
return schema.Topic(
title=title,
path=path,
topics=topics,
threads=threads,
user=schema.User.from_orm(topic.user)
)
def get_thread(session: Session, id: int) -> Optional[schema.Thread]:
thread = session.query(Thread).get(id)
if thread is None:
return None
title = thread.title
path = get_thread_path(thread)
posts = (
schema.PostData(
**camelize(
schema.DBPost.from_orm(post).dict()
),
user=schema.User.from_orm(post.user)
)
for post in thread.children_posts
)
return schema.Thread(
title=title,
path=path,
posts=posts
)
def get_thread_posts(session, id):
thread = get_thread(session, id)
return thread.children_posts if thread is not None else None
def get_topic_path(topic: Topic) -> schema.Path:
path = [topic]
def recur_add_parent(topic):
nonlocal path
parent = topic.parent
if parent is not None:
path.append(parent)
recur_add_parent(parent)
recur_add_parent(topic)
return [
schema.PathElement(
id=topic.id,
title=topic.title
)
for topic in reversed(path)
]
def get_thread_path(thread):
parent = thread.parent
if parent is not None:
return get_topic_path(parent)
else:
return []
# def get_user_by_id(session: Session, id: int) -> Optional[schema.User]:
# user = session.query(User).get(id)
# if user is None:
# return None
# db_user = schema.DBUser.from_orm(user)
# return schema.User(**db_user.dict())
def get_user_by_name(session: Session, username: str) -> schema.DBUser:
user = session.query(User).filter(User.name == username).one()
db_user = schema.DBUser.from_orm(user)
return db_user
def get_user_by_token(session: Session, token: str) -> Optional[schema.User]:
users = session.query(User).filter(User.token == token).all()
if len(users) != 1:
return None
user = users[0]
db_user = schema.DBUser.from_orm(user)
if datetime.now() > db_user.token_expires_at:
return None
return schema.User(**db_user.dict())
def set_user_token(session: Session, username: str) -> str:
token = make_token()
expires_at = datetime.now() + timedelta(days=1)
session.query(User).filter(User.name == username)\
.update({
User.token: token,
User.token_expires_at: expires_at
})
session.commit()
return token
def add_user(session: Session, username: str, password: str):
hash, salt = make_password(password)
user = User(
name=username,
password_hash=hash,
password_salt=salt
)
session.add(user)
session.commit()
def change_user_password(session: Session, user_id: int, password: str):
user: User = session.query(User).get(user_id)
hash, salt = make_password(password)
user.password_hash = hash
user.password_salt = salt
session.commit()
def user_exists(session: Session, username: str):
count = session.query(User).filter(User.name == username).count()
return count == 1
def thread_exists(session: Session, thread_id: int):
count = session.query(Thread).filter(Thread.id == thread_id).count()
return count == 1
def add_post(session: Session, thread_id: int, user: schema.User, text: str):
post = Post(
user_id=user.id,
text=text,
parent_id=thread_id
)
session.add(post)
session.commit()
DB_SCHEMA = {
Post: schema.DBPost,
Thread: schema.DBThread,
Topic: schema.DBTopic
}
def row_belongs_to_user(
session: Session, row_id: int,
user_id: int, table: Literal[Post, Thread, Topic]
):
row: Optional[table] = session.query(table).get(row_id)
if row is None:
return False
db_row = DB_SCHEMA[table].from_orm(row)
return db_row.user_id == user_id
def remove_row(session: Session, row_id: int, table: Literal[Post, Thread, Topic]):
row = session.query(table).get(row_id)
session.delete(row)
session.commit()
def post_belongs_to_user(session: Session, post_id: int, user_id: int) -> bool:
return row_belongs_to_user(session, post_id, user_id, Post)
def remove_post(session: Session, post_id: int):
post = session.query(Post).get(post_id)
session.delete(post)
session.commit()
def topic_exists(session: Session, topic_id: int) -> bool:
count = session.query(Topic).filter(Topic.id == topic_id).count()
return count == 1
def add_topic(session: Session, title: str, parent_id: int, user_id: int):
topic = Topic(
title=title,
parent_id=parent_id,
user_id=user_id
)
session.add(topic)
session.commit()
def add_thread(session: Session, title: str, is_vegan: bool, parent_id: int, user_id: int):
thread = Thread(
title=title,
is_vegan=is_vegan,
parent_id=parent_id,
user_id=user_id
)
session.add(thread)
session.commit()
def remove_topic(session: Session, topic_id: int):
remove_row(session, topic_id, Topic)
def remove_thread(session: Session, thread_id: int):
remove_row(session, thread_id, Thread)
def thread_belongs_to_user(session: Session, thread_id: int, user_id: int):
return row_belongs_to_user(session, thread_id, user_id, Thread)
def topic_belongs_to_user(session: Session, topic_id: int, user_id: int):
return row_belongs_to_user(session, topic_id, user_id, Topic)