-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.py
84 lines (59 loc) · 1.82 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
import psycopg2
import bcrypt
class DbManager:
def __init__(self):
self.conn = psycopg2.connect(
dbname = "chess",
user = "backslash057",
password = "root"
)
self.init_tables()
def init_tables(self):
cursor = self.conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS users(id serial PRIMARY KEY,
username VARCHAR(20), password VARCHAR(64));
TRUNCATE TABLE users;
CREATE TABLE IF NOT EXISTS banned_passwords(pwd VARCHAR(64));
TRUNCATE TAbLE banned_passwords;
""")
self.conn.commit()
cursor.close()
def password_valid(self, password):
cursor = self.conn.cursor()
cursor.execute("SELECT * FROM banned_passwords WHERE pwd=%s", (password,))
num_lines = cursor.rowcount
cursor.close()
if num_lines != 0:
return False
return True
def username_in_use(self, username):
cursor = self.conn.cursor()
cursor.execute("SELECT * FROM users WHERE username=%s", (username,))
num_lines = cursor.rowcount
cursor.close()
if num_lines == 0:
return False
return True
def save_user(self, username, password):
# random_salt = bcrypt.gensalt()
# hashpwd = bcrypt.hashpw(password.encode("utf-8"), random_salt)
# hashpwd = hashpwd.decode("utf-8")
cursor = self.conn.cursor()
sql = "INSERT INTO users(username, password) VALUES (%s, %s);"
cursor.execute(sql, (username, password))
self.conn.commit()
cursor.close()
def valid_user(self, username, password):
cursor = self.conn.cursor()
# random_salt = bcrypt.gensalt()
# hashpwd = bcrypt.hashpw(password.encode("utf-8"), random_salt)
# hashpwd = hashpwd.decode("utf-8")
# print(hashpwd)
cursor.execute("SELECT * FROM users WHERE username=%s AND password=%s", (username, password))
if cursor.rowcount == 0:
return False
return True
def close(self):
self.conn.close()
dbManager = DbManager()