-
Notifications
You must be signed in to change notification settings - Fork 1
/
db.py
214 lines (189 loc) · 7.67 KB
/
db.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
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
import sqlite3
import hashlib
import datetime
import secrets
import os
from email_validator import validate_email, EmailNotValidError
def adduser(by, age, tel, email, password):
path = os.path.dirname(os.path.abspath(__file__))
db = os.path.join(path, 'database.db')
conn = sqlite3.connect(db)
cursor = conn.cursor()
ID = str(secrets.token_urlsafe(16))
passw = (password + ID)
hashed_password = hashlib.md5(passw.encode())
data = {
'ID': ID,
'dete_de_creation': str(datetime.date.today()),
'name': by,
'age': age,
'tel': tel,
'email': email,
'hashed_password': str(hashed_password.hexdigest())
}
cursor.execute("SELECT * FROM USEUR WHERE email=?", (email,))
existing_user = cursor.fetchone()
print(existing_user)
if existing_user:
print("L'e-mail existe déjà dans la base de données.")
raise ValueError("L'e-mail existe déjà dans la base de données.")
try:
valid = validate_email(email)
except EmailNotValidError:
raise ValueError("Email Incorrect")
cursor.execute("""INSERT INTO USEUR(ID, dete_de_crétion, name, age, tel, email, hashed_password)
VALUES(:ID, :dete_de_creation, :name, :age, :tel, :email, :hashed_password)""", data)
conn.commit()
cursor.execute("INSERT INTO stsate(id_user, tiqué_créer, tiqué_partisipé, comm) VALUES (?, 0, 0, 0)", (ID,))
conn.commit()
conn.close()
print("Utilisateur ajouté avec succès!")
return ID
def verify_password(password, email):
conn = sqlite3.connect('database.db') # Updated connection string
cursor = conn.cursor()
cursor.execute("SELECT hashed_password FROM USEUR WHERE email=?", (email,))
existing_user = cursor.fetchone()
cursor.execute("SELECT ID FROM USEUR WHERE email=?", (email,))
ID_user = cursor.fetchone()
if existing_user:
hashed_password = hashlib.md5((password + ID_user[0]).encode()).hexdigest()
if hashed_password == existing_user[0]:
return str(ID_user[0])
else:
return False
else:
return False
def delete_user(email, password):
conn = sqlite3.connect('database.db') # Updated connection string
cursor = conn.cursor()
verify_password(password, email)
cursor.execute("SELECT * FROM USEUR WHERE email=?", (email,))
existing_user = cursor.fetchone()
if existing_user:
cursor.execute("DELETE FROM USEUR WHERE email=?", (email,))
conn.commit()
cursor.execute("DELETE FROM stsate WHERE id_user=?", (existing_user[0],))
conn.commit()
conn.close()
print("Utilisateur supprimé avec succès!")
return True
else:
print("Pas de compte associé à cet e-mail.")
return False
def modify_user(ID_user, by, age, tel, old_email, now_email, old_password, now_password):
conn = sqlite3.connect('database.db') # Updated connection string
cursor = conn.cursor()
cursor.execute("SELECT * FROM USEUR WHERE email=?", (old_email,))
existing_user = cursor.fetchone()
print(existing_user)
if existing_user is None:
print("L'e-mail existe pas dans la base de données.")
raise ValueError("L'e-mail existe pas dans la base de données.")
return False
# Verify the old password and get the user ID
if ID_user == verify_password(old_password, old_email):
# Hash the new password with the user ID
now_password_hashed = hashlib.md5((now_password + ID_user).encode()).hexdigest()
# Prepare data for update
data = (by, age, tel, now_email, now_password_hashed, ID_user)
# Execute the update statement
cursor.execute("""UPDATE USEUR SET name=?, age=?, tel=?, email=?, hashed_password=? WHERE ID=?""", data)
# Commit the transaction
conn.commit()
print("Utilisateur mis à jour avec succès!")
return True
elif ID_user != verify_password(old_password, old_email):
print("Mauvais mot de passe.")
raise ValueError("Mauvais mot de passe.")
return False
# Close the connection
conn.close()
def who(ID):
conn = sqlite3.connect('database.db') # Updated connection string
cursor = conn.cursor()
cursor.execute("SELECT * FROM USEUR WHERE ID=?", (str(ID),))
existing_user = cursor.fetchone()
print(existing_user)
print(str(ID)+" coucou")
if existing_user is None:
print("ID existe pas dans la base de données.")
raise ValueError("ID existe pas dans la base de données.")
else:
return existing_user[3]
def state_add_user(type, ID_user, ID_tiqué):
conn = sqlite3.connect('database.db') # Updated connection string
cursor = conn.cursor()
print("state user")
if type == "tiqué_créer" or "post_comm":
try:
user = who(ID_user)
if user is None:
raise ValueError("id user erreur")
except ValueError as e:
print(e)
return ValueError(e)
if type == "tiqué_créer":
cursor.execute(f"SELECT * FROM stsate WHERE id_user=?", (ID_user,))
user = cursor.fetchone()
user = list(user)
user[1] = int(user[1])+1
user = tuple(user)
cursor.execute("UPDATE stsate SET id_user=?, tiqué_créer=?, tiqué_partisipé=?, comm=?", user)
conn.commit()
return True
elif type == "post_comm":
print("post_comm")
conn.close
conn = sqlite3.connect('comm.db') # Updated connection string
cursor = conn.cursor()
cursor.execute(f"SELECT * FROM {ID_tiqué} WHERE ID_user=?", (ID_user,))
existing_tiqué = cursor.fetchone()
if existing_tiqué:
conn.close
conn = sqlite3.connect('database.db') # Updated connection string
cursor = conn.cursor()
cursor.execute("SELECT * FROM stsate WHERE id_user=?", (ID_user,))
user = cursor.fetchone()
user = list(user)
user[3] = int(user[3]) +1
user = tuple(user)
cursor.execute("""UPDATE stsate SET id_user=?, tiqué_créer=?, tiqué_partisipé=?, comm=?""", user)
conn.commit()
return True
else:
conn.close
conn = sqlite3.connect('database.db') # Updated connection string
cursor = conn.cursor()
cursor.execute("SELECT * FROM stsate WHERE id_user=?", (ID_user,))
user = cursor.fetchone()
user = list(user)
user[2] = int(user[2]) +1
user[3] = int(user[3]) +1
user = tuple(user)
cursor.execute("""UPDATE stsate SET id_user=?, tiqué_créer=?, tiqué_partisipé=?, comm=?""", user)
conn.commit()
return True
else:
print("Type invalide.")
return False
conn.close()
def tiqué_type() :
conn = sqlite3.connect('database.db') # Updated connection string
cursor = conn.cursor()
cursor.execute("SELECT * FROM tiqué_type")
tickets = cursor.fetchall()
conn.close()
return tickets
def ader_type() :
conn = sqlite3.connect('database.db') # Updated connection string
cursor = conn.cursor()
cursor.execute("SELECT * FROM arder")
tickets = cursor.fetchall()
conn.close()
return tickets
print(tiqué_type())
# Example usage:
# adduser("coucou", 16, "0643744076", "opm852159@gmail.com", "test123")
# print(verify_password("test123", "opm852159@gmail.com"))
# delete_user("opm852159@gmail.com", "test123")