-
Notifications
You must be signed in to change notification settings - Fork 1
/
models.py
136 lines (124 loc) · 4.72 KB
/
models.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
import sqlite3 as sql
from datetime import datetime
from urllib import request
import bcrypt
import secrets
import string
from main import mail, app
import threading
from flask_mail import Message
def mailing(email, body):
with app.app_context():
msg = Message(
f'Your password has been Reset Successfully', recipients=[email])
msg.body = body
mail.send(msg)
def insert_user(username, email, password, mobile_no):
con = sql.connect("database.db")
current = datetime.now()
cur = con.cursor()
password = password.encode('utf-8')
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
try:
cur.execute(
"INSERT INTO users (username, email, password, created_time, mobile_no) VALUES (?,?,?,?,?)", (username, email, hashed, current, mobile_no))
con.commit()
con.close()
con = sql.connect("database.db")
cur = con.cursor()
cur.execute(
"SELECT id, username, email, mobile_no FROM users WHERE email = ?", (email,))
data = cur.fetchall()
con.close()
# print(data)
return True, "user created successfully", data
except Exception as e:
print(str(e))
con.close()
return False, str(e)
def login(email, password):
con = sql.connect("database.db")
cur = con.cursor()
try:
cur.execute(f"SELECT password FROM users where email='{str(email)}'")
hashed = cur.fetchall()
con.close()
if hashed:
password = password.encode('utf-8')
if bcrypt.checkpw(password, hashed[0][0]):
con = sql.connect("database.db")
cur = con.cursor()
cur.execute(
"SELECT id, username, email, mobile_no FROM users WHERE email = ?", (email,))
data = cur.fetchall()
con.close()
# print(data)
return True, "Login Successful", data
else:
return False, "Login Failed, Incorrect password"
else:
return False, "Login Failed, Incorrect Email-id"
except Exception as e:
print(str(e))
con.close()
return False, str(e)
def forget_password(email, username):
con = sql.connect("database.db")
cur = con.cursor()
try:
cur.execute(f"SELECT email FROM users where email='{str(email)}'")
userEmail = cur.fetchall()
con.close()
if userEmail:
res = ''.join(secrets.choice(string.ascii_uppercase + string.digits)
for i in range(8))
password = str(res).encode('utf-8')
hashed = bcrypt.hashpw(password, bcrypt.gensalt())
con = sql.connect("database.db")
cur = con.cursor()
cur.execute(
f"""UPDATE users SET password=? WHERE email=? AND username=?""", (hashed, email, username))
con.commit()
con.close()
body = f'Your new password is : {str(res)}'
x = threading.Thread(target=mailing, args=(
email, body,), daemon=True)
x.start()
# Send new password as email to user OR as SMS to user.
# There are different methods for forget password and reset, I used emailing new password method.
return True, f"Password reset successfully, please check your mail inbox."
else:
return False, "Invalid Information"
except Exception as e:
print(str(e))
con.close()
return False, str(e)
def reset_password(email, password, new_password):
con = sql.connect("database.db")
cur = con.cursor()
try:
cur.execute(f"SELECT password FROM users where email='{str(email)}'")
hashed = cur.fetchall()
con.close()
if hashed:
password = password.encode('utf-8')
if bcrypt.checkpw(password, hashed[0][0]):
new_password = new_password.encode('utf-8')
con = sql.connect("database.db")
cur = con.cursor()
cur.execute(
f"""UPDATE users SET password=? WHERE email=?""", (bcrypt.hashpw(new_password, bcrypt.gensalt()), email))
con.commit()
con.close()
body = f'Your password has been changed successfully\n Time Stamp: {str(datetime.now())}.'
x = threading.Thread(target=mailing, args=(
email, body,), daemon=True)
x.start()
return True, f"Password reset successfully"
else:
return False, "Incorrect password"
else:
return False, "Incorrect Email-id"
except Exception as e:
print(str(e))
return False, str(e)