Spaces:
Running
Running
fixed streaks issue
Browse files- streaksManagement.py +52 -8
- testes.py +6 -0
streaksManagement.py
CHANGED
@@ -1,6 +1,6 @@
|
|
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
|
@@ -17,7 +17,45 @@ def check_date_streak(start_date: str, end_date: str) -> bool:
|
|
17 |
end_date = datetime.fromisoformat(end_date)
|
18 |
|
19 |
# Compare the dates to check if they are consecutive (1 day apart)
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
def streaks_manager(db_uri: str, document: Dict) -> Union[bool, str]:
|
23 |
"""
|
@@ -38,6 +76,7 @@ def streaks_manager(db_uri: str, document: Dict) -> Union[bool, str]:
|
|
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:
|
@@ -52,18 +91,23 @@ def streaks_manager(db_uri: str, document: Dict) -> Union[bool, str]:
|
|
52 |
if is_a_streak:
|
53 |
print("its a streak guys")
|
54 |
# Extend existing streak
|
55 |
-
|
|
|
|
|
|
|
56 |
collection.update_one(
|
57 |
{"user_id": document.get("user_id")},
|
58 |
{"$set": {"streak_dates": dates}}
|
59 |
)
|
60 |
return True
|
61 |
else:
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
)
|
|
|
|
|
67 |
return "User Already Exists"
|
68 |
|
69 |
except Exception as e:
|
|
|
1 |
from pymongo import MongoClient
|
2 |
from datetime import datetime, timedelta
|
3 |
+
from typing import Dict, List, Union
|
4 |
|
5 |
def get_current_date() -> str:
|
6 |
# Get the current date and return it in YYYY-MM-DD format
|
|
|
17 |
end_date = datetime.fromisoformat(end_date)
|
18 |
|
19 |
# Compare the dates to check if they are consecutive (1 day apart)
|
20 |
+
if end_date - start_date == timedelta(days=0) or end_date - start_date == timedelta(days=1):
|
21 |
+
return True
|
22 |
+
else:
|
23 |
+
return False
|
24 |
+
|
25 |
+
def save_in_streaks_history(db_uri,document:dict={"user_id":"67c9b68678fbf39f4ed94e01","streaks_date":["2025-03-07"]}):
|
26 |
+
client = MongoClient(db_uri)
|
27 |
+
document= document.pop("_id",None)
|
28 |
+
try:
|
29 |
+
db = client["crayonics"]
|
30 |
+
collection = db["StreaksHistory"]
|
31 |
+
# Check for existing user
|
32 |
+
found_user = collection.find_one({"user_id": document.get('user_id')})
|
33 |
+
current_date = get_current_date()
|
34 |
+
if found_user is None:
|
35 |
+
# New user case
|
36 |
+
print("added new streak reset record")
|
37 |
+
document["streaks_records"] = {"date_of_streaks_reset":current_date,"streaks_dates":document["streaks_date"]}
|
38 |
+
document.pop("streaks_date")
|
39 |
+
collection.insert_one(document)
|
40 |
+
return True
|
41 |
+
else:
|
42 |
+
print("added another streak reset record")
|
43 |
+
current_record={"date_of_streaks_reset":current_date,"streaks_dates":document["streaks_date"]}
|
44 |
+
|
45 |
+
dates = found_user["streaks_records"] + [current_record]
|
46 |
+
collection.update_one(
|
47 |
+
{"user_id": document.get("user_id")},
|
48 |
+
{"$set": {"streaks_records": dates}}
|
49 |
+
)
|
50 |
+
return True
|
51 |
+
|
52 |
+
except Exception as e:
|
53 |
+
print(f"Error in creating a streaks reset record: {str(e)}")
|
54 |
+
return False
|
55 |
+
finally:
|
56 |
+
client.close()
|
57 |
+
|
58 |
+
|
59 |
|
60 |
def streaks_manager(db_uri: str, document: Dict) -> Union[bool, str]:
|
61 |
"""
|
|
|
76 |
# Check for existing user
|
77 |
found_user = collection.find_one({"user_id": document.get('user_id')})
|
78 |
current_date = get_current_date()
|
79 |
+
|
80 |
document['streak_dates'] = [current_date]
|
81 |
|
82 |
if found_user is None:
|
|
|
91 |
if is_a_streak:
|
92 |
print("its a streak guys")
|
93 |
# Extend existing streak
|
94 |
+
if not (current_date== found_user["streak_dates"][-1]):
|
95 |
+
dates = found_user["streak_dates"] + [current_date]
|
96 |
+
else:
|
97 |
+
dates=found_user["streak_dates"]
|
98 |
collection.update_one(
|
99 |
{"user_id": document.get("user_id")},
|
100 |
{"$set": {"streak_dates": dates}}
|
101 |
)
|
102 |
return True
|
103 |
else:
|
104 |
+
print(found_user,type(found_user))
|
105 |
+
# save_in_streaks_history(db_uri=db_uri,document=found_user)
|
106 |
+
# # Reset streak if not consecutive
|
107 |
+
# collection.find_one_and_replace(
|
108 |
+
# {"user_id": document.get('user_id')},
|
109 |
+
# document
|
110 |
+
# )
|
111 |
return "User Already Exists"
|
112 |
|
113 |
except Exception as e:
|
testes.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from streaksManagement import streaks_manager
|
2 |
+
|
3 |
+
|
4 |
+
streaks_manager(db_uri="mongodb+srv://groupcresearchseminar:[email protected]/?retryWrites=true&w=majority&appName=Cluster0",document={"user_id":"67c9b68678fbf39f4ed94e01"})
|
5 |
+
|
6 |
+
|