Spaces:
Running
Running
added questionaire to the user details
Browse files- streaksManagement.py +49 -44
- utils.py +26 -13
streaksManagement.py
CHANGED
@@ -1,13 +1,13 @@
|
|
1 |
-
|
2 |
from pymongo import MongoClient
|
3 |
-
from datetime import datetime,timedelta
|
|
|
4 |
|
5 |
-
def get_current_date():
|
6 |
# Get the current date and return it in YYYY-MM-DD format
|
7 |
return datetime.now().date().isoformat()
|
8 |
|
9 |
-
def get_another_date(day:int):
|
10 |
-
# Get the current date and add
|
11 |
tomorrow = datetime.now() + timedelta(days=day)
|
12 |
return tomorrow.date().isoformat()
|
13 |
|
@@ -19,52 +19,57 @@ def check_date_streak(start_date: str, end_date: str) -> bool:
|
|
19 |
# Compare the dates to check if they are consecutive (1 day apart)
|
20 |
return end_date - start_date == timedelta(days=1)
|
21 |
|
22 |
-
def streaks_manager(db_uri: str,
|
23 |
"""
|
24 |
-
|
25 |
|
26 |
-
|
27 |
-
db_uri
|
28 |
-
|
29 |
-
collection_name (str): Name of the collection.
|
30 |
-
document (dict): The document to insert.
|
31 |
|
32 |
Returns:
|
33 |
-
str:
|
34 |
"""
|
35 |
-
# Connect to MongoDB
|
36 |
client = MongoClient(db_uri)
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
|
|
|
|
43 |
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
client.close()
|
48 |
-
return True
|
49 |
-
else:
|
50 |
-
print("user has a streak record")
|
51 |
-
is_a_streak=check_date_streak(start_date=foundUser['streak_dates'][-1],end_date=get_current_date())
|
52 |
-
if is_a_streak:
|
53 |
-
print("its a streak guys")
|
54 |
-
dates = []
|
55 |
-
for d in foundUser["streak_dates"]:
|
56 |
-
dates.append(d)
|
57 |
-
dates.append(get_current_date())
|
58 |
-
collection.update_one(filter={"user_id":document.get("user_id")},update={
|
59 |
-
"$set":{"streak_dates":dates}
|
60 |
-
}
|
61 |
-
)
|
62 |
-
client.close()
|
63 |
return True
|
64 |
else:
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
|
|
|
|
1 |
from pymongo import MongoClient
|
2 |
+
from datetime import datetime, timedelta
|
3 |
+
from typing import Dict, Union
|
4 |
|
5 |
+
def get_current_date() -> str:
|
6 |
# Get the current date and return it in YYYY-MM-DD format
|
7 |
return datetime.now().date().isoformat()
|
8 |
|
9 |
+
def get_another_date(day: int) -> str:
|
10 |
+
# Get the current date and add specified days to it
|
11 |
tomorrow = datetime.now() + timedelta(days=day)
|
12 |
return tomorrow.date().isoformat()
|
13 |
|
|
|
19 |
# Compare the dates to check if they are consecutive (1 day apart)
|
20 |
return end_date - start_date == timedelta(days=1)
|
21 |
|
22 |
+
def streaks_manager(db_uri: str, document: Dict) -> Union[bool, str]:
|
23 |
"""
|
24 |
+
Manage user streaks in MongoDB.
|
25 |
|
26 |
+
Args:
|
27 |
+
db_uri: MongoDB connection string
|
28 |
+
document: Dictionary containing user data with 'user_id' key
|
|
|
|
|
29 |
|
30 |
Returns:
|
31 |
+
Union[bool, str]: True if successful, "User Already Exists" if streak is reset
|
32 |
"""
|
|
|
33 |
client = MongoClient(db_uri)
|
34 |
+
try:
|
35 |
+
db = client["crayonics"]
|
36 |
+
collection = db["Streaks"]
|
37 |
+
|
38 |
+
# Check for existing user
|
39 |
+
found_user = collection.find_one({"user_id": document.get('user_id')})
|
40 |
+
current_date = get_current_date()
|
41 |
+
document['streak_dates'] = [current_date]
|
42 |
|
43 |
+
if found_user is None:
|
44 |
+
# New user case
|
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
|
52 |
+
)
|
53 |
+
if is_a_streak:
|
54 |
+
print("its a streak guys")
|
55 |
+
# Extend existing streak
|
56 |
+
dates = found_user["streak_dates"] + [current_date]
|
57 |
+
collection.update_one(
|
58 |
+
{"user_id": document.get("user_id")},
|
59 |
+
{"$set": {"streak_dates": dates}}
|
60 |
+
)
|
61 |
+
return True
|
62 |
+
else:
|
63 |
+
# Reset streak if not consecutive
|
64 |
+
collection.find_one_and_replace(
|
65 |
+
{"user_id": document.get('user_id')},
|
66 |
+
document
|
67 |
+
)
|
68 |
+
return "User Already Exists"
|
69 |
+
|
70 |
+
except Exception as e:
|
71 |
+
print(f"Error: {str(e)}")
|
72 |
+
raise
|
73 |
+
finally:
|
74 |
+
client.close()
|
75 |
|
utils.py
CHANGED
@@ -231,23 +231,36 @@ def user_details_func(db_uri: str, document: dict) -> str:
|
|
231 |
db = client["crayonics"]
|
232 |
collection = db["users"]
|
233 |
streaks_collection = db["Streaks"]
|
234 |
-
questionaire_collection = db["
|
235 |
# Insert the document
|
236 |
doc = collection.find_one({"_id":ObjectId(document.get("user_id"))})
|
237 |
-
|
238 |
if doc==None:
|
239 |
return None
|
240 |
else:
|
241 |
streaks_collection_doc = streaks_collection.find_one(filter={"user_id":document.get("user_id")})
|
242 |
-
|
243 |
-
|
244 |
-
|
245 |
-
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
252 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
253 |
|
|
|
231 |
db = client["crayonics"]
|
232 |
collection = db["users"]
|
233 |
streaks_collection = db["Streaks"]
|
234 |
+
questionaire_collection = db["Questionaire"]
|
235 |
# Insert the document
|
236 |
doc = collection.find_one({"_id":ObjectId(document.get("user_id"))})
|
|
|
237 |
if doc==None:
|
238 |
return None
|
239 |
else:
|
240 |
streaks_collection_doc = streaks_collection.find_one(filter={"user_id":document.get("user_id")})
|
241 |
+
try:
|
242 |
+
questionaire_collection_doc= questionaire_collection.find_one(filter={"user_id":document.get("user_id")})
|
243 |
+
streaks_doc['user_id'] = document.get("user_id")
|
244 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
245 |
+
streaks_collection_doc.pop("_id")
|
246 |
+
doc['user_id'] = str(doc['_id'])
|
247 |
+
doc.pop('_id')
|
248 |
+
questionaire_collection_doc.pop('_id')
|
249 |
+
questionaire_collection_doc.pop('user_id')
|
250 |
+
doc.pop('password')
|
251 |
+
streaks_collection_doc.pop('user_id')
|
252 |
+
doc['streak_dates'] =streaks_collection_doc['streak_dates']
|
253 |
+
doc['career_questions'] = questionaire_collection_doc
|
254 |
+
return doc
|
255 |
+
except:
|
256 |
+
streaks_doc['user_id'] = document.get("user_id")
|
257 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
258 |
+
streaks_collection_doc.pop("_id")
|
259 |
+
doc['user_id'] = str(doc['_id'])
|
260 |
+
doc.pop('_id')
|
261 |
+
doc.pop('password')
|
262 |
+
streaks_collection_doc.pop('user_id')
|
263 |
+
doc['streak_dates'] =streaks_collection_doc['streak_dates']
|
264 |
+
return doc
|
265 |
+
|
266 |
|