Spaces:
Running
Running
added streaks feature
Browse files- app.py +0 -1
- streaksManagement.py +72 -0
- tokenManagement.py +19 -3
- utils.py +8 -1
app.py
CHANGED
@@ -336,7 +336,6 @@ def protected_route(authorization: str = Header(...)):
|
|
336 |
# Here, you would validate the token (e.g., check with a JWT library)
|
337 |
decoded_user_id,decoded_access_token = decode_jwt(token)
|
338 |
is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
|
339 |
-
print(decoded_user_id,decoded_access_token)
|
340 |
if is_valid != True: # Example check
|
341 |
raise HTTPException(status_code=401, detail="Invalid token")
|
342 |
|
|
|
336 |
# Here, you would validate the token (e.g., check with a JWT library)
|
337 |
decoded_user_id,decoded_access_token = decode_jwt(token)
|
338 |
is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
|
|
|
339 |
if is_valid != True: # Example check
|
340 |
raise HTTPException(status_code=401, detail="Invalid token")
|
341 |
|
streaksManagement.py
ADDED
@@ -0,0 +1,72 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 one day to it
|
11 |
+
tomorrow = datetime.now() + timedelta(days=day)
|
12 |
+
return tomorrow.date().isoformat()
|
13 |
+
|
14 |
+
def check_date_streak(start_date: str, end_date: str) -> bool:
|
15 |
+
# Convert ISO strings to datetime objects
|
16 |
+
start_date = datetime.fromisoformat(start_date)
|
17 |
+
end_date = datetime.fromisoformat(end_date)
|
18 |
+
|
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) -> str:
|
23 |
+
"""
|
24 |
+
Inserts a new document into the specified MongoDB collection.
|
25 |
+
|
26 |
+
Parameters:
|
27 |
+
db_uri (str): MongoDB connection URI.
|
28 |
+
db_name (str): Name of the database.
|
29 |
+
collection_name (str): Name of the collection.
|
30 |
+
document (dict): The document to insert.
|
31 |
+
|
32 |
+
Returns:
|
33 |
+
str: The ID of the inserted document.
|
34 |
+
"""
|
35 |
+
# Connect to MongoDB
|
36 |
+
client = MongoClient(db_uri)
|
37 |
+
db = client["crayonics"]
|
38 |
+
collection = db["Streaks"]
|
39 |
+
|
40 |
+
# Insert the document
|
41 |
+
foundUser = collection.find_one({"user_id":document.get('user_id')})
|
42 |
+
streak_dates=get_current_date()
|
43 |
+
|
44 |
+
document['streak_dates']= [streak_dates]
|
45 |
+
if foundUser==None:
|
46 |
+
result = collection.insert_one(document)
|
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 |
+
collection.find_one_and_replace(filter={"user_id":document.get('user_id')},replacement=document)
|
66 |
+
return "User Already Exists"
|
67 |
+
|
68 |
+
# Close the connection
|
69 |
+
|
70 |
+
|
71 |
+
|
72 |
+
# streaks_manager(db_uri="mongodb+srv://groupcresearchseminar:[email protected]/?retryWrites=true&w=majority&appName=Cluster0",document={"user_id":"67c9600773ef7350189a2d90"})
|
tokenManagement.py
CHANGED
@@ -3,8 +3,10 @@
|
|
3 |
import datetime
|
4 |
from bson import ObjectId
|
5 |
|
|
|
6 |
|
7 |
-
|
|
|
8 |
# Get the current date and time
|
9 |
current_date =datetime.datetime.now()
|
10 |
|
@@ -42,6 +44,9 @@ def create_accessToken(db_uri: str, user_id: str, refresh_token: str) -> str:
|
|
42 |
collection.find_one_and_delete({"refresh_token":refresh_token})
|
43 |
# Insert the document
|
44 |
result = collection.insert_one({"user_id":user_id,"refresh_token":refresh_token,"current_time":current_time,"expire_at":expire_at})
|
|
|
|
|
|
|
45 |
client.close()
|
46 |
return str(result.inserted_id)
|
47 |
|
@@ -73,6 +78,9 @@ def create_refreshToken(db_uri: str, user_id: str) -> str:
|
|
73 |
collection = db["RefreshToken"]
|
74 |
# Insert the document
|
75 |
result = collection.insert_one({"user_id":user_id,"current_time":current_time,"expire_at":expire_at,"previous_access_token":"None"})
|
|
|
|
|
|
|
76 |
client.close()
|
77 |
return str(result.inserted_id)
|
78 |
|
@@ -123,12 +131,20 @@ def verify_access_token(db_uri: str, user_id: str, access_token: str) -> bool:
|
|
123 |
return False
|
124 |
else:
|
125 |
if str(doc['_id']) == access_token:
|
126 |
-
if
|
|
|
|
|
|
|
127 |
return False
|
128 |
else:
|
|
|
|
|
|
|
129 |
return True
|
130 |
else:
|
131 |
-
|
|
|
|
|
132 |
return False
|
133 |
|
134 |
|
|
|
3 |
import datetime
|
4 |
from bson import ObjectId
|
5 |
|
6 |
+
from streaksManagement import streaks_manager
|
7 |
|
8 |
+
|
9 |
+
def isexpired(previous_date):
|
10 |
# Get the current date and time
|
11 |
current_date =datetime.datetime.now()
|
12 |
|
|
|
44 |
collection.find_one_and_delete({"refresh_token":refresh_token})
|
45 |
# Insert the document
|
46 |
result = collection.insert_one({"user_id":user_id,"refresh_token":refresh_token,"current_time":current_time,"expire_at":expire_at})
|
47 |
+
streaks_doc={}
|
48 |
+
streaks_doc['user_id'] = user_id
|
49 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
50 |
client.close()
|
51 |
return str(result.inserted_id)
|
52 |
|
|
|
78 |
collection = db["RefreshToken"]
|
79 |
# Insert the document
|
80 |
result = collection.insert_one({"user_id":user_id,"current_time":current_time,"expire_at":expire_at,"previous_access_token":"None"})
|
81 |
+
streaks_doc={}
|
82 |
+
streaks_doc['user_id'] = user_id
|
83 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
84 |
client.close()
|
85 |
return str(result.inserted_id)
|
86 |
|
|
|
131 |
return False
|
132 |
else:
|
133 |
if str(doc['_id']) == access_token:
|
134 |
+
if isexpired(doc['expire_at']):
|
135 |
+
streaks_doc={}
|
136 |
+
streaks_doc['user_id'] = user_id
|
137 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
138 |
return False
|
139 |
else:
|
140 |
+
streaks_doc={}
|
141 |
+
streaks_doc['user_id'] = user_id
|
142 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
143 |
return True
|
144 |
else:
|
145 |
+
streaks_doc={}
|
146 |
+
streaks_doc['user_id'] = user_id
|
147 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
148 |
return False
|
149 |
|
150 |
|
utils.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import requests
|
2 |
from pymongo import MongoClient
|
3 |
from password import *
|
|
|
4 |
|
5 |
def google_search(query, api_key, cx):
|
6 |
url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={api_key}&cx={cx}"
|
@@ -130,7 +131,6 @@ def create_user(db_uri: str, db_name: str, collection_name: str, document: dict)
|
|
130 |
client = MongoClient(db_uri)
|
131 |
db = client[db_name]
|
132 |
collection = db[collection_name]
|
133 |
-
|
134 |
# Insert the document
|
135 |
s = collection.find_one({"email":document.get('email')})
|
136 |
password = hash_password(document.get('password'))
|
@@ -138,6 +138,9 @@ def create_user(db_uri: str, db_name: str, collection_name: str, document: dict)
|
|
138 |
if s==None:
|
139 |
result = collection.insert_one(document)
|
140 |
client.close()
|
|
|
|
|
|
|
141 |
return str(result.inserted_id)
|
142 |
else:
|
143 |
client.close()
|
@@ -167,6 +170,7 @@ def create_questionaire(db_uri: str, db_name: str, collection_name: str, documen
|
|
167 |
# Insert the document
|
168 |
result = collection.insert_one(document)
|
169 |
client.close()
|
|
|
170 |
return str(result.inserted_id)
|
171 |
|
172 |
|
@@ -178,6 +182,7 @@ def create_questionaire(db_uri: str, db_name: str, collection_name: str, documen
|
|
178 |
|
179 |
|
180 |
def login_user(db_uri: str, db_name: str, collection_name: str, document: dict) -> str:
|
|
|
181 |
"""
|
182 |
Inserts a new document into the specified MongoDB collection.
|
183 |
|
@@ -204,6 +209,8 @@ def login_user(db_uri: str, db_name: str, collection_name: str, document: dict)
|
|
204 |
else:
|
205 |
|
206 |
if check_password(password=document['password'],hashed_password=s['password']):
|
|
|
|
|
207 |
client.close()
|
208 |
return str(s['_id'])
|
209 |
else:
|
|
|
1 |
import requests
|
2 |
from pymongo import MongoClient
|
3 |
from password import *
|
4 |
+
from streaksManagement import streaks_manager
|
5 |
|
6 |
def google_search(query, api_key, cx):
|
7 |
url = f"https://www.googleapis.com/customsearch/v1?q={query}&key={api_key}&cx={cx}"
|
|
|
131 |
client = MongoClient(db_uri)
|
132 |
db = client[db_name]
|
133 |
collection = db[collection_name]
|
|
|
134 |
# Insert the document
|
135 |
s = collection.find_one({"email":document.get('email')})
|
136 |
password = hash_password(document.get('password'))
|
|
|
138 |
if s==None:
|
139 |
result = collection.insert_one(document)
|
140 |
client.close()
|
141 |
+
streaks_doc={}
|
142 |
+
streaks_doc['user_id'] = result.inserted_id
|
143 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
144 |
return str(result.inserted_id)
|
145 |
else:
|
146 |
client.close()
|
|
|
170 |
# Insert the document
|
171 |
result = collection.insert_one(document)
|
172 |
client.close()
|
173 |
+
|
174 |
return str(result.inserted_id)
|
175 |
|
176 |
|
|
|
182 |
|
183 |
|
184 |
def login_user(db_uri: str, db_name: str, collection_name: str, document: dict) -> str:
|
185 |
+
streaks_doc={}
|
186 |
"""
|
187 |
Inserts a new document into the specified MongoDB collection.
|
188 |
|
|
|
209 |
else:
|
210 |
|
211 |
if check_password(password=document['password'],hashed_password=s['password']):
|
212 |
+
streaks_doc['user_id'] = s.get("_id")
|
213 |
+
streaks_manager(db_uri=db_uri,document=streaks_doc)
|
214 |
client.close()
|
215 |
return str(s['_id'])
|
216 |
else:
|