-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
418 lines (336 loc) · 12.1 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
from datetime import datetime
from flask import Flask
import mysql.connector
from werkzeug.security import generate_password_hash
from user import User
import mysql.connector
from contextlib import closing
import base64
mydb = mysql.connector.connect(
host="db",
user="root",
password="12345",
database="chat"
)
mycursor = mydb.cursor()
mycursor.execute("USE chat")
""""""
mycursor.execute("""
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
password VARCHAR(255) NOT NULL
)
""")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS rooms (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
created_by VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS room_members (
room_id INT,
username VARCHAR(255) NOT NULL,
room_name VARCHAR(255) NOT NULL,
added_by VARCHAR(255) NOT NULL,
added_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
is_room_admin BOOLEAN,
PRIMARY KEY (room_id, username),
FOREIGN KEY (room_id) REFERENCES rooms(id)
)
""")
mycursor.execute("""
CREATE TABLE IF NOT EXISTS messages (
id INT AUTO_INCREMENT PRIMARY KEY,
room_id INT NOT NULL,
text TEXT NOT NULL,
sender VARCHAR(255) NOT NULL,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
""")
def save_user(username, email, password):
try:
# Şifreyi güvenli bir şekilde hashle
pasword_hash = generate_password_hash(password)
# Kullanıcıyı veritabanına ekle
mycursor.execute("""
INSERT INTO users (username, email, password)
VALUES (%s, %s, %s)
""", (username, email, pasword_hash))
mydb.commit()
except mysql.connector.IntegrityError as e:
if e.errno==1062:
raise ValueError("Bu kullanıcı adı veya e-posta adresi zaten kullanılıyor.")
else:
raise e
def get_user(username):
user = None
try:
with closing(mydb.cursor()) as mycursor:
mycursor.execute("""
SELECT id, username, email, password FROM users
WHERE username = %s
""", (username,))
user_data = mycursor.fetchone()
print("USERDATA FETCHONE")
if user_data:
print("USERDATA İFİN İÇİ = ",user_data)
user_id, username, email, password_hash = user_data
print("VERİLER = ",user_id, username, email, password_hash)
user = User(user_id, email, password_hash)
mycursor.clear_attributes()
print("USERE GİRDİ =",user)
return User(user_id, email, password_hash)
except Exception as e:
print(f"HATA ? ? ******** = {e}")
return None
def save_room(room_name,created_by):
room_id = None
try:
# Kullanıcı kontrolü
user = get_user(created_by)
if not user:
raise ValueError("Kullanıcı bulunamadı veya geçersiz.")
# Odayı veritabanına ekle
created_at = datetime.now()
mycursor.execute("""
INSERT INTO rooms (name, created_by, created_at)
VALUES (%s, %s, %s)
""", (room_name, created_by, created_at))
mycursor.execute("SELECT LAST_INSERT_ID()")
room_id = mycursor.fetchone()[0]
mydb.commit()
except Exception as e:
print(f"HATA = {e}")
add_room_member(room_id, room_name, created_by, created_by, is_room_admin=True)
return room_id
def delete_room_and_members(room_id):
try:
# Odaya ait üyelerin silinmesi
remove_room_members(room_id)
# Odaya ait mesajların silinmesi
delete_room_messages(room_id)
# Odayı veritabanından silme
mycursor.execute("""
DELETE FROM rooms
WHERE id = %s
""", (room_id,))
mydb.commit()
print(f"Oda {room_id} Ve Üyeleri Başarıyla Silindi.")
except mysql.connector.Error as e:
print(f"Hata: {e}")
mydb.rollback() # Hata durumunda geri alma işlemi yapılır
def remove_room_members(room_id):
delet_query = """
DELETE FROM room_members
WHERE room_id = %s
"""
mycursor.execute(delet_query, (room_id,))
mydb.commit()
def delete_room_messages(room_id):
try:
global mycursor, mydb # Eğer global değişkenler değilse bu satırı çıkarın
# Odaya ait mesajların silinmesi
mycursor.execute("""
DELETE FROM messages
WHERE room_id = %s
""", (room_id,))
mydb.commit()
print(f"{room_id} Odasının Mesajları Başarıyla Silindi")
except mysql.connector.Error as e:
print(f"Hata: {e}")
def get_room(room_id):
try:
mycursor.execute("""
SELECT * FROM rooms
WHERE id = %s
""", (room_id,))
room_data = mycursor.fetchone()
if room_data:
room_id, name, created_by, created_at = room_data
print(f"Oda Bulundu: id={room_id}, name={name}, created_by={created_by}, created_at={created_at}")
return room_data
else:
print("Oda Bulunamadı")
return None
except mysql.connector.Error as e:
print(f"Hata: {e}")
return None
def add_room_member(room_id, room_name, username, added_by, is_room_admin=False):
try:
# Oda üyesini eklemek için INSERT sorgusu
added_at = datetime.now()
mycursor.execute("""
INSERT INTO room_members (room_id, username, room_name, added_by, added_at, is_room_admin)
VALUES (%s, %s, %s, %s, %s, %s)
""", (room_id, username, room_name, added_by, added_at, is_room_admin))
# Veritabanındaki değişiklikleri kaydetme
mydb.commit()
print("Oda üyesi başarıyla eklendi.")
except mysql.connector.Error as e:
print(f"Hata: {e}")
def add_room_members(room_id, room_name, usernames, added_by):
try:
# Oda üyelerini eklemek için INSERT sorgusu
added_at = datetime.now()
is_room_admin = False
insert_query = """
INSERT INTO room_members (room_id, username, room_name, added_by, added_at, is_room_admin)
VALUES (%s, %s, %s, %s, %s, %s)
"""
# Her kullanıcı için INSERT işlemi gerçekleştir
for username in usernames:
mycursor.execute(insert_query, (room_id, username, room_name, added_by, added_at, is_room_admin))
# Veritabanındaki değişiklikleri kaydetme
mydb.commit()
print("Oda üyeleri başarıyla eklendi.")
except mysql.connector.Error as e:
print(f"Hata: {e}")
def remove_room_members(room_id, usernames):
try:
print("Oda Üyeleri Siliniyor")
delet_query = """
DELETE FROM room_members
WHERE room_id = %s AND username = %s
"""
for username in usernames:
mycursor.execute(delet_query, (room_id, username))
mydb.commit()
print(f"Oda üyeleri başarıyla silindi.")
except mysql.connector.Error as e:
print(f"Hata: {e}")
def get_room_members(room_id):
try:
mycursor.execute("""
SELECT * FROM room_members
WHERE room_id = %s
""", (room_id,))
room_members_data = mycursor.fetchall()
if room_members_data:
print(f"room_id={room_id} içinde oda üyeleri bulundu.")
return room_members_data
else:
print("Oda üyeleri bulunamadı.")
return []
except mysql.connector.Error as e:
print(f"Hata: {e}")
return []
def get_rooms_for_user(username):
try:
mycursor.execute("""
SELECT * FROM room_members
WHERE username = %s
""", (username,))
user_rooms_data = mycursor.fetchall()
if user_rooms_data:
print(f"{username} Kullanıcısı Odada Bulundu")
return user_rooms_data
else:
print(f"{username} Kullanıcısı Odada Bulunamadı")
return []
except mysql.connector.Error as e:
print(f"Hata: {e}")
return []
def is_valid_members(usernames):
try:
with closing(mydb.cursor()) as mycursor:
for username in usernames:
mycursor.execute("""
SELECT COUNT(*) FROM users
WHERE username = %s
""", (username,))
count = mycursor.fetchone()[0]
if count == 0:
return False
return True
except mysql.connector.Error as e:
print(f"Hata: {e}")
def is_email_members(email):
try:
with closing(mydb.cursor()) as mycursor:
for email in email:
mycursor.execute("""
SELECT COUNT(*) FROM users
WHERE email = %s
""", (email,))
count = mycursor.fetchone()[0]
if count == 0:
return False
return True
except mysql.connector.Error as e:
print(f"Hata: {e}")
def is_room_member(room_id, username):
try:
mycursor.execute("""
SELECT COUNT(*) FROM room_members
WHERE room_id = %s AND username = %s
""", (room_id, username))
count = mycursor.fetchone()[0]
if count > 0:
print(f"{username} Kullanıcısı {room_id} Odasına Üye")
return True
else:
print(f"{username} Kullanıcısı {room_id} Odasına Üye Değil")
return False
except mysql.connector.Error as e:
print(f"Hata: {e}")
return False
def is_room_admin(room_id, username):
try:
mycursor.execute("""
SELECT COUNT(*) FROM room_members
WHERE room_id = %s AND username = %s AND is_room_admin = 1
""", (room_id, username))
count = mycursor.fetchone()[0]
if count > 0:
print(f"{username} room_id={room_id} Odasının Admini ")
return True
else:
print(f"{username} room_id={room_id} Odasının Admini Değil")
return False
except mysql.connector.Error as e:
print(f"Hata: {e}")
return False
def save_message(room_id, text, sender):
try:
date_time = datetime.now()
# Mesajı Base64 ile şifrele
encoded_message = base64.b64encode(text.encode()).decode()
insert_query = """
INSERT INTO messages (room_id, text, sender, created_at)
VALUES (%s, %s, %s, %s)
"""
mycursor.execute(insert_query, (room_id, encoded_message, sender, date_time))
mydb.commit()
except mysql.connector.Error as e:
print(f"HATA: {e}")
message_fetch_limit=999999 # Sayfa başına gösterilecek mesaj sayısı
def get_message(room_id,page=0):
try:
offset = page * message_fetch_limit
with closing(mydb.cursor()) as mycursor:
mycursor.execute("""
SELECT * FROM messages
WHERE room_id = %s
ORDER BY created_at DESC
LIMIT %s OFFSET %s
""", (room_id, message_fetch_limit, offset))
messages_data = mycursor.fetchall()
if messages_data:
print(f"Mesaj bulundu, Oda={room_id}")
messages_data = [list(message) for message in messages_data]
# Mesajı Base64 şifresini çözümle
for message in messages_data:
message[2] = base64.b64decode(message[2]).decode()
for message in messages_data:
message[4] = message[4].strftime("%d %b, %H:%M")
return messages_data[::-1]
else:
print(f"Mesaj Bulunamadı, Oda={room_id}")
return []
except mysql.connector.Error as e:
print(f"Hata: {e}")