/
post.py
388 lines (302 loc) · 9.36 KB
/
post.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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
import database
import util
import datetime
db = database.gains_db
def create_post(username, message, likes=0, post_tags="", photo_path=None, video_path=None):
"""
Create a post
:param photo_path:
:param video_path:
:param username:
:param message:
:param post_tags:
:param likes:
:return:
"""
cursor = db.cursor()
post_id = util.gen_id()
date = datetime.datetime.now()
post_sql = "INSERT INTO post (post_id, message, post_tags, likes, username_id, photo_path, video_path, post_date) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)"
values = (post_id, message, post_tags, likes, username, photo_path, video_path, date)
# Check tags are valid
post_tags = post_tags[1:] if post_tags.startswith(",") else post_tags
post_tags = post_tags[:-1] if post_tags.endswith(",") else post_tags
try:
cursor.execute(post_sql, values)
db.commit()
except:
print("Unable to create post")
raise Exception
return post_id
def remove_post(post_id):
"""
Remove a post
:param post_id:
:return:
"""
cursor = db.cursor()
remove_sql = "DELETE FROM post WHERE post_id = (%s)"
try:
cursor.execute(remove_sql, (post_id,))
except:
print("Unable to remove post")
raise Exception
finally:
db.commit()
def get_post(post_id):
"""
Return information about a post
:param post_id:
:return:
"""
cursor = db.cursor()
post_sql = "SELECT username_id, message, post_tags, likes, post_date, photo_path, video_path " \
"FROM post " \
"WHERE post_id = (%s)"
result = ()
try:
cursor.execute(post_sql, (post_id,))
result = cursor.fetchone()
except:
print("Unable to get post")
raise Exception
finally:
db.commit()
return_post = {
"username": None,
"message": None,
"post_tags": None,
"likes": None,
"post_date": None,
"photo_path": None,
"video_path": None
}
index = 0
for key in return_post:
return_post[key] = result[index]
index += 1
return return_post
def update_post(
post_id,
post_tags=None,
likes=None
):
"""
Update a posts likes and tags. Result of post NOT editable
:param post_id:
:param post_tags:
:param likes:
:return:
"""
cursor = db.cursor()
try:
if post_tags is not None:
tags_sql = "UPDATE post SET post_tags = %s WHERE post_id = %s"
cursor.execute(tags_sql, (post_tags, post_id))
if likes is not None:
likes_sql = "UPDATE post SET likes = %s WHERE post_id = %s"
cursor.execute(likes_sql, (likes, post_id))
except:
print("Unable to update post")
raise Exception
finally:
db.commit()
return post_id
def get_post_user(username):
"""
Get all posts associated with a user
:param username:
:return:
"""
all_user_data = ()
try:
cursor = db.cursor()
all_user_sql = "SELECT post.post_id, post.username_id, post.message, post.post_tags, post.likes, post.post_date, post.photo_path, post.video_path, user.name, user.avatar_path " \
"FROM post INNER JOIN user " \
"ON post.username_id=user.username " \
"WHERE username_id = (%s)"
cursor.execute(all_user_sql, (username,))
posts = cursor.fetchall()
# format comments to (list of) dict
return_posts = util.format_posts(posts)
return return_posts
except:
print("Unable to get all posts")
raise Exception
finally:
db.commit()
return all_user_data
def get_post_tag_name(
tags=None,
name=None):
"""
Get posts associated with tags or name
:param name:
:param tags:
:return:
"""
# convert tags into list
valid_posts = set()
try:
cursor = db.cursor()
if tags is not None:
tags = tags.split(",")
for tag in tags:
tag_sql = "SELECT post.post_id, post.username_id, post.message, post.post_tags, post.likes, post.post_date, post.photo_path, post.video_path, user.name, user.avatar_path " \
"FROM post INNER JOIN user ON post.username_id=user.username " \
"WHERE post_tags LIKE CONCAT('%', %s,'%')"
cursor.execute(tag_sql, (tag,))
tag_posts = cursor.fetchall()
# Add valid posts into valid_posts
for tag_post in tag_posts:
if tag_post is not None:
valid_posts.add(tag_post)
if name is not None:
name_sql = "SELECT post.post_id, post.username_id, post.message, post.post_tags, post.likes, post.post_date, post.photo_path, post.video_path, user.name, user.avatar_path " \
"FROM post INNER JOIN user ON post.username_id=user.username " \
"WHERE user.name LIKE CONCAT('%', %s,'%')"
cursor.execute(name_sql, (name,))
name_posts = cursor.fetchall()
for name_post in name_posts:
if name_post is not None:
valid_posts.add(name_post)
except:
print("Unable to get posts associated with tags / name")
raise Exception
finally:
db.commit()
return util.format_posts(list(valid_posts))
def get_posts():
"""
Get all posts
:return:
"""
post_sql = "SELECT post.post_id, post.username_id, post.message, post.post_tags, post.likes, post.post_date, post.photo_path, post.video_path, user.name, user.avatar_path " \
"FROM post INNER JOIN user " \
"ON post.username_id=user.username"
valid_posts = set()
try:
cursor = db.cursor()
cursor.execute(post_sql)
posts = cursor.fetchall()
# Add valid posts into valid_posts
for post in posts:
if post is not None:
valid_posts.add(post)
return util.format_posts(list(valid_posts))
except:
print("Unable to get all posts")
raise Exception
finally:
db.commit()
def add_comment(
post_id,
message,
username):
"""
Add a comment to a post
:param post_id:
:param message:
:return:
"""
try:
cursor = db.cursor()
comment_id = util.gen_id()
comment_sql = "INSERT INTO comment (comment_id, message, post_id, username) VALUES (%s, %s, %s, %s)"
values = (comment_id, message, post_id, username)
cursor.execute(comment_sql, values)
except:
print("Unable to add comment")
raise Exception
finally:
db.commit()
return comment_id
def remove_comment(comment_id):
"""
Remove a comment
:param comment_id:
:return:
"""
cursor = db.cursor()
remove_sql = "DELETE FROM comment WHERE comment_id = (%s)"
try:
cursor.execute(remove_sql, (comment_id,))
except:
print("Unable to remove comment")
raise Exception
finally:
db.commit()
return comment_id
def get_comments(post_id):
"""
Retrieve comments associated with particular comment
:param post_id:
:return:
"""
try:
cursor = db.cursor()
comment_sql = "SELECT comment_id, message, post_id, username " \
"FROM comment WHERE comment.post_id=(%s)"
cursor.execute(comment_sql, (post_id,))
all_comments = cursor.fetchall()
# format comments to (list of) dict
return_comments = list()
template_comment = {
"comment_id": None,
"message": None,
"post_id": None,
"username": None
}
for comment in all_comments:
index = 0
for key in template_comment:
template_comment[key] = comment[index]
index += 1
return_comments.append(template_comment)
return return_comments
except:
print("Unable to get comments")
raise Exception
finally:
db.commit()
def get_comment(comment_id):
"""
Retrieve comment associated with particular comment_id
:param post_id:
:return:
"""
try:
cursor = db.cursor()
comment_sql = "SELECT comment_id, message, post_id, username " \
"FROM comment WHERE comment.comment_id=(%s)"
cursor.execute(comment_sql, (comment_id,))
comment = cursor.fetchone()
template_comment = {
"comment_id": None,
"message": None,
"post_id": None,
"username": None
}
index = 0
for key in template_comment:
template_comment[key] = comment[index]
index += 1
return template_comment
except:
print("Unable to get comments")
raise Exception
finally:
db.commit()
def remove_post_comments(post_id):
"""
Delete all comments associated with post
:param post_id:
:return:
"""
try:
cursor = db.cursor()
comment_sql = "DELETE FROM comment WHERE post_id=(%s)"
cursor.execute(comment_sql, (post_id,))
except:
print("Unable to remove posts associated with " + post_id)
raise Exception