-
Notifications
You must be signed in to change notification settings - Fork 0
/
historyFunctions.py
151 lines (115 loc) · 4.35 KB
/
historyFunctions.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
import pandas as pd
import time
import mysql.connector
from db import getDBCursor, mydb
import json
def insertHistory(sessionCookie, number, w1, w2, w3, replacements, isMultiple=False):
# Get the database cursor
mycursor = getDBCursor()
try:
# Get user id from session cookie
id = sessionCookie.split('-')[0]
# SQL query to insert into search history
query = "INSERT INTO historys (user_id, item_number, w_1, w_2, w_3, results, favorite, timestamp, isMultiple) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s)"
values = (id, number, w1, w2, w3, replacements, False, time.time(), isMultiple,)
# Execute the SQL command
mycursor.execute(query, values)
# Commit the changes
mydb.commit()
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
# Close the cursor and connection
mycursor.close()
def getHistory(sessionCookie):
# Get the database cursor
mycursor = getDBCursor()
try:
# Get user id from session cookie
id = sessionCookie.split('-')[0]
# SQL query to get search history
query = "SELECT id, item_number, w_1, w_2, w_3, isMultiple, timestamp, results FROM historys WHERE user_id = %s"
values = (id,)
# Execute the SQL command
mycursor.execute(query, values)
# Fetch all the results
result = mycursor.fetchall()
return result
# # Get the column names
# columns = [desc[0] for desc in mycursor.description]
# # Create a list of dictionaries, where each dictionary represents a row
# data = []
# for row in result:
# data.append(dict(zip(columns, row)))
# # Convert the list of dictionaries to JSON
# json_result = json.dumps(data)
# return json_result
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
# Close the cursor and connection
mycursor.close()
def addToFavorites(sessionCookie, history_id):
# Get the database cursor
mycursor = getDBCursor()
try:
# Get user id from session cookie
id = sessionCookie.split('-')[0]
# SQL query to update the favorite column
query = "UPDATE historys SET favorite = %s WHERE user_id = %s AND id = %s"
values = (True, id, history_id,)
# Execute the SQL command
mycursor.execute(query, values)
# Commit the changes
mydb.commit()
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
# Close the cursor and connection
mycursor.close()
def removeFromFavorites(sessionCookie, history_id):
# Get the database cursor
mycursor = getDBCursor()
try:
# Get user id from session cookie
id = sessionCookie.split('-')[0]
# SQL query to update the favorite column
query = "UPDATE historys SET favorite = %s WHERE user_id = %s AND id = %s"
values = (False, id, history_id,)
# Execute the SQL command
mycursor.execute(query, values)
# Commit the changes
mydb.commit()
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
# Close the cursor and connection
mycursor.close()
def getFavorites(sessionCookie):
# Get the database cursor
mycursor = getDBCursor()
try:
# Get user id from session cookie
id = sessionCookie.split('-')[0]
# SQL query to get favorite history
query = "SELECT id, item_number, w_1, w_2, w_3, isMultiple, timestamp, results FROM historys WHERE user_id = %s AND favorite = %s"
values = (id, True,)
# Execute the SQL command
mycursor.execute(query, values)
# Fetch all the results
result = mycursor.fetchall()
return result
# # Get the column names
# columns = [desc[0] for desc in mycursor.description]
# # Create a list of dictionaries, where each dictionary represents a row
# data = []
# for row in result:
# data.append(dict(zip(columns, row)))
# # Convert the list of dictionaries to JSON
# json_result = json.dumps(data)
# return json_result
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
# Close the cursor and connection
mycursor.close()