-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembeddedQueries.txt
190 lines (170 loc) · 7.18 KB
/
embeddedQueries.txt
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
from flask import Flask, render_template, request, redirect, url_for
import mysql.connector
import datetime
import ast
userMain = None
app = Flask(__name__)
# Change these settings with your own machine specs
# Also don't forget to copy-paste and run dbQueryDump in default console before running the program
db_config = {
'host': 'localhost',
'user': 'root',
'password': 'dev123',
'database': 'kads'
}
def delete_cart_table():
connect = mysql.connector.connect(**db_config)
cursor = connect.cursor()
cursor.execute("DROP TABLE IF EXISTS CART")
connect.commit()
cursor.close()
connect.close()
@app.route('/')
def index():
delete_cart_table()
return render_template('home.html')
@app.route('/customerHome.html')
def customerHome():
return render_template('customerHome.html')
@app.route('/customer_profile.html')
def customer_profile():
user_id_str = request.args.get('user_id')
user_id = ast.literal_eval(user_id_str)
print(user_id)
print(type(user_id))
return render_template('customer_profile.html', user=user_id)
@app.route('/get_products.html')
def getProductOnWeb():
# user_id_str = request.args.get('user_id')
# user_id = ast.literal_eval(user_id_str)
connect = mysql.connector.connect(**db_config)
cursor = connect.cursor()
cursor.execute(
"SELECT * FROM PRODUCT"
)
inventory = cursor.fetchall()
cursor.close()
connect.close()
# print(user_id['CustomerID'])
# print(type(user_id))
return render_template('/get_products.html', inventory=inventory)
@app.route('/forgot_password.html', methods=['GET', 'POST'])
def forgot_password():
if request.method == 'POST':
email = request.form.get('email')
security_answer = request.form.get('security_answer')
new_password = request.form.get('new_password')
connect = mysql.connector.connect(**db_config)
cursor = connect.cursor(dictionary=True)
# Fetch the user's security answer based on the email
cursor.execute("SELECT CustomerID, SecurityAnswer FROM customer WHERE Email = %s", (email,))
user = cursor.fetchone()
if user and user['SecurityAnswer'].lower() == security_answer.lower():
# Update the user's password (ensure to hash the password in a real application)
cursor.execute("UPDATE customer SET Password = %s WHERE CustomerID = %s",
(new_password, user['CustomerID']))
connect.commit()
message = "Your password has been successfully changed."
print(message)
else:
message = "Incorrect security answer or email."
print(message)
cursor.close()
connect.close()
return redirect(url_for('login', message = message))
else:
return render_template('forgot_password.html')
@app.route('/signup.html', methods=['POST', 'GET'])
def signup():
if request.method == 'POST':
fullname = request.form['fullname']
email = request.form['email']
contact = request.form['contact']
address1 = request.form['address1']
address2 = request.form['address2']
address3 = request.form['address3']
password = request.form['password']
customerid = str(datetime.date.today().year) + fullname.split()[0] + contact[5:10]
contact = int(contact)
security_question = request.form['security_question']
security_answer = request.form['security_answer']
connect = mysql.connector.connect(**db_config)
cursor = connect.cursor()
cursor.execute(
"INSERT INTO customer (CustomerID, FullName, Email, ContactDetails, AddressLine1, AddressLine2, AddressLine3, Password, SecurityQuestion, SecurityAnswer) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
(customerid, fullname, email, contact, address1, address2, address3, password, security_question,
security_answer))
connect.commit()
cursor.close()
connect.close()
return redirect(url_for('index'))
else:
return render_template('/signup.html')
@app.route('/login.html', methods=['GET', 'POST'])
def login():
message = request.args.get('message', '')
if request.method == 'POST':
email = request.form.get('email')
password = request.form.get('password')
if not email or not password:
return "Email and password are required."
connect = mysql.connector.connect(**db_config)
cursor = connect.cursor(dictionary=True)
cursor.execute("SELECT AccountLockedUntil FROM customer WHERE Email = %s", (email,))
account_info = cursor.fetchone()
if account_info and account_info['AccountLockedUntil']:
remaining_lockout_time = account_info['AccountLockedUntil'] - datetime.datetime.now()
if remaining_lockout_time.total_seconds() > 0:
remaining_minutes = remaining_lockout_time.seconds // 60
remaining_seconds = remaining_lockout_time.seconds % 60
lockout_message = f"Your account is locked. Please try again in {remaining_minutes} minutes and {remaining_seconds} seconds."
return render_template('/profileLock.html', lockout_message=lockout_message)
# Need to add one html page here
cursor.execute("SELECT * FROM customer WHERE Email = %s", (email,))
user = cursor.fetchone()
print(user)
if user and user['Password'] == password:
cursor.execute(
"INSERT INTO LoginAttempts (CustomerID, Success) VALUES (%s, TRUE)",
(user['CustomerID'],)
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS cart (
ProductID VARCHAR(60) NOT NULL,
Quantity INT NOT NULL,
CustomerID VARCHAR(20) NOT NULL,
FOREIGN KEY (ProductID) REFERENCES PRODUCT(ProductID),
FOREIGN KEY (CustomerID) REFERENCES CUSTOMER(CustomerID)
)
"""
)
connect.commit()
cursor.close()
connect.close()
dic = {
'CustomerID':user['CustomerID'],
'FullName':user['FullName'],
'Email':user['Email'],
'ContactDetails': user['ContactDetails'],
'AddressLine1':user['AddressLine1'],
'AddressLine2':user['AddressLine2'],
'AddressLine3':user['AddressLine3'],
'MembershipStatus':user['MembershipStatus'],
'OrderHistory':user['OrderHistory']
}
return render_template('/customerHome.html', user=dic)
else:
if user:
cursor.execute(
"INSERT INTO LoginAttempts (CustomerID, Success) VALUES (%s, FALSE)",
(user['CustomerID'],)
)
connect.commit()
cursor.close()
connect.close()
return render_template('/invalidEmailPass.html')
else:
return render_template('login.html', message=message)
if __name__ == '__main__':
app.run(debug=True)