forked from CodecoolBase/ask-mate-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_handler.py
More file actions
625 lines (496 loc) · 21 KB
/
data_handler.py
File metadata and controls
625 lines (496 loc) · 21 KB
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
import bcrypt as bcrypt
from psycopg2 import sql
import connection
from datetime import datetime
@connection.connection_handler
def get_latest_five_questions(cursor):
cursor.execute("""
SELECT *
FROM question
ORDER BY submission_time DESC
""")
question = cursor.fetchmany(5)
return question
@connection.connection_handler
def get_all_details(cursor):
cursor.execute("""SELECT * FROM question
;""")
questions = cursor.fetchall()
return questions
def date_time():
dt = datetime.now()
date = dt.strftime('%Y-%m-%d %H:%M:%S')
return date
@connection.connection_handler
def edit_answer_row(cursor, new_data, id):
cursor.execute("""UPDATE answer
SET submission_time= %(submission_time)s,
message = %(message)s,
image = %(image)s
WHERE id = %(id)s """
, {'id': id, 'submission_time': new_data['submission_time'],
'message': new_data['message'], 'image': new_data['image']})
@connection.connection_handler
def edit_question_row(cursor, new_data, id):
cursor.execute("""UPDATE question
SET submission_time= %(submission_time)s,
view_number= view_number,
vote_number= vote_number,
title= %(title)s,
message= %(message)s,
image= %(image)s
WHERE id= %(id)s"""
, {'id': id,
'submission_time': new_data['submission_time'],
'view_number': new_data['view_number'],
'vote_number': new_data['vote_number'],
'title': new_data['title'],
'message': new_data['message'],
'image': new_data['image']})
@connection.connection_handler
def add_new_question(cursor, new_data):
cursor.execute("""INSERT INTO question (submission_time, view_number, vote_number, title, message, image, userid)
VALUES (%(submission_time)s,%(view_number)s,%(vote_number)s,%(title)s,%(message)s,%(image)s,%(userid)s)"""
, {'submission_time': new_data['submission_time'],
'view_number': new_data['view_number'],
'vote_number': new_data['vote_number'],
'title': new_data['title'],
'message': new_data['message'],
'image': new_data['image'],
'userid': new_data['userid']})
@connection.connection_handler
def add_new_answer(cursor, new_data):
cursor.execute("""INSERT INTO answer (submission_time,question_id,vote_number, message,image,userid)
VALUES (%(submission_time)s,%(question_id)s,%(vote_number)s,%(message)s,%(image)s,%(userid)s)"""
, {'submission_time': new_data['submission_time'], 'question_id': new_data['question_id'],
'vote_number': new_data['vote_number'],
'message': new_data['message'], 'image': new_data['image'], 'userid': new_data['userid']})
@connection.connection_handler
def get_question_for_id(cursor, id):
cursor.execute("""
SELECT * FROM question
WHERE id = %(id)s """,
{'id': id})
question = cursor.fetchall()
return question[0]
@connection.connection_handler
def get_answers_id_for_edit(cursor, id):
cursor.execute("""
SELECT * FROM answer
WHERE id = %(id)s""",
{'id': id})
answers = cursor.fetchall()
return answers
@connection.connection_handler
def get_answers_for_id(cursor, id):
cursor.execute("""
SELECT * FROM answer
JOIN user_info on answer.userid = user_info.user_id
WHERE answer.question_id = %(id)s
ORDER BY submission_time""",
{'id': id})
answers = cursor.fetchall()
return answers
@connection.connection_handler
def get_answers_for_answer_id(cursor, id):
cursor.execute("""
SELECT * FROM answer
WHERE id = %(id)s""",
{'id': id})
answers = cursor.fetchall()
return answers
@connection.connection_handler
def get_question_id_for_answer_id(cursor, answer_id):
cursor.execute("""
SELECT question_id FROM answer
WHERE id = %(answer_id)s""",
{'answer_id': int(answer_id)})
question_id = cursor.fetchall()
return question_id[0].get('question_id')
@connection.connection_handler
def get_all_comments_for_answer(cursor, question_id):
answer_ids = get_all_answer_id_to_delete_comments(question_id)
if len(answer_ids) > 0:
cursor.execute("""
DELETE FROM comment
WHERE answer_id IN %(id)s """,
{'id': answer_ids})
else:
return
@connection.connection_handler
def get_all_answer_id_to_delete_comments(cursor, question_id):
cursor.execute("""
SELECT id FROM answer
WHERE question_id = %(id)s
""", {'id': question_id})
answer_ids = cursor.fetchall()
id_values = []
for ids in answer_ids:
id_values.append(ids['id'])
id_values = tuple(id_values)
return tuple(id_values)
@connection.connection_handler
def del_question_row(cursor, id):
cursor.execute("""
DELETE FROM question_tag
WHERE question_id = %(id)s;
DELETE FROM comment
WHERE question_id = %(id)s;
DELETE FROM answer
WHERE question_id = %(id)s;
DELETE FROM question
WHERE id = %(id)s""",
{'id': id})
@connection.connection_handler
def answer_delete_by_id(cursor, id):
cursor.execute("""
DELETE FROM comment
WHERE answer_id = %(id)s;
DELETE FROM answer
WHERE id = %(id)s;
""",
{'id': id})
@connection.connection_handler
def question_view_number_counter(cursor, id):
cursor.execute("""
UPDATE question
SET view_number = view_number +1
WHERE id = %(id)s""", {'id': id})
@connection.connection_handler
def vote_up(cursor, id, table):
cursor.execute(
sql.SQL("""
UPDATE {table}
SET vote_number = vote_number + 1
WHERE id = @id""").format(table=sql.Identifier(table), id=sql.Identifier(id)))
@connection.connection_handler
def vote_down(cursor, id, table):
cursor.execute(
sql.SQL("""
UPDATE {table}
SET vote_number = vote_number -1
WHERE id = @id""").format(table=sql.Identifier(table), id=sql.Identifier(id)))
@connection.connection_handler
def add_new_comment(cursor, new_data):
cursor.execute("""INSERT INTO comment (question_id, answer_id, message, submission_time, edited_count, userid)
VALUES (%(question_id)s,%(answer_id)s,%(message)s,%(submission_time)s,%(edited_count)s,%(userid)s)"""
, {'question_id': new_data['question_id'],
'answer_id': new_data['answer_id'],
'message': new_data['message'],
'submission_time': new_data['submission_time'],
'edited_count': new_data['edited_count'],
'userid': new_data['userid']})
@connection.connection_handler
def update_comment(cursor, comment):
cursor.execute("""UPDATE comment
SET
message = %(message)s,
edited_count = edited_count +1
WHERE id = %(id)s"""
, {'id': comment['id'], 'message': comment['message']})
@connection.connection_handler
def get_question_comments(cursor, id):
cursor.execute("""SELECT * FROM comment
JOIN user_info on comment.userid = user_info.user_id
WHERE comment.question_id = %(id)s""", {'id': id})
comments = cursor.fetchall()
return comments
@connection.connection_handler
def get_answer_comments(cursor):
cursor.execute("""SELECT * FROM comment
JOIN user_info on comment.userid = user_info.user_id
WHERE answer_id=answer_id""", {'id': id})
comments = cursor.fetchall()
return comments
@connection.connection_handler
def get_answer_comment_by_comment_id(cursor, id):
cursor.execute("""SELECT * FROM comment WHERE id = %(id)s""", {'id': id})
comment = cursor.fetchall()
return comment[0]
@connection.connection_handler
def get_answer_id_by_comment_id(cursor, id):
cursor.execute("""SELECT answer_id FROM comment WHERE id = %(id)s""", {'id': id})
answer_id = cursor.fetchall()
return answer_id[0].get('answer_id')
@connection.connection_handler
def delete_comment_by_comment_id(cursor, id):
cursor.execute("""DELETE FROM comment WHERE id = %(id)s""", {'id': id})
@connection.connection_handler
def get_question_id_by_answer_id(cursor, id):
cursor.execute("""SELECT question_id FROM answer WHERE id = %(id)s""", {'id': id})
question_id = cursor.fetchall()
return question_id[0].get('question_id')
@connection.connection_handler
def get_question_id_by_comment_id(cursor, id):
cursor.execute("""select question_id from comment where id = %(id)s""", {'id': id})
question_id = cursor.fetchall()
return question_id[0].get('question_id')
@connection.connection_handler
def ascending_order(cursor, sorted):
if sorted == 'sub_asc':
sort = 'submission_time'
if sorted == 'view_asc':
sort = 'view_number'
if sorted == 'vote_asc':
sort = 'vote_number'
cursor.execute(sql.SQL("""
SELECT *
FROM question
ORDER BY {sort} ASC """)
.format(sort=sql.Identifier(sort)))
sort_asc = cursor.fetchall()
return sort_asc
@connection.connection_handler
def descending_order(cursor, sorted):
if sorted == 'sub_desc':
sort = 'submission_time'
if sorted == 'view_desc':
sort = 'view_number'
if sorted == 'vote_desc':
sort = 'vote_number'
cursor.execute(sql.SQL("""
SELECT *
FROM question
ORDER BY {sort} DESC """)
.format(sort=sql.Identifier(sort)))
sort_desc = cursor.fetchall()
return sort_desc
def do_search(search_phrase):
question_ids_from_questions = get_search_question_ids(search_phrase)
question_ids_from_answers = get_search_answers_ids(search_phrase)
ids_ = []
if len(question_ids_from_answers) > 0 and len(question_ids_from_questions) > 0:
print('both valid')
ids_ = question_ids_from_answers + question_ids_from_questions
elif len(question_ids_from_answers) > 0:
print('fromanswers is valid only')
ids_ = question_ids_from_answers
elif len(question_ids_from_questions) > 0:
ids_ = question_ids_from_questions
print('fromquestions is valid only')
result = make_the_result(ids_)
return result
@connection.connection_handler
def get_search_question_ids(cursor, search_phrase):
phrase = search_phrase.lower()
cursor.execute("""SELECT id FROM question
WHERE lower(title) LIKE '%%' || %(phrase)s || '%%' OR
lower (message ) LIKE '%%' || %(phrase)s || '%%'
""", {'phrase': phrase})
ids_dictsinlist = cursor.fetchall()
ids = []
for id in ids_dictsinlist:
ids.append(id['id'])
return ids
@connection.connection_handler
def get_search_answers_ids(cursor, search_phrase):
phrase = search_phrase.lower()
cursor.execute("""
SELECT question_id FROM answer
WHERE lower (message ) LIKE '%%' || %(phrase)s || '%%'""", {'phrase': phrase})
question_id = cursor.fetchall()
if len(question_id) > 0:
# print(question_id) #this is a list with dictionaries
cursor.execute(""" SELECT * FROM question
WHERE id = %(question_id)s """, {'question_id': question_id[0]['question_id']})
ids_dictsinlist = cursor.fetchall()
ids = []
for id in ids_dictsinlist:
ids.append(id['id'])
return ids
else:
return []
@connection.connection_handler
def make_the_result(cursor, ids_):
if len(ids_) > 0:
cursor.execute(""" SELECT * FROM question
WHERE id IN %(ids)s""", {'ids': tuple(ids_)})
result_table = cursor.fetchall()
return result_table
else:
return []
def get_search_results(cursor, search_phrase):
cursor.execute("""SELECT * FROM question
WHERE title LIKE %(search_phrase)s OR
message LIKE %(search_phrase)s
""", {'search_phrase': search_phrase})
search_result = cursor.fetchall()
return search_result
@connection.connection_handler
def get_all_tag_name_for_question(cursor, question_id):
cursor.execute("""
SELECT tag.name,tag.id FROM question_tag
INNER JOIN tag on question_tag.tag_id = tag.id
WHERE question_id = %(question_id)s
""", {'question_id': question_id})
tag_names = cursor.fetchall()
return tag_names
@connection.connection_handler
def get_tag_id_from_tag_name(cursor, name):
cursor.execute(""" SELECT id FROM tag
WHERE name = %(name)s
""", {'name': name})
tag_id = cursor.fetchone()
return tag_id['id']
@connection.connection_handler
def get_tags_for_select(cursor):
cursor.execute("""SELECT * FROM tag
""")
tags = cursor.fetchall()
return tags
@connection.connection_handler
def add_to_tag_table(cursor, new_data):
cursor.execute("""INSERT INTO tag (name)
VALUES (%(name)s)"""
, {'name': new_data})
@connection.connection_handler
def write_to_question_tag(cursor, question_id, tag_id):
cursor.execute("""
INSERT INTO question_tag (question_id, tag_id)
VALUES (%(question_id)s,%(tag_id)s)"""
, {'question_id': question_id, 'tag_id': tag_id})
@connection.connection_handler
def delete_question_tag(cursor, question_id, tag_id):
cursor.execute("""
DELETE FROM question_tag
WHERE question_id = %(id)s AND tag_id = %(tag)s
""", {'id': question_id, 'tag': tag_id})
@connection.connection_handler
def add_user_details_to_database(cursor, user_information):
cursor.execute("""
INSERT INTO user_info(username, hash, email, creation_date, status)
VALUES ( %(username)s,%(hash)s,%(email)s,%(creation_date)s,%(status)s)""",
{'username': user_information['username'], 'hash': user_information['hash'],
'email': user_information['email'], 'creation_date': user_information['creation_date'],
'status': user_information['status']
})
def hash_password(plain_text_password):
# By using bcrypt, the salt is saved into the hash itself
hashed_bytes = bcrypt.hashpw(plain_text_password.encode('utf-8'), bcrypt.gensalt())
return hashed_bytes.decode('utf-8')
def verify_password(plain_text_password, hashed_password):
hashed_bytes_password = hashed_password.encode('utf-8')
return bcrypt.checkpw(plain_text_password.encode('utf-8'), hashed_bytes_password)
@connection.connection_handler
def verify_pwd(cursor, username):
cursor.execute("""SELECT hash FROM user_info
WHERE username = %(username)s""", {'username': username})
result = cursor.fetchall()
return result[0].get('hash')
@connection.connection_handler
def get_user_id_by_username(cursor, username):
cursor.execute("""SELECT user_id FROM user_info WHERE username = %(username)s""", {'username': username})
result = cursor.fetchone()
return result.get('user_id')
@connection.connection_handler
def get_username_by_user_id(cursor, user_id):
cursor.execute(""" SELECT username FROM user_info
WHERE user_id = %(user_id)s
""", {'user_id': user_id})
name = cursor.fetchone()
return name
@connection.connection_handler
def user_questions(cursor, user_id):
cursor.execute("""
SELECT * FROM question
INNER JOIN user_info on question.userid = user_info.user_id
WHERE question.userid = %(user_id)s AND user_info.user_id = %(user_id)s
""", {'user_id': user_id})
questions = cursor.fetchall()
return questions
@connection.connection_handler
def user_answers(cursor, user_id):
cursor.execute("""
SELECT * FROM answer
LEFT JOIN user_info on answer.userid = user_info.user_id
WHERE answer.userid = %(user_id)s AND user_info.user_id = %(user_id)s
""", {'user_id': user_id})
answers = cursor.fetchall()
return answers
@connection.connection_handler
def user_comments(cursor, user_id):
cursor.execute("""
SELECT * FROM comment
INNER JOIN user_info on comment.userid = user_info.user_id
WHERE comment.userid = %(user_id)s AND user_info.user_id = %(user_id)s
""", {'user_id': user_id})
answers = cursor.fetchall()
return answers
@connection.connection_handler
def list_of_users(cursor):
cursor.execute("""SELECT user_id,username,email,creation_date FROM user_info ;""")
users = cursor.fetchall()
return users
@connection.connection_handler
def get_username_by_question_id(cursor, question_id):
cursor.execute("""SELECT username FROM user_info
INNER JOIN question on question.userid = user_info.user_id
WHERE question.id = %(id)s""", {'id': question_id})
user = cursor.fetchone()
return user['username']
@connection.connection_handler
def check_user_id_authentication_for_question(cursor, username, question_id):
cursor.execute("""SELECT user_id FROM user_info WHERE username = %(username)s
UNION ALL
SELECT user_info.user_id FROM user_info
INNER JOIN question on user_info.user_id = question.userid
WHERE question.id = %(question_id)s""",
{'username': username, 'question_id': question_id})
check = list(cursor)
return check[0] == check[1]
@connection.connection_handler
def check_user_id_authentication_for_answer(cursor, username, answer_id):
cursor.execute("""SELECT user_id FROM user_info WHERE username = %(username)s
UNION ALL
SELECT user_info.user_id FROM user_info
INNER JOIN answer on user_info.user_id = answer.userid
WHERE answer.id = %(answer_id)s""",
{'username': username, 'answer_id': answer_id})
check = list(cursor)
return check[0] == check[1]
@connection.connection_handler
def check_user_id_authentication_for_comment(cursor, username, comment_id):
cursor.execute("""SELECT user_id FROM user_info WHERE username = %(username)s
UNION ALL
SELECT user_info.user_id FROM user_info
INNER JOIN comment on user_info.user_id = comment.userid
WHERE comment.id = %(comment_id)s""",
{'username': username, 'comment_id': comment_id})
check = list(cursor)
return check[0] == check[1]
@connection.connection_handler
def accept_answer(cursor, answer_id):
cursor.execute("""
UPDATE answer
SET answer_status= TRUE
WHERE id = %(answer_id)s""",
{'answer_id': answer_id})
@connection.connection_handler
def deny_answer(cursor, answer_id):
cursor.execute("""
UPDATE answer
SET answer_status= FALSE
WHERE id = %(answer_id)s""",
{'answer_id': answer_id})
@connection.connection_handler
def unaccepted_answers(cursor, user_id):
cursor.execute("""
SELECT answer.id,answer.submission_time,answer.vote_number,answer.question_id,answer.message
FROM answer
LEFT JOIN question
ON answer.question_id = question.id
WHERE answer.answer_status = FALSE AND question.userid = %(user_id)s
""",
{'user_id': user_id})
answers = cursor.fetchall()
return answers
@connection.connection_handler
def question_id_by_comment_ids_for_user_page(cursor, user_id):
cursor.execute("""
SELECT question.id FROM question
LEFT JOIN comment
ON question.id = comment.question_id
WHERE comment.userid = %(user_id)s
""", {'user_id': user_id})
question_ids = cursor.fetchall()
return question_ids