-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
360 lines (298 loc) · 10.1 KB
/
db.py
File metadata and controls
360 lines (298 loc) · 10.1 KB
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
from fastapi import Cookie
from sqlalchemy import Column, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from constants import db_path
# Define a User model using SQLAlchemy
Base = declarative_base()
class User(Base):
__tablename__ = "users"
user_id = Column(Integer, primary_key=True, index=True)
username = Column(String, unique=True, index=True)
password = Column(String)
# Define a function to establish a SQLite database connection
def connect_db():
import sqlite3
return sqlite3.connect(db_path)
# Function to check if a user is logged in using a cookie
async def get_current_user(username: str = Cookie(None)):
if username:
db_user = get_user_by_username(username)
if db_user:
return db_user
return None
# Function to get a user by username from the database
def get_user_by_username(username: str):
conn = connect_db()
cursor = conn.cursor()
cursor.execute("SELECT * FROM users WHERE username=?", (username,))
user = cursor.fetchone()
conn.close()
if not user is None:
return {
"id": user[0],
"username": user[1],
"password": user[2],
}
return user
# Function to create a new user
def create_user(username: str, password: str):
conn = connect_db()
cursor = conn.cursor()
cursor.execute(
"INSERT INTO users (username, password) VALUES (?, ?)", (username, password)
)
conn.commit()
conn.close()
def insert_user_word_and_translations(user_id, original_word, translations):
conn = connect_db()
cursor = conn.cursor()
try:
# Insert the original word into UserSubmissions table
cursor.execute(
"INSERT INTO user_submissions (user_id, original_word) VALUES (?, ?)",
(user_id, original_word),
)
submission_id = cursor.lastrowid # Get the ID of the newly inserted submission
# Insert translations into Translations table for each language
for language_code, translated_word in translations.items():
cursor.execute(
"INSERT INTO translations (submission_id, language_code, translated_word) VALUES (?, ?, ?)",
(submission_id, language_code, translated_word),
)
conn.commit()
return submission_id
except Exception as e:
conn.rollback()
raise e
finally:
conn.close()
def delete_user_word_and_translations(submission_id):
conn = connect_db()
cursor = conn.cursor()
try:
# Begin a transaction
conn.execute("BEGIN TRANSACTION")
# Delete the word from the Translations table
cursor.execute(
"""
DELETE FROM translations
WHERE submission_id = ?
""",
(submission_id,),
)
# Delete the word from the UserSubmissions table
cursor.execute(
"""
DELETE FROM user_submissions
WHERE submission_id = ?
""",
(submission_id,),
)
# Commit the transaction to save changes
conn.commit()
except Exception as e:
# Rollback the transaction in case of an error
conn.rollback()
raise e
finally:
conn.close()
def get_user_translations(user_id):
conn = connect_db()
cursor = conn.cursor()
try:
# Retrieve translated words for the current user
cursor.execute(
"""
SELECT us.submission_id, us.original_word, t.language_code, t.translation_id, t.translated_word
FROM user_submissions us
JOIN translations t ON us.submission_id = t.submission_id
WHERE us.user_id = ?
""",
(user_id,),
)
translations = cursor.fetchall()
# Organize the translations into a list of dictionaries
user_translations = []
word_entry = {}
submission_id_current = None
for index, (
submission_id,
original_word,
language_code,
translation_id,
translated_word,
) in enumerate(sorted(translations)):
if not submission_id_current:
submission_id_current = submission_id
if submission_id_current != submission_id:
user_translations.append(word_entry)
word_entry = {}
submission_id_current = submission_id
word_entry["submission_id"] = submission_id
word_entry["origin"] = original_word
word_entry[language_code] = {
"word": translated_word,
"translation_id": translation_id,
}
if index == len(translations) - 1:
user_translations.append(word_entry)
return user_translations
finally:
conn.close()
def add_new_language(user_id, new_language_code):
conn = connect_db()
cursor = conn.cursor()
try:
# Check if the new language already exists in UserLanguages table
cursor.execute(
"""
SELECT language_code
FROM user_languages
WHERE user_id = ? AND language_code = ?
""",
(user_id, new_language_code),
)
existing_language = cursor.fetchone()
# If the language doesn't exist, insert it into UserLanguages table
if not existing_language:
cursor.execute(
"""
INSERT INTO user_languages (user_id, language_code)
VALUES (?, ?)
""",
(user_id, new_language_code),
)
# Identify the user's submitted words for which translations need to be added
cursor.execute(
"""
SELECT submission_id
FROM user_submissions
WHERE user_id = ?
""",
(user_id,),
)
submission_ids = cursor.fetchall()
# Insert new entries for translations with the new language code and empty translated word
for submission_id in submission_ids:
cursor.execute(
"""
INSERT INTO translations (submission_id, language_code, translated_word)
VALUES (?, ?, ?)
""",
(submission_id[0], new_language_code, ""),
)
conn.commit()
finally:
conn.close()
def get_user_languages(user_id):
conn = connect_db()
cursor = conn.cursor()
try:
# Retrieve the list of languages associated with the user
cursor.execute(
"""
SELECT language_code
FROM user_languages
WHERE user_id = ?
""",
(user_id,),
)
languages = [row[0] for row in cursor.fetchall()]
return languages
finally:
conn.close()
def delete_user_language(user_id, language_code):
conn = connect_db()
cursor = conn.cursor()
try:
# Begin a transaction
conn.execute("BEGIN TRANSACTION")
# Delete the language from the UserLanguages table
cursor.execute(
"""
DELETE FROM user_languages
WHERE user_id = ? AND language_code = ?
""",
(user_id, language_code),
)
# Delete the corresponding translations from the Translations table
cursor.execute(
"""
DELETE FROM translations
WHERE submission_id IN (
SELECT submission_id
FROM user_submissions
WHERE user_id = ?
) AND language_code = ?
""",
(user_id, language_code),
)
# Commit the transaction to save changes
conn.commit()
except Exception as e:
# Rollback the transaction in case of an error
conn.rollback()
raise e
finally:
conn.close()
def update_user_languages(user_id, old_languages, updated_languages):
conn = connect_db()
cursor = conn.cursor()
try:
# Begin a transaction
conn.execute("BEGIN TRANSACTION")
# Calculate differences: languages to add and languages to delete
print(updated_languages)
print(old_languages)
languages_to_add = list(set(updated_languages) - set(old_languages))
languages_to_delete = list(set(old_languages) - set(updated_languages))
# Add languages to UserLanguages table
for language_code in languages_to_add:
cursor.execute(
"""
INSERT INTO user_languages (user_id, language_code)
VALUES (?, ?)
""",
(user_id, language_code),
)
# Remove languages from UserLanguages table
for language_code in languages_to_delete:
cursor.execute(
"""
DELETE FROM user_languages
WHERE user_id = ? AND language_code = ?
""",
(user_id, language_code),
)
# Add translations for new languages with empty values
for language_code in languages_to_add:
cursor.execute(
"""
INSERT INTO translations (submission_id, language_code, translated_word)
SELECT submission_id, ?, ''
FROM user_submissions
WHERE user_id = ?
""",
(language_code, user_id),
)
# Remove translations for deleted languages
for language_code in languages_to_delete:
cursor.execute(
"""
DELETE FROM translations
WHERE submission_id IN (
SELECT submission_id
FROM user_submissions
WHERE user_id = ?
) AND language_code = ?
""",
(user_id, language_code),
)
# Commit the transaction to save changes
conn.commit()
except Exception as e:
# Rollback the transaction in case of an error
conn.rollback()
raise e
finally:
conn.close()