This repository was archived by the owner on Dec 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuser_commits.py
More file actions
105 lines (81 loc) · 3.8 KB
/
user_commits.py
File metadata and controls
105 lines (81 loc) · 3.8 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
import connection
from psycopg2 import sql
import util
import data_handler
def bind_user_to_message(data_dict):
msg_id, msg_column, msg_table, connection_table, usr_table = (data_dict["id"],
data_dict["bind_column"],
data_dict["message_table"],
data_dict["connection_table"],
data_dict["user_table_column"])
handle_new_message(msg_id, msg_column, msg_table, connection_table)
handle_user_attributes(usr_table, msg_id)
def handle_new_message(user_id, column, message_table, connection_table):
message_id = get_last_id_from_table(message_table)[0]['id']
connect_user_to_id(user_id, column, message_id, connection_table)
@connection.connection_handler
def handle_user_attributes(cursor, column, user_id):
query = """
UPDATE user_table
SET {column} = COALESCE({column}, 0) + 1
WHERE user_id = {user_id}
"""
cursor.execute(sql.SQL(query).format(column=sql.Identifier(column),
user_id=sql.Literal(user_id)))
@connection.connection_handler
def get_last_id_from_table(cursor, table):
query = """
SELECT id FROM {table}
ORDER BY id DESC
LIMIT 1
"""
cursor.execute(sql.SQL(query).format(table=sql.Identifier(table)))
return cursor.fetchall()
@connection.connection_handler
def connect_user_to_id(cursor, user_id, column, message_id, table):
query = """
INSERT INTO {table}
(user_id, {column})
VALUES ({user_id}, {message_id})
"""
cursor.execute(sql.SQL(query).format(table=sql.Identifier(table),
column=sql.Identifier(column),
user_id=sql.Literal(user_id),
message_id=sql.Literal(message_id)))
def get_questions_by_user_id(user_id):
question_ids = get_message_ids(user_id, "question_id", "question_to_user")
return get_elements_from_db_by_id("question_id", "question", question_ids)
def get_answer_dict_by_question_id(question_ids):
answers = {id_dict['question_id']: data_handler.get_answers_by_question_id(id_dict['question_id'])
for id_dict in question_ids}
return answers
def get_answers_by_user_id(user_id):
answer_ids = get_message_ids(user_id, "answer_id", "answer_to_user")
return get_elements_from_db_by_id("answer_id", "answer", answer_ids)
def get_comments_by_user_id(user_id):
comment_ids = get_message_ids(user_id, "comment_id", "comment_to_user")
comments = get_elements_from_db_by_id("comment_id", "comment", comment_ids)
for comment in comments:
if not comment.get("question_id"):
comment['question_id'] = data_handler.get_question_id_by_answer_id(comment["answer_id"])[0]["question_id"]
return comments
def get_elements_from_db_by_id(key, table, dictionary):
return [util.get_data_by_id(answer_id[key], table)[0] for answer_id in dictionary]
@connection.connection_handler
def get_user_data_by_user_id(cursor, user_id):
query = """
SELECT * FROM user_table
WHERE user_id = {user_id}
"""
cursor.execute(sql.SQL(query).format(user_id=sql.Literal(user_id)))
return cursor.fetchall()
@connection.connection_handler
def get_message_ids(cursor, user_id, column, table):
query = """
SELECT {column} FROM {table}
WHERE user_id = {user_id}
"""
cursor.execute(sql.SQL(query).format(column=sql.Identifier(column),
table=sql.Identifier(table),
user_id=sql.Literal(user_id)))
return cursor.fetchall()