Spaces:
Running
Running
added a new streaks feature
Browse files- streaksManagement.py +38 -37
- testes.py +0 -6
streaksManagement.py
CHANGED
|
@@ -22,38 +22,40 @@ def check_date_streak(start_date: str, end_date: str) -> bool:
|
|
| 22 |
else:
|
| 23 |
return False
|
| 24 |
|
| 25 |
-
def save_in_streaks_history(db_uri,document
|
| 26 |
client = MongoClient(db_uri)
|
| 27 |
-
document
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 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 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
|
| 59 |
|
|
@@ -101,13 +103,12 @@ def streaks_manager(db_uri: str, document: Dict) -> Union[bool, str]:
|
|
| 101 |
)
|
| 102 |
return True
|
| 103 |
else:
|
| 104 |
-
|
| 105 |
-
#
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
# )
|
| 111 |
return "User Already Exists"
|
| 112 |
|
| 113 |
except Exception as e:
|
|
|
|
| 22 |
else:
|
| 23 |
return False
|
| 24 |
|
| 25 |
+
def save_in_streaks_history(db_uri,document):
|
| 26 |
client = MongoClient(db_uri)
|
| 27 |
+
document.pop("_id",None)
|
| 28 |
+
print(document)
|
| 29 |
+
db = client["crayonics"]
|
| 30 |
+
collection = db["StreaksHistory"]
|
| 31 |
+
# Check for existing user
|
| 32 |
+
found_user = collection.find_one({"user_id": document['user_id']})
|
| 33 |
+
current_date = get_current_date()
|
| 34 |
+
# try:
|
| 35 |
+
if found_user is None:
|
| 36 |
+
# New user case
|
| 37 |
+
print("added new streak reset record")
|
| 38 |
+
document["streaks_records"] = [{"date_of_streak_reset":current_date,"streak_dates":document["streak_dates"]}]
|
| 39 |
+
document.pop("streak_dates")
|
| 40 |
+
collection.insert_one(document)
|
| 41 |
+
return True
|
| 42 |
+
else:
|
| 43 |
+
print("added another streak reset record")
|
| 44 |
+
current_record={"date_of_streaks_reset":current_date,"streak_dates":document["streak_dates"]}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
dates = found_user["streaks_records"] + [current_record]
|
| 47 |
+
collection.update_one(
|
| 48 |
+
{"user_id": document.get("user_id")},
|
| 49 |
+
{"$set": {"streaks_records": dates}}
|
| 50 |
+
)
|
| 51 |
+
return True
|
| 52 |
+
|
| 53 |
+
# except Exception as e:
|
| 54 |
+
# print(f"Error in creating a streaks reset record: {str(e)}")
|
| 55 |
+
# print(document,found_user)
|
| 56 |
+
# return False
|
| 57 |
+
# finally:
|
| 58 |
+
# client.close()
|
| 59 |
|
| 60 |
|
| 61 |
|
|
|
|
| 103 |
)
|
| 104 |
return True
|
| 105 |
else:
|
| 106 |
+
save_in_streaks_history(db_uri=db_uri,document=found_user)
|
| 107 |
+
# Reset streak if not consecutive
|
| 108 |
+
collection.find_one_and_replace(
|
| 109 |
+
{"user_id": document.get('user_id')},
|
| 110 |
+
document
|
| 111 |
+
)
|
|
|
|
| 112 |
return "User Already Exists"
|
| 113 |
|
| 114 |
except Exception as e:
|
testes.py
CHANGED
|
@@ -1,6 +0,0 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|