-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUser.py
More file actions
303 lines (254 loc) · 11 KB
/
User.py
File metadata and controls
303 lines (254 loc) · 11 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
from Autentication import *
from datetime import datetime
import logging
#logger = logging.get#logger(__name__)
class User:
def __init__(self):
self.email = None
self.password = None
self.name = None
self.api_key = {}
self.sessions = {}
self.Paid_User = False
self.time = None
self.date = None
# #logger.info("User module initialized")
def Find_User(self, email):
# Convert email to lowercase for consistency
email = email.lower()
user = Find_User_DB(email)
if user:
return True
else:
return False
def Set_data(self, email, password, name,keys,sessions,Summary_and_Keywords, time, date):
self.email = email
self.password = password
self.name = name
self.api_key = keys
self.sessions = sessions
self.Summary_and_Keywords = Summary_and_Keywords
self.time = time
self.date = date
def Get_data(self):
return self.email, self.password, self.name, self.api_key, self.sessions, self.Summary_and_Keywords, self.time, self.date
def Update_Data(self):
# Convert email to lowercase for consistency
email = self.email.lower() if self.email else ""
collection.update_one({"Email": email}, {"$set": {"Session": self.sessions}})
def Get_Session(self,date):
return self.sessions[date]
def Get_All_Sessions(self):
return self.sessions
def Get_All_Sessions_Dates(self):
return list(self.sessions.keys())
def Get_User_Data(self, email):
# Convert email to lowercase for consistency
email = email.lower()
return Find_User_DB(email)
def Check_User_Credentials(self, email, password):
# Convert email to lowercase for consistency
email = email.lower()
return Authenticate_User(email, password)
def Add_User(self, user):
# Convert all user fields to lowercase for consistency
if isinstance(user, dict):
user = {key.lower(): value for key, value in user.items()}
# Ensure email is lowercase
if 'email' in user:
user['email'] = user['email'].lower()
return Add_User_To_DB(user)
def Get_User_Dates(self, email):
''' Gets all the dates of the user's sessions '''
user = self.Get_User_Data(email)
if user:
return list(user['Session'].keys())
else:
return []
def Get_User_Data_By_Date(self, email, date):
''' Gets the data of the user's session for a specific date '''
user = self.Get_User_Data(email)
if user:
return user['Session'][date]
else:
return None
def Update_S_and_K(self, email, Summary_and_Keywords):
''' Updates the summary and keywords preference of the user '''
try:
# Convert email to lowercase for consistency
email = email.lower()
result = Update_User_Keyword_and_Summary(email, Summary_and_Keywords)
return result
except Exception as e:
return False
def Update_API_Key(self, email, api_key):
''' Updates the API key of the user '''
try:
# Convert email to lowercase for consistency
email = email.lower()
user = self.Get_User_Data(email)
if user:
# Use upsert to create the field if it doesn't exist
result = collection.update_one(
{"Email": email},
{"$set": {"API_Key": api_key}},
upsert=True
)
return True
else:
return False
except Exception as e:
#logger.error(f"Error updating API key for {email}: {str(e)}")
return False
def Update_Paid_User(self, email, paid_user_status):
''' Updates the Paid User status of the user '''
try:
# Convert email to lowercase for consistency
email = email.lower()
user = self.Get_User_Data(email)
if user:
# Use upsert to create the field if it doesn't exist
result = collection.update_one(
{"Email": email},
{"$set": {"Paid_User": paid_user_status}},
upsert=True
)
#logger.info(f"Updated Paid_User status for {email} to {paid_user_status}")
return True
else:
#logger.error(f"User not found when updating Paid_User status: {email}")
return False
except Exception as e:
#logger.error(f"Error updating Paid_User status for {email}: {str(e)}")
return False
def Update_Stripe_Customer_ID(self, email, stripe_customer_id):
''' Updates the Stripe Customer ID of the user '''
try:
# Convert email to lowercase for consistency
email = email.lower()
user = self.Get_User_Data(email)
if user:
# Use upsert to create the field if it doesn't exist
result = collection.update_one(
{"Email": email},
{"$set": {"Stripe_Customer_ID": stripe_customer_id}},
upsert=True
)
#logger.info(f"Updated Stripe Customer ID for {email} to {stripe_customer_id}")
return True
else:
#logger.error(f"User not found when updating Stripe Customer ID: {email}")
return False
except Exception as e:
#logger.error(f"Error updating Stripe Customer ID for {email}: {str(e)}")
return False
def Get_Stripe_Customer_ID(self, email):
''' Gets the Stripe Customer ID of the user '''
try:
# Convert email to lowercase for consistency
email = email.lower()
user = self.Get_User_Data(email)
if user:
return user.get('Stripe_Customer_ID')
else:
return None
except Exception as e:
#logger.error(f"Error getting Stripe Customer ID for {email}: {str(e)}")
return None
def Delete_Session(self, email):
''' Deletes the session of the user '''
try:
# Convert email to lowercase for consistency
email = email.lower()
result = Delete_Session_DB(email)
return result
except Exception as e:
return False
def Delete_Session_By_Date(self, email, date):
''' Deletes the session of the user for a specific date '''
try:
# Convert email to lowercase for consistency
email = email.lower()
result = Delete_Session_DB_By_Date(email, date)
return result
except Exception as e:
return False
# Fixed User class method
def Get_Credits_History(self, email, page, limit):
''' Getting user credits history with pagination '''
try:
# Convert email to lowercase for consistency
email = email.lower()
result = Get_Credits_History_DB(email, page, limit)
return result
except Exception as e:
return False
def Delete_Session_By_Platform(self, email,date, platform):
''' Deletes the session of the user for a specific platform '''
try:
# Convert email to lowercase for consistency
email = email.lower()
result = Delete_Session_DB_By_Platform(email,date, platform)
return result
except Exception as e:
return False
def Delete_Session_By_Date_Platform_Time(self, email,date, platform, time):
''' Deletes the session of the user for a specific platform and time '''
try:
# Convert email to lowercase for consistency
email = email.lower()
result = Delete_Session_DB_By_Date_Platform_Time(email,date, platform, time)
return result
except Exception as e:
return False
def Get_Models(self, email):
''' Gets the models available to the user based on their API keys '''
try:
# Convert email to lowercase for consistency
email = email.lower()
user = self.Get_User_Data(email)
if not user or 'API_Key' not in user:
return []
api_keys = user['API_Key']
all_models = []
# Get OpenAI models if API key exists
if api_keys.get('OpenAI'):
try:
openai_models = Get_Models_From_OpenAI(api_keys['OpenAI'])
if openai_models:
all_models.extend(openai_models)
except Exception as e:
#logger.error(f"Error getting OpenAI models: {e}")
pass
# Get Claude models if API key exists (Claude doesn't have a direct model listing API)
if api_keys.get('Claude'):
try:
# Add default Claude models
claude_models = ['claude-3-5-sonnet', 'claude-3-5-haiku']
all_models.extend(claude_models)
except Exception as e:
#logger.error(f"Error adding Claude models: {e}")
pass
# Get Gemini models if API key exists
if api_keys.get('Gemini'):
try:
gemini_models = Get_Models_From_Gemini(api_keys['Gemini'])
if gemini_models:
all_models.extend(gemini_models)
except Exception as e:
#logger.error(f"Error getting Gemini models: {e}")
pass
# Remove duplicates and return
return list(set(all_models)) if all_models else []
except Exception as e:
#logger.error(f"Error getting models for user {email}: {e}")
return []
def Get_Platforms_By_Date(self, email,date):
''' Gets the platforms of the user for a specific date '''
try:
# Convert email to lowercase for consistency
email = email.lower()
result = Get_Platforms_By_Date_DB(email,date)
return result
except Exception as e:
return False