Nattyboi commited on
Commit
73b6185
·
1 Parent(s): e2a962e

fixed questionaire retrieval

Browse files
Files changed (3) hide show
  1. streaksManagement.py +0 -1
  2. tokenManagement.py +0 -6
  3. utils.py +58 -34
streaksManagement.py CHANGED
@@ -45,7 +45,6 @@ def streaks_manager(db_uri: str, document: Dict) -> Union[bool, str]:
45
  collection.insert_one(document)
46
  return True
47
  else:
48
- print("user has a streak record")
49
  is_a_streak = check_date_streak(
50
  start_date=found_user['streak_dates'][-1],
51
  end_date=current_date
 
45
  collection.insert_one(document)
46
  return True
47
  else:
 
48
  is_a_streak = check_date_streak(
49
  start_date=found_user['streak_dates'][-1],
50
  end_date=current_date
tokenManagement.py CHANGED
@@ -123,28 +123,22 @@ def verify_access_token(db_uri: str, user_id: str, access_token: str) -> bool:
123
  collection = db["AccessToken"]
124
  docs = collection.find({"user_id":user_id})
125
  for doc in docs:
126
- print("doc=", doc,"user access token =" , access_token)
127
 
128
  if doc==None:
129
  return False
130
  else:
131
  if str(doc['_id']) == access_token:
132
- print("accesstoke is correct")
133
  if isexpired(doc['expire_at'])!=False:
134
- print("isexpired!=False")
135
  streaks_doc={}
136
  streaks_doc['user_id'] = str(user_id)
137
  streaks_manager(db_uri=db_uri,document=streaks_doc)
138
  pass
139
  else:
140
  streaks_doc={}
141
- print("isexpired!=True")
142
  streaks_doc['user_id'] = str(user_id)
143
  streaks_manager(db_uri=db_uri,document=streaks_doc)
144
  return True
145
  else:
146
- print("doc=", str(doc['_id']),"user access token =" , access_token)
147
- print("accesstoken is wrong")
148
  streaks_doc={}
149
  streaks_doc['user_id'] = str(user_id)
150
  streaks_manager(db_uri=db_uri,document=streaks_doc)
 
123
  collection = db["AccessToken"]
124
  docs = collection.find({"user_id":user_id})
125
  for doc in docs:
 
126
 
127
  if doc==None:
128
  return False
129
  else:
130
  if str(doc['_id']) == access_token:
 
131
  if isexpired(doc['expire_at'])!=False:
 
132
  streaks_doc={}
133
  streaks_doc['user_id'] = str(user_id)
134
  streaks_manager(db_uri=db_uri,document=streaks_doc)
135
  pass
136
  else:
137
  streaks_doc={}
 
138
  streaks_doc['user_id'] = str(user_id)
139
  streaks_manager(db_uri=db_uri,document=streaks_doc)
140
  return True
141
  else:
 
 
142
  streaks_doc={}
143
  streaks_doc['user_id'] = str(user_id)
144
  streaks_manager(db_uri=db_uri,document=streaks_doc)
utils.py CHANGED
@@ -225,47 +225,71 @@ def login_user(db_uri: str, db_name: str, collection_name: str, document: dict)
225
 
226
 
227
 
 
 
 
228
 
229
-
230
- def user_details_func(db_uri: str, document: dict) -> str:
231
- streaks_doc={}
 
 
 
 
 
 
 
 
 
232
 
233
  # Connect to MongoDB
234
  client = MongoClient(db_uri)
235
  db = client["crayonics"]
236
- collection = db["users"]
 
 
237
  streaks_collection = db["Streaks"]
238
  questionaire_collection = db["Questionaire"]
239
- # Insert the document
240
- doc = collection.find_one({"_id":ObjectId(document.get("user_id"))})
241
- if doc==None:
 
 
 
242
  return None
243
- else:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
244
  try:
245
- streaks_collection_doc = streaks_collection.find_one(filter={"user_id":document.get("user_id")})
246
- questionaire_collection_doc= questionaire_collection.find_one(filter={"user_id":document.get("user_id")})
247
- streaks_doc['user_id'] = document.get("user_id")
248
- streaks_manager(db_uri=db_uri,document=streaks_doc)
249
- streaks_collection_doc.pop("_id")
250
- doc['user_id'] = str(doc['_id'])
251
- doc.pop('_id')
252
- questionaire_collection_doc.pop('_id')
253
- questionaire_collection_doc.pop('user_id')
254
- doc.pop('password')
255
- streaks_collection_doc.pop('user_id')
256
- doc['streak_dates'] =streaks_collection_doc['streak_dates']
257
- doc['career_questions'] = questionaire_collection_doc
258
- return doc
259
- except:
260
- streaks_collection_doc = streaks_collection.find_one(filter={"user_id":document.get("user_id")})
261
- streaks_doc['user_id'] = document.get("user_id")
262
- streaks_manager(db_uri=db_uri,document=streaks_doc)
263
- streaks_collection_doc.pop("_id")
264
- doc['user_id'] = str(doc['_id'])
265
- doc.pop('_id')
266
- doc.pop('password')
267
- streaks_collection_doc.pop('user_id')
268
- doc['streak_dates'] =streaks_collection_doc['streak_dates']
269
- return doc
270
-
271
 
 
225
 
226
 
227
 
228
+ from pymongo import MongoClient
229
+ from bson.objectid import ObjectId
230
+ from typing import Dict, Optional
231
 
232
+ def user_details_func(db_uri: str, document: Dict) -> Optional[Dict]:
233
+ """
234
+ Retrieve and process user details from MongoDB collections.
235
+
236
+ Args:
237
+ db_uri (str): MongoDB connection URI
238
+ document (dict): Document containing user_id
239
+
240
+ Returns:
241
+ dict: Processed user details or None if user not found
242
+ """
243
+ streaks_doc = {}
244
 
245
  # Connect to MongoDB
246
  client = MongoClient(db_uri)
247
  db = client["crayonics"]
248
+
249
+ # Define collections
250
+ users_collection = db["users"]
251
  streaks_collection = db["Streaks"]
252
  questionaire_collection = db["Questionaire"]
253
+
254
+ # Find user document
255
+ user_id = document.get("user_id")
256
+ user_doc = users_collection.find_one({"_id": ObjectId(user_id)})
257
+
258
+ if not user_doc:
259
  return None
260
+
261
+ # Prepare base user document
262
+ user_doc['user_id'] = str(user_doc['_id'])
263
+ user_doc.pop('_id')
264
+ user_doc.pop('password', None) # Use default None in case password doesn't exist
265
+
266
+ # Get streaks data
267
+ streaks_collection_doc = streaks_collection.find_one({"user_id": user_id})
268
+ streaks_doc['user_id'] = user_id
269
+
270
+ # Call streaks_manager (assuming this function exists elsewhere)
271
+ streaks_manager(db_uri=db_uri, document=streaks_doc)
272
+
273
+ if streaks_collection_doc:
274
+ streaks_collection_doc.pop("_id", None)
275
+ streaks_collection_doc.pop("user_id", None)
276
+ user_doc['streak_dates'] = streaks_collection_doc.get('streak_dates', [])
277
+
278
+ # Try to get questionnaire data
279
+
280
+ questionaire_doc = questionaire_collection.find_one({"userId": user_id})
281
+ if questionaire_doc:
282
+ print(f"in questionaire retrieval:")
283
  try:
284
+ questionaire_doc.pop("_id", None)
285
+ questionaire_doc.pop("user_id", None)
286
+ user_doc['career_questions'] = questionaire_doc
287
+ except Exception as e:
288
+ # If questionnaire fails, continue with what we have
289
+ print(f"Error in questionaire retrieval: {str(e)}")
290
+ print(questionaire_doc)
291
+ pass
292
+ client.close()
293
+ return user_doc
294
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
295