-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.py
100 lines (76 loc) · 3.21 KB
/
user.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
import hashlib
import mysql
from database import Database
from item import Item
class User:
def __init__(self, user_id, first_name, last_name, username, email):
self.user_id = user_id
self.first_name = first_name
self.last_name = last_name
self.username = username
self.email = email
@classmethod
def login(cls, username, password):
hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
db = Database()
cursor = db.database.cursor()
query = "SELECT user_id, first_name, last_name, username, email FROM users WHERE username = %s AND password = %s"
cursor.execute(query, [username, hashed_password])
user_info = cursor.fetchone()
cursor.close()
db.database.close()
if user_info is None or user_info[0] is None:
raise ValueError("Invalid Login Credentials")
user = cls(*user_info)
return user
@classmethod
def signup(cls, first_name, last_name, username, email, password, repeat_password):
if not User.password_validation(password, repeat_password):
raise ValueError("Passwords dont match")
if not User.user_info_validation(first_name, last_name, username, email):
raise ValueError("Invalid SignUp Credentials")
hashed_password = hashlib.sha256(password.encode('utf-8')).hexdigest()
db = Database()
cursor = db.database.cursor()
query = "INSERT INTO users (username, password, first_name, last_name, email) VALUES (%s, %s, %s, %s, %s)"
try:
cursor.execute(query, [username, hashed_password, first_name, last_name, email])
db.database.commit()
except mysql.connector.errors.IntegrityError:
raise ValueError("This Username is taken")
cursor.close()
db.database.close()
user = cls(cursor.lastrowid, first_name, last_name, username, email)
return user
@staticmethod
def password_validation(password, repeatpassword):
return password == repeatpassword
@staticmethod
def user_info_validation(*args):
for arg in args:
if len(arg) == 0:
return False
return True
def __str__(self) -> str:
return f"User({self.username}, {self.user_id})"
def __repr__(self) -> str:
return f"User({self.username}, {self.user_id})"
def getFavorites(self):
db = Database()
cursor = db.database.cursor()
query = "SELECT item_id FROM favorites WHERE user_id = %s"
cursor.execute(query, [self.user_id])
item_ids = cursor.fetchall()
favorite_items = []
for item_id in item_ids:
favorite_items.append(Item.load(item_id[0]))
return favorite_items
def addFavorite(self, item_id):
db = Database()
cursor = db.database.cursor()
query = "INSERT INTO favorites (user_id, item_id) VALUES (%s, %s)"
print("user_id, item_id", self.user_id, item_id)
cursor.execute(query, [self.user_id, item_id])
db.database.commit()
cursor.close()
db.database.close()