-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
384 lines (300 loc) · 13.8 KB
/
app.py
File metadata and controls
384 lines (300 loc) · 13.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
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
from flask import Flask, request, jsonify, flash
import mysql.connector
from flask_cors import CORS, cross_origin
from mysql.connector import Error
import bcrypt
from werkzeug.security import generate_password_hash, check_password_hash
import re
from flask_sqlalchemy import SQLAlchemy
from flask_user import current_user, login_required, roles_required, UserManager, UserMixin
from email_validator import validate_email, EmailNotValidError
app = Flask(__name__)
CORS(app)
app.config['JSON_ADD_STATUS'] = True
@app.route('/sign-in', methods=['POST'])
def sign_in():
result = [{'msg': 'success'}, {'stat': '200 ok'}]
if request.method == 'POST':
sign_in_details = request.get_json()
email = sign_in_details['email']
password = sign_in_details['password']
hashed_value = generate_password_hash(password)
connection = mysql.connector.connect(host="localhost", user="root", password="1234",
database="sample_project_test_db")
mycursor = connection.cursor()
query = "INSERT into sign_in(email,password) "
mycursor.execute(query)
connection.commit()
return jsonify({'result': result})
@app.route('/sign-in-check-2', methods=['POST'])
def sign_in_check2():
if request.method == 'POST':
sign_in_details = request.get_json(silent=True,force=True)
email = sign_in_details['email']
password = sign_in_details['password']
connection = mysql.connector.connect(host="localhost", user="root", password="1234", database="sample_project_db")
mycursor = connection.cursor()
sql = "SELECT password FROM users Where email=%s LIMIT 1 "
data_search = (email,)
mycursor.execute(sql, data_search)
results = mycursor.fetchone()[0]
connection.commit()
result = check_password_hash(results, password)
if (result):
print("OKEY")
sql ="SELECT * FROM payment Where payment_email=%s "
data_search = (email,)
mycursor.execute(sql, data_search)
results = mycursor.fetchall()
return jsonify(results)
else:
print("WRONG")
return jsonify("user details are invalid")
@app.route("/sign-in-get-all", methods=['POST'])
def sign_in_get_all():
if request.method == 'POST':
user_details = request.get_json()
email = user_details['email']
password = user_details['password']
print(user_details)
connection = mysql.connector.connect(host="localhost", user="root", password="1234", database="sample_project_db")
mycursor = connection.cursor()
mycursor.execute("SELECT * FROM users")
results = mycursor.fetchall()
connection.commit()
return jsonify(results)
@app.route("/sign-in-get", methods=['POST'])
def sign_in_get():
if request.method == 'POST':
user_details = request.form
email = user_details['email']
password = user_details['password']
print(user_details)
connection = mysql.connector.connect(host="localhost", user="root", password="1234", database="sample_project_db")
mycursor = connection.cursor()
sql = "SELECT password FROM users Where email=%s LIMIT 1 "
data_search = (email,)
mycursor.execute(sql, data_search)
results = mycursor.fetchone()[0]
print(results)
connection.commit()
result = check_password_hash(results, password)
return str(result)
return jsonify({'results': result})
@app.route('/<password>', methods=['POST'])
def pasword_hash(password):
hashed_value = generate_password_hash(password)
print(hashed_value)
stored_password = 'pbkdf2:sha256:150000$XDjeRCGn$7c8451de5476ff5c9ffbb038d2128cd0042032170d8b6388f0a8a9bf86f781b4'
print(stored_password)
result = check_password_hash(stored_password, password)
print(result)
return str(result)
@app.route("/sign-in-get-without-hash", methods=['GET'])
def sign_in_get_hash():
sign_in_get_details = request.get_json()
email = sign_in_get_details['email']
password = sign_in_get_details['password']
print(sign_in_get_details)
connection = mysql.connector.connect(host="localhost", user="root", password="1234", database="sample_project_db")
mycursor = connection.cursor()
sql = "SELECT email,password FROM users Where email=%s AND password=%s "
data_search = (email, password)
mycursor.execute(sql, data_search)
results = mycursor.fetchone()[0]
print(results)
connection.commit()
result = check_password_hash(results, password)
return str(result)
return jsonify({'results': result})
@app.route('/sign-up', methods=['POST'])
def sign_up_get():
result = [{'msg': 'success'}, {'stat': '200 ok'}]
if request.method == 'POST':
sign_up_details = request.get_json()
email = sign_up_details['email']
password = sign_up_details['password']
firstname = sign_up_details['firstname']
lastname = sign_up_details['lastname']
hashed_value = generate_password_hash(password)
connection = mysql.connector.connect(host="localhost", user="root", password="1234",
database="sample_project_db")
mycursor = connection.cursor()
sql = "SELECT email FROM users Where email=%s"
data_search = (email,)
mycursor.execute(sql, data_search)
results = mycursor.fetchall()
print(results)
if (results == []):
regex = '(^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$)'
# for custom mails use: '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w+$'
# Define a function for
# for validating an Email
# pass the regular expression
# and the string in search() method
if (re.search(regex, email)):
print("Valid Email")
l, u, p, d = 0, 0, 0, 0
s = password
if (len(s) >= 8):
for i in s:
# counting lowercase alphabets
if (i.islower()):
l += 1
# counting uppercase alphabets
if (i.isupper()):
u += 1
# counting digits
if (i.isdigit()):
d += 1
# counting the mentioned special characters
if (i == '@' or i == '$' or i == '_'):
p += 1
if (l >= 1 and u >= 1 and p >= 1 and d >= 1 and l + p + u + d == len(s)):
print("Valid Password")
hashed_value = generate_password_hash(password)
connection = mysql.connector.connect(host="localhost", user="root", password="1234",
database="sample_project_db")
mycursor = connection.cursor()
query = "INSERT INTO users(first_name,last_name,email, password) VALUES (%s,%s,%s,%s)"
val = (firstname, lastname, email, hashed_value)
mycursor.execute(query, val)
connection.commit()
return "OKEY"
else:
print("Please Try Again")
return "Invalid Password"
else:
print("Invalid Email")
return "Enter Valid Email"
l, u, p, d = 0, 0, 0, 0
s = password
if (len(s) >= 8):
for i in s:
# counting lowercase alphabets
if (i.islower()):
l += 1
# counting uppercase alphabets
if (i.isupper()):
u += 1
# counting digits
if (i.isdigit()):
d += 1
# counting the mentioned special characters
if (i == '@' or i == '$' or i == '_'):
p += 1
if (l >= 1 and u >= 1 and p >= 1 and d >= 1 and l + p + u + d == len(s)):
print("Valid Password")
hashed_value = generate_password_hash(password)
connection = mysql.connector.connect(host="localhost", user="root", password="1234",
database="sample_project_db")
mycursor = connection.cursor()
query = "INSERT INTO users(first_name,last_name,email, password) VALUES (%s,%s,%s,%s)"
val = (firstname, lastname, email, hashed_value)
mycursor.execute(query, val)
connection.commit()
return jsonify({'result': result})
else:
print("Please Try Again")
return "Invalid Password"
else:
print("user already exists in the database")
return 'Please enter another email'
connection.commit()
return jsonify({'results': results})
@app.route('/address', methods=['POST'])
def address():
result = [{'msg': 'success'}, {'stat': '200 ok'}]
if request.method == 'POST':
address_details = request.get_json()
line1 = address_details['line1']
line2 = address_details['line2']
postalcode = address_details['postalcode']
city = address_details['city']
state = address_details['state']
country = address_details['country']
connection = mysql.connector.connect(host="localhost", user="root", password="1234",
database="sample_project_db")
mycursor = connection.cursor()
query = "INSERT INTO address(line1,line2,postal_code,city,state,country) VALUES (%s,%s,%s,%s,%s,%s)"
val = (line1, line2, postalcode, city, state, country)
mycursor.execute(query, val)
connection.commit()
return jsonify({'result': result})
@app.route('/payment', methods=['POST'])
def payment():
result = [{'msg': 'success'}, {'stat': '200 ok'}]
if request.method == 'POST':
payment_details = request.get_json()
paymenttype = payment_details['paymenttype']
paymentamount = payment_details['paymentamount']
paymentemail = payment_details['paymentemail']
paymentownername = payment_details['paymentownername']
connection = mysql.connector.connect(host="localhost", user="root", password="1234",
database="sample_project_db")
mycursor = connection.cursor()
sql = "SELECT email FROM users Where email=%s"
data_search = (paymentemail,)
mycursor.execute(sql, data_search)
results = mycursor.fetchall()
if(results==[]):
return jsonify("Enter Valid email you Sign In")
else:
query = "INSERT INTO payment(payment_type,payment_amount,payment_email,payment_owner_name) VALUES (%s,%s,%s,%s)"
val = (paymenttype,paymentamount, paymentemail, paymentownername)
mycursor.execute(query, val)
connection.commit()
return jsonify("Payment Details successfully Entered Into the Database")
@app.route("/address-get", methods=['GET'])
def address_get():
try:
connection = mysql.connector.connect(host='localhost', database='sample_project_db', user='root',
password='1234')
sql_select_Query = "select * from address"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
return jsonify({'records': records})
except Error as e:
print("Error reading data from MySQL table", e)
finally:
if (connection.is_connected()):
connection.close()
cursor.close()
print("MySQL connection is closed")
@app.route('/business', methods=['POST'])
def business():
result = [{'msg': 'success'}, {'stat': '200 ok'}]
if request.method == 'POST':
business_details = request.get_json()
businessname = business_details['businessname']
businessownername = business_details['businessownername']
businessregno = business_details['businessregno']
contactno = business_details['contactno']
connection = mysql.connector.connect(host="localhost", user="root", password="1234",
database="sample_project_db")
mycursor = connection.cursor()
query = "INSERT INTO business(business_name,business_owner_name,business_reg_no,contact_no) VALUES (%s,%s,%s,%s)"
val = (businessname, businessownername, businessregno, contactno)
mycursor.execute(query, val)
connection.commit()
return jsonify({'result': result})
@app.route("/business-get", methods=['GET'])
def business_get():
try:
connection = mysql.connector.connect(host='localhost', database='sample_project_db', user='root',
password='1234')
sql_select_Query = "select * from business"
cursor = connection.cursor()
cursor.execute(sql_select_Query)
records = cursor.fetchall()
return jsonify({'records': records})
except Error as e:
print("Error reading data from MySQL table", e)
finally:
if (connection.is_connected()):
connection.close()
cursor.close()
print("MySQL connection is closed")
if __name__ == "__main__":
app.run(debug=True)