Nattyboi commited on
Commit
bbd997e
·
1 Parent(s): 27ff250

updated codebase to make it faster

Browse files
app.py CHANGED
@@ -9,86 +9,9 @@ app = FastAPI()
9
  app.mount('/gamification',gamification)
10
 
11
 
12
- class UserBody(BaseModel):
13
- firstName: Optional[str] = None
14
- lastName: Optional[str] = None
15
- email:str
16
- password:str
17
-
18
- class AiAnalysis(BaseModel):
19
- query:str
20
-
21
- class Token(BaseModel):
22
- refreshToken:str
23
-
24
-
25
- class RecommendedCourse(BaseModel):
26
- courseTitle:Optional[str]=None
27
- courseLink:Optional[str]=None
28
- duration:Optional[str]=None
29
- courseLevel:Optional[str]=None
30
- interimRoleBenefit:Optional[str]=None
31
- dreamRoleBenefit:Optional[str]=None
32
- courseDescription:Optional[str]=None
33
- courseProvider:Optional[str]=None
34
- class UserCourse(BaseModel):
35
- employmentStatus:str
36
- interimRole:bool
37
- interimRoleOptions:Optional[List[str]]=None
38
- dreamRole:str
39
- motivation:str
40
- learningPreference:str
41
- timeCommitmentPerDay:str
42
- challenges:list
43
- timeframeToAchieveDreamRole:str
44
- recommendedCourses: Optional[List[RecommendedCourse]]=None
45
-
46
- class LeaderBoardRanking(BaseModel):
47
- userId:str
48
- firstName:str
49
- lastName:str
50
- totalpoints:float
51
- lastUpdated:datetime
52
- careerPath:str
53
- class Config:
54
- json_encoder ={
55
- ObjectId:str
56
- }
57
-
58
-
59
-
60
- class CourseRecommendation(BaseModel):
61
- courseName: str
62
- completionTime: str
63
-
64
- def extract_course_info(text: str) -> CourseRecommendation:
65
- # Example regex patterns – adjust these as needed based on the response format.
66
- course_pattern =r'"coursename":\s*"([^"]+)"'
67
- time_pattern = r"(\d+\s*-\s*\d+\s*months)"
68
-
69
- course_match = re.search(course_pattern, text)
70
- time_match = re.search(time_pattern, text)
71
-
72
- coursename = course_match.group(1).strip() if course_match else "Unknown"
73
- completiontime = time_match.group(0).strip() if time_match else "Unknown"
74
 
75
- return CourseRecommendation(courseName=coursename, completionTime=completiontime)
76
 
77
 
78
-
79
- def extract_provider(url):
80
- # Parse the URL
81
- parsed_url = urlparse(url)
82
-
83
- # Extract domain and split it to get the main part
84
- domain = parsed_url.netloc.split('.')[1]
85
-
86
- # Extract course name
87
- match = re.search(r'/course/([^/]+)/', url)
88
- course_name = match.group(1) if match else "Not found"
89
-
90
- return domain
91
-
92
  @app.get("/courses",tags=["Scrape"])
93
  def get_course(query):
94
  # Example search query
@@ -116,27 +39,6 @@ def get_course(query):
116
 
117
 
118
 
119
- def get_course_func(query):
120
- # Example search query
121
- results = google_search(query, API_KEY, CX)
122
- content=[]
123
-
124
- if results:
125
- for item in results.get('items', []):
126
- title = item.get('title')
127
- link = item.get('link')
128
- snippet = item.get('snippet')
129
- provider = extract_provider(link)
130
-
131
- content_structure={}
132
- content_structure["courseTitle"]=title
133
- content_structure["courseLink"]=link
134
- content_structure["courseSnippet"]= snippet
135
- content_structure["scrapedCourseDetails"]= scrapeCourse(url=link)
136
- content.append(content_structure)
137
-
138
- return content
139
-
140
 
141
 
142
  @app.post("/ai/upload",tags=["AI"])
@@ -144,7 +46,7 @@ async def upload_file(file: UploadFile = File(...),authorization: str = Header(.
144
  # Extract the token from the Authorization header (Bearer token)
145
  token = authorization.split("Bearer ")[-1]
146
 
147
- # Here, you would validate the token (e.g., check with a JWT library)
148
  decoded_user_id,decoded_access_token = decode_jwt(token)
149
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
150
  if is_valid != True: # Example check
@@ -177,7 +79,7 @@ async def upload_file(file: UploadFile = File(...),authorization: str = Header(.
177
  docs = generate_embedding_for_user_resume(data=sentences,user_id=file.filename)
178
  response= insert_embeddings_into_pinecone_database(doc=docs,api_key=PINECONE_API_KEY,name_space=decoded_user_id)
179
 
180
- return {" name": file.filename,"response":str(response) }
181
 
182
 
183
 
@@ -186,7 +88,7 @@ def ask_ai_about_resume(req:AiAnalysis,authorization: str = Header(...)):
186
  # Retrieve context from your vector database
187
  token = authorization.split("Bearer ")[-1]
188
 
189
- # Here, you would validate the token (e.g., check with a JWT library)
190
  decoded_user_id,decoded_access_token = decode_jwt(token)
191
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
192
  if is_valid != True: # Example check
@@ -224,10 +126,10 @@ def ask_ai_to_recommnd_courses(request:UserCourse,authorization:str=Header(...))
224
  # Extract the token from the Authorization header (Bearer token)
225
  token = authorization.split("Bearer ")[-1]
226
 
227
- # Here, you would validate the token (e.g., check with a JWT library)
228
  decoded_user_id,decoded_access_token = decode_jwt(token)
229
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
230
- if is_valid != True: # Example check
231
  raise HTTPException(status_code=401, detail="Invalid token")
232
 
233
  # Ensure that an event loop is present in this thread.
@@ -241,24 +143,24 @@ def ask_ai_to_recommnd_courses(request:UserCourse,authorization:str=Header(...))
241
  # client = genai.Client(api_key=GEMINI_API_KEY)
242
 
243
  # response = client.models.generate_content(
244
- # model="gemini-2.0-flash",
245
- # contents=f"""
246
- # please respond with a JSON object that contains the following keys as a response:
247
- # - "coursename": the name of the recommended course,
248
- # - "completiontime": an estimate of how long it would take to complete the course.
249
- # Do not include any extra text.
250
- # Recommend a course using this information below :
251
- # Which of the following best describes you?: {request.employmentStatus}
252
- # Would you like to prepare for an interim role to gain experience and income while pursuing your dream job?: {request.interimRole}
253
- # What is your desired role?: {request.dreamRole}
254
- # Why do you want to achieve this desired role?: {request.motivation}
255
- # How do you prefer to learn new skills?: {request.learningPreference}
256
- # How many hours per day can you dedicate to learning?: {request.timeCommitmentPerDay}
257
- # What are the biggest challenges or obstacles you face in reaching your dream role?: {request.challenges}
258
- # What is your ideal timeframe for achieving your dream role?: {request.timeframeToAchieveDreamRole}
259
 
260
 
261
- # """
262
  # )
263
  questions=request.model_dump()
264
  questions['userId']=decoded_user_id
@@ -324,7 +226,7 @@ def refresh_access_token(refresh_token:Token, authorization: str = Header(...)):
324
 
325
  token = authorization.split("Bearer ")[-1]
326
 
327
- # Here, you would validate the token (e.g., check with a JWT library)
328
  decoded_user_id,decoded_access_token = decode_jwt(token)
329
  is_valid = verify_refresh_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token,refresh_token=refresh_token.refreshToken)
330
  if is_valid != True: # Example check
@@ -340,7 +242,7 @@ def get_user_details(authorization: str = Header(...)):
340
  # Extract the token from the Authorization header (Bearer token)
341
  token = authorization.split("Bearer ")[-1]
342
 
343
- # Here, you would validate the token (e.g., check with a JWT library)
344
  decoded_user_id,decoded_access_token = decode_jwt(token)
345
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
346
  if is_valid != True: # Example check
@@ -357,7 +259,7 @@ def protected_route(authorization: str = Header(...)):
357
  # Extract the token from the Authorization header (Bearer token)
358
  token = authorization.split("Bearer ")[-1]
359
 
360
- # Here, you would validate the token (e.g., check with a JWT library)
361
  decoded_user_id,decoded_access_token = decode_jwt(token)
362
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
363
  if is_valid != True: # Example check
 
9
  app.mount('/gamification',gamification)
10
 
11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
 
13
 
14
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
  @app.get("/courses",tags=["Scrape"])
16
  def get_course(query):
17
  # Example search query
 
39
 
40
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
 
44
  @app.post("/ai/upload",tags=["AI"])
 
46
  # Extract the token from the Authorization header (Bearer token)
47
  token = authorization.split("Bearer ")[-1]
48
 
49
+
50
  decoded_user_id,decoded_access_token = decode_jwt(token)
51
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
52
  if is_valid != True: # Example check
 
79
  docs = generate_embedding_for_user_resume(data=sentences,user_id=file.filename)
80
  response= insert_embeddings_into_pinecone_database(doc=docs,api_key=PINECONE_API_KEY,name_space=decoded_user_id)
81
 
82
+ return {"name": file.filename,"response":str(response) }
83
 
84
 
85
 
 
88
  # Retrieve context from your vector database
89
  token = authorization.split("Bearer ")[-1]
90
 
91
+
92
  decoded_user_id,decoded_access_token = decode_jwt(token)
93
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
94
  if is_valid != True: # Example check
 
126
  # Extract the token from the Authorization header (Bearer token)
127
  token = authorization.split("Bearer ")[-1]
128
 
129
+
130
  decoded_user_id,decoded_access_token = decode_jwt(token)
131
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
132
+ if is_valid != True:
133
  raise HTTPException(status_code=401, detail="Invalid token")
134
 
135
  # Ensure that an event loop is present in this thread.
 
143
  # client = genai.Client(api_key=GEMINI_API_KEY)
144
 
145
  # response = client.models.generate_content(
146
+ # model="gemini-2.0-flash",
147
+ # contents=f"""
148
+ # please respond with a JSON object that contains the following keys as a response:
149
+ # - "coursename": the name of the recommended course,
150
+ # - "completiontime": an estimate of how long it would take to complete the course.
151
+ # Do not include any extra text.
152
+ # Recommend a course using this information below :
153
+ # Which of the following best describes you?: {request.employmentStatus}
154
+ # Would you like to prepare for an interim role to gain experience and income while pursuing your dream job?: {request.interimRole}
155
+ # What is your desired role?: {request.dreamRole}
156
+ # Why do you want to achieve this desired role?: {request.motivation}
157
+ # How do you prefer to learn new skills?: {request.learningPreference}
158
+ # How many hours per day can you dedicate to learning?: {request.timeCommitmentPerDay}
159
+ # What are the biggest challenges or obstacles you face in reaching your dream role?: {request.challenges}
160
+ # What is your ideal timeframe for achieving your dream role?: {request.timeframeToAchieveDreamRole}
161
 
162
 
163
+ # """
164
  # )
165
  questions=request.model_dump()
166
  questions['userId']=decoded_user_id
 
226
 
227
  token = authorization.split("Bearer ")[-1]
228
 
229
+
230
  decoded_user_id,decoded_access_token = decode_jwt(token)
231
  is_valid = verify_refresh_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token,refresh_token=refresh_token.refreshToken)
232
  if is_valid != True: # Example check
 
242
  # Extract the token from the Authorization header (Bearer token)
243
  token = authorization.split("Bearer ")[-1]
244
 
245
+
246
  decoded_user_id,decoded_access_token = decode_jwt(token)
247
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
248
  if is_valid != True: # Example check
 
259
  # Extract the token from the Authorization header (Bearer token)
260
  token = authorization.split("Bearer ")[-1]
261
 
262
+
263
  decoded_user_id,decoded_access_token = decode_jwt(token)
264
  is_valid = verify_access_token(db_uri=MONGO_URI, user_id=decoded_user_id, access_token=decoded_access_token)
265
  if is_valid != True: # Example check
controller/imports.py CHANGED
@@ -2,15 +2,15 @@ from io import BytesIO
2
  from dotenv import load_dotenv
3
  import os
4
  from gamification.objects import SimpleIndividualUserLevel
5
- from utils import *
6
  from fastapi import FastAPI, File, HTTPException, Header, UploadFile,status
7
- from tokenManagement import *
8
- from jwtcoding import *
9
  from fastapi.responses import JSONResponse
10
  import docx
11
  import fitz
12
  from gamification.routes import gamification
13
- from scraper import scrapeCourse
14
  import asyncio
15
  from google import genai
16
  from typing import Optional,List
@@ -37,4 +37,111 @@ CX = os.getenv("SEARCH_ENGINE_ID")
37
  API_KEY = os.getenv("GOOGLE_API_KEY")
38
  PINECONE_API_KEY=os.getenv("PINECONE_API_KEY")
39
  GEMINI_API_KEY=os.getenv("GEMINI_API_KEY")
40
- MONGO_URI=os.getenv("MONGO_URI")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2
  from dotenv import load_dotenv
3
  import os
4
  from gamification.objects import SimpleIndividualUserLevel
5
+ from controller.utils import *
6
  from fastapi import FastAPI, File, HTTPException, Header, UploadFile,status
7
+ from controller.tokenManagement import *
8
+ from controller.jwtcoding import *
9
  from fastapi.responses import JSONResponse
10
  import docx
11
  import fitz
12
  from gamification.routes import gamification
13
+ from controller.scraper import scrapeCourse
14
  import asyncio
15
  from google import genai
16
  from typing import Optional,List
 
37
  API_KEY = os.getenv("GOOGLE_API_KEY")
38
  PINECONE_API_KEY=os.getenv("PINECONE_API_KEY")
39
  GEMINI_API_KEY=os.getenv("GEMINI_API_KEY")
40
+ MONGO_URI=os.getenv("MONGO_URI")
41
+
42
+ class UserBody(BaseModel):
43
+ firstName: Optional[str] = None
44
+ lastName: Optional[str] = None
45
+ email:str
46
+ password:str
47
+
48
+ class AiAnalysis(BaseModel):
49
+ query:str
50
+
51
+ class Token(BaseModel):
52
+ refreshToken:str
53
+
54
+
55
+ class RecommendedCourse(BaseModel):
56
+ courseTitle:Optional[str]=None
57
+ courseLink:Optional[str]=None
58
+ duration:Optional[str]=None
59
+ courseLevel:Optional[str]=None
60
+ interimRoleBenefit:Optional[str]=None
61
+ dreamRoleBenefit:Optional[str]=None
62
+ courseDescription:Optional[str]=None
63
+ courseProvider:Optional[str]=None
64
+ class UserCourse(BaseModel):
65
+ employmentStatus:str
66
+ interimRole:bool
67
+ interimRoleOptions:Optional[List[str]]=None
68
+ dreamRole:str
69
+ motivation:str
70
+ learningPreference:str
71
+ timeCommitmentPerDay:str
72
+ challenges:list
73
+ timeframeToAchieveDreamRole:str
74
+ recommendedCourses: Optional[List[RecommendedCourse]]=None
75
+
76
+ class LeaderBoardRanking(BaseModel):
77
+ userId:str
78
+ firstName:str
79
+ lastName:str
80
+ totalpoints:float
81
+ lastUpdated:datetime
82
+ careerPath:str
83
+ class Config:
84
+ json_encoder ={
85
+ ObjectId:str
86
+ }
87
+
88
+
89
+
90
+ class CourseRecommendation(BaseModel):
91
+ courseName: str
92
+ completionTime: str
93
+
94
+
95
+
96
+
97
+
98
+
99
+ def extract_provider(url):
100
+ # Parse the URL
101
+ parsed_url = urlparse(url)
102
+
103
+ # Extract domain and split it to get the main part
104
+ domain = parsed_url.netloc.split('.')[1]
105
+
106
+ # Extract course name
107
+ match = re.search(r'/course/([^/]+)/', url)
108
+ course_name = match.group(1) if match else "Not found"
109
+
110
+ return domain
111
+
112
+ def get_course_func(query):
113
+ # Example search query
114
+ results = google_search(query, API_KEY, CX)
115
+ content=[]
116
+
117
+ if results:
118
+ for item in results.get('items', []):
119
+ title = item.get('title')
120
+ link = item.get('link')
121
+ snippet = item.get('snippet')
122
+ provider = extract_provider(link)
123
+
124
+ content_structure={}
125
+ content_structure["courseTitle"]=title
126
+ content_structure["courseLink"]=link
127
+ content_structure["courseSnippet"]= snippet
128
+ content_structure["scrapedCourseDetails"]= scrapeCourse(url=link)
129
+ content.append(content_structure)
130
+
131
+ return content
132
+
133
+
134
+ def extract_course_info(text: str) -> CourseRecommendation:
135
+ # Example regex patterns – adjust these as needed based on the response format.
136
+ course_pattern =r'"coursename":\s*"([^"]+)"'
137
+ time_pattern = r"(\d+\s*-\s*\d+\s*months)"
138
+
139
+ course_match = re.search(course_pattern, text)
140
+ time_match = re.search(time_pattern, text)
141
+
142
+ coursename = course_match.group(1).strip() if course_match else "Unknown"
143
+ completiontime = time_match.group(0).strip() if time_match else "Unknown"
144
+
145
+ return CourseRecommendation(courseName=coursename, completionTime=completiontime)
146
+
147
+
controller/scraper.py CHANGED
@@ -3,26 +3,14 @@ def scrapeCourse(url):
3
  from bs4 import BeautifulSoup
4
  webcontent=[]
5
 
6
- # URL of the page you want to scrape
7
-
8
-
9
- # Send a GET request to fetch the raw HTML content
10
  response = requests.get(url)
11
 
12
- # Check if the request was successful
13
  if response.status_code == 200:
14
- # Parse the HTML content using BeautifulSoup
15
  soup = BeautifulSoup(response.text, 'html.parser')
16
-
17
- # Find the content based on the given CSS selector
18
  selector = "#main-content-anchor > div.paid-course-landing-page__body > div > div.ud-text-sm.component-margin.styles--description--AfVWV > div > div > div > div:nth-child(1) > ul"
19
  content = soup.select(selector)
20
-
21
- # Check if any elements are found
22
  if content:
23
- # Extract and print the text content from the first matched element
24
- for item in content[0].find_all('li'): # Assuming the list items <li> are the ones you're interested in
25
- # print(item.get_text(strip=True))
26
 
27
  webcontent.append(item.get_text(strip=True))
28
  return webcontent
 
3
  from bs4 import BeautifulSoup
4
  webcontent=[]
5
 
 
 
 
 
6
  response = requests.get(url)
7
 
 
8
  if response.status_code == 200:
 
9
  soup = BeautifulSoup(response.text, 'html.parser')
 
 
10
  selector = "#main-content-anchor > div.paid-course-landing-page__body > div > div.ud-text-sm.component-margin.styles--description--AfVWV > div > div > div > div:nth-child(1) > ul"
11
  content = soup.select(selector)
 
 
12
  if content:
13
+ for item in content[0].find_all('li'):
 
 
14
 
15
  webcontent.append(item.get_text(strip=True))
16
  return webcontent
controller/tokenManagement.py CHANGED
@@ -4,8 +4,55 @@ import datetime
4
  from bson import ObjectId
5
  from concurrent.futures import ThreadPoolExecutor
6
  executor = ThreadPoolExecutor(max_workers=5)
7
- from streaksManagement import streaks_manager
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  def isexpired(previous_date):
11
  # Get the current date and time
@@ -38,19 +85,16 @@ def create_accessToken(db_uri: str, user_id: str, refresh_token: str) -> str:
38
  Returns:
39
  str: The ID of the inserted document.
40
  """
41
- # Connect to MongoDB
42
  client = MongoClient(db_uri)
43
  db = client["crayonics"]
44
  collection = db["AccessToken"]
45
  collection.find_one_and_delete({"refresh_token":refresh_token})
46
- # Insert the document
47
  result = collection.insert_one({"user_id":user_id,"refresh_token":refresh_token,"current_time":current_time,"expire_at":expire_at})
48
-
49
  client.close()
50
  return str(result.inserted_id)
51
 
52
-
53
- # Close the connection
54
 
55
 
56
 
@@ -71,11 +115,9 @@ def create_refreshToken(db_uri: str, user_id: str) -> str:
71
  str: The ID of the inserted document.
72
  """
73
 
74
- # Connect to MongoDB
75
  client = MongoClient(db_uri)
76
  db = client["crayonics"]
77
  collection = db["RefreshToken"]
78
- # Insert the document
79
  result = collection.insert_one({"user_id":user_id,"current_time":current_time,"expire_at":expire_at,"previous_access_token":"None"})
80
  streaks_doc={}
81
  streaks_doc['user_id'] = str(user_id)
@@ -111,44 +153,19 @@ def update_refreshTokenWithPreviouslyUsedAccessToken(db_uri: str, refresh_token:
111
  except:
112
  return False
113
 
114
- from pymongo import MongoClient
115
  def verify_access_token(db_uri: str, user_id: str, access_token: str) -> bool:
 
116
 
117
- current_time = datetime.datetime.now()
118
- expire_at = current_time + datetime.timedelta(minutes=15)
119
- """
 
 
 
 
 
120
 
121
- """
122
- # Connect to MongoDB
123
- client = MongoClient(db_uri)
124
- db = client["crayonics"]
125
- collection = db["AccessToken"]
126
- docs = collection.find({"user_id":user_id})
127
- for doc in docs:
128
 
129
- if doc==None:
130
- return False
131
- else:
132
- if str(doc['_id']) == access_token:
133
- if isexpired(doc['expire_at'])!=False:
134
- streaks_doc={}
135
- streaks_doc['user_id'] = str(user_id)
136
- # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
137
- streaks_manager(db_uri=db_uri,document=streaks_doc)
138
- pass
139
- else:
140
- streaks_doc={}
141
- streaks_doc['user_id'] = str(user_id)
142
- # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
143
- streaks_manager(db_uri=db_uri,document=streaks_doc)
144
- return True
145
- else:
146
- streaks_doc={}
147
- streaks_doc['user_id'] = str(user_id)
148
- # executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
149
- streaks_manager(db_uri=db_uri,document=streaks_doc)
150
- pass
151
- return False
152
 
153
  def verify_refresh_access_token(db_uri: str, user_id: str, access_token: str,refresh_token:str) -> bool:
154
 
 
4
  from bson import ObjectId
5
  from concurrent.futures import ThreadPoolExecutor
6
  executor = ThreadPoolExecutor(max_workers=5)
7
+ from controller.streaksManagement import streaks_manager
8
+ from pymongo import MongoClient
9
+ from cryptography.fernet import Fernet
10
+ from dotenv import load_dotenv
11
+ import redis
12
+ import os
13
+ load_dotenv()
14
+
15
+
16
+ REDIS_HOST = os.getenv('REDIS_HOST', 'localhost')
17
+ REDIS_PORT = int(os.getenv('REDIS_PORT', 6379))
18
+ REDIS_PASSWORD = os.getenv('REDIS_PASSWORD', None)
19
+ FERNET_SECRET_KEY = os.getenv('FERNET_SECRET_KEY')
20
+ REDIS_USERNAME=os.getenv('REDIS_USERNAME')
21
+ fernet = Fernet(FERNET_SECRET_KEY)
22
+
23
+ r = redis.StrictRedis(
24
+ host=REDIS_HOST,
25
+ port=REDIS_PORT,
26
+ password=REDIS_PASSWORD,
27
+ username=REDIS_USERNAME,
28
+ db=0,
29
+ decode_responses=True
30
+ )
31
+
32
+
33
+
34
+
35
+
36
+
37
+
38
+ # Function to store an access token in Redis with an expiration time
39
+ def store_access_token(user_id: str, access_token: str, expiration_time_seconds: int = 4400):
40
+ """Store the access token in Redis after encrypting it."""
41
+
42
+
43
+ # Store the encrypted token in Redis with expiration time
44
+ r.setex(f"{user_id}.{access_token}", expiration_time_seconds,value=access_token)
45
 
46
+ # Function to check if an access token is valid
47
+ def check_cache_for_accesstokens(user_id: str, access_token: str):
48
+ """Check if the access token is valid."""
49
+
50
+ token_value = r.get(f"{user_id}.{access_token}")
51
+
52
+ if token_value:
53
+ return True
54
+ else:
55
+ return False
56
 
57
  def isexpired(previous_date):
58
  # Get the current date and time
 
85
  Returns:
86
  str: The ID of the inserted document.
87
  """
 
88
  client = MongoClient(db_uri)
89
  db = client["crayonics"]
90
  collection = db["AccessToken"]
91
  collection.find_one_and_delete({"refresh_token":refresh_token})
 
92
  result = collection.insert_one({"user_id":user_id,"refresh_token":refresh_token,"current_time":current_time,"expire_at":expire_at})
93
+ store_access_token(user_id=user_id,access_token=str(result.inserted_id))
94
  client.close()
95
  return str(result.inserted_id)
96
 
97
+
 
98
 
99
 
100
 
 
115
  str: The ID of the inserted document.
116
  """
117
 
 
118
  client = MongoClient(db_uri)
119
  db = client["crayonics"]
120
  collection = db["RefreshToken"]
 
121
  result = collection.insert_one({"user_id":user_id,"current_time":current_time,"expire_at":expire_at,"previous_access_token":"None"})
122
  streaks_doc={}
123
  streaks_doc['user_id'] = str(user_id)
 
153
  except:
154
  return False
155
 
 
156
  def verify_access_token(db_uri: str, user_id: str, access_token: str) -> bool:
157
+ is_valid = check_cache_for_accesstokens(user_id=user_id,access_token=access_token)
158
 
159
+ if is_valid==True:
160
+ streaks_doc={}
161
+ streaks_doc['user_id'] = str(user_id)
162
+ executor.submit(streaks_manager,db_uri=db_uri,document=streaks_doc)
163
+ return True
164
+ # Connect to MongoDB
165
+ else:
166
+ return False
167
 
 
 
 
 
 
 
 
168
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169
 
170
  def verify_refresh_access_token(db_uri: str, user_id: str, access_token: str,refresh_token:str) -> bool:
171
 
controller/utils.py CHANGED
@@ -8,8 +8,8 @@ import requests
8
  from pymongo import MongoClient
9
  from gamification.logic import create_points_func
10
  from gamification.objects import PlatformEngagement, Points
11
- from password import *
12
- from streaksManagement import streaks_manager
13
  from concurrent.futures import ThreadPoolExecutor
14
  executor = ThreadPoolExecutor(max_workers=5)
15
  def google_search(query, api_key, cx):
@@ -307,3 +307,6 @@ def user_details_func(db_uri: str, document: Dict) -> Optional[Dict]:
307
  return user_doc
308
 
309
 
 
 
 
 
8
  from pymongo import MongoClient
9
  from gamification.logic import create_points_func
10
  from gamification.objects import PlatformEngagement, Points
11
+ from controller.password import *
12
+ from controller.streaksManagement import streaks_manager
13
  from concurrent.futures import ThreadPoolExecutor
14
  executor = ThreadPoolExecutor(max_workers=5)
15
  def google_search(query, api_key, cx):
 
307
  return user_doc
308
 
309
 
310
+
311
+
312
+
gamification/objects.py CHANGED
@@ -238,7 +238,7 @@ class CustomerInfo(BaseModel):
238
 
239
 
240
  class individualPoints(BaseModel):
241
- numOfPoints:int
242
  earnedAt:datetime
243
  platformEngagement:Optional[dict]=None
244
  learningProgress:Optional[dict]=None
@@ -254,11 +254,13 @@ class individualPoints(BaseModel):
254
 
255
 
256
  class IndividualUserLevel(BaseModel):
257
- totalpoints:int
258
  levelName:str
 
259
  maxPoints:float
260
  minPoints:float
261
  individualPoints:List[individualPoints]
 
262
 
263
  class Config:
264
  json_encoders = {
@@ -268,10 +270,10 @@ class IndividualUserLevel(BaseModel):
268
 
269
  class SimpleIndividualUserLevel(BaseModel):
270
  totalpoints:float
271
- levelName:Optional[str]=None
272
- maxPoints:Optional[float]=None
273
- minPoints:Optional[float]=None
274
-
275
  class Config:
276
  json_encoders = {
277
  ObjectId: str
 
238
 
239
 
240
  class individualPoints(BaseModel):
241
+ numOfPoints:float
242
  earnedAt:datetime
243
  platformEngagement:Optional[dict]=None
244
  learningProgress:Optional[dict]=None
 
254
 
255
 
256
  class IndividualUserLevel(BaseModel):
257
+ totalpoints:float
258
  levelName:str
259
+ levelNumber:int
260
  maxPoints:float
261
  minPoints:float
262
  individualPoints:List[individualPoints]
263
+
264
 
265
  class Config:
266
  json_encoders = {
 
270
 
271
  class SimpleIndividualUserLevel(BaseModel):
272
  totalpoints:float
273
+ levelName:Optional[str]="default"
274
+ maxPoints:Optional[float]=0
275
+ minPoints:Optional[float]=999999999999999999999
276
+ levelNumber:Optional[int]=1
277
  class Config:
278
  json_encoders = {
279
  ObjectId: str
gamification/pointLogic.py CHANGED
@@ -5,6 +5,8 @@ from gamification.imports import *
5
  from gamification.levelLogic import get_all_levels_func
6
  from concurrent.futures import ThreadPoolExecutor
7
  executor = ThreadPoolExecutor(max_workers=5)
 
 
8
  # utils
9
  def get_particular_level(totalPoints,dreamJob)->UserLevel:
10
  # query db and get the results of all the level probably re use a function
@@ -38,7 +40,6 @@ def create_points_func(document:UserPoints)->bool:
38
  db = client[db_name]
39
 
40
  collection = db[collection_name]
41
- # Insert the document
42
 
43
  if document!=None:
44
  doc = document.model_dump()
@@ -53,7 +54,6 @@ def create_points_func(document:UserPoints)->bool:
53
 
54
 
55
  def get_all_points_func(userId) -> IndividualUserLevel:
56
- # MongoDB URI and configuration
57
  db_uri = MONGO_URI
58
  db_name = "crayonics"
59
  collection_name = "Points"
@@ -62,8 +62,7 @@ def get_all_points_func(userId) -> IndividualUserLevel:
62
  collection = db[collection_name]
63
  dreamJob = get_dream_job(userId=userId)
64
 
65
- # Fetch all documents from the collection
66
- point_cursor = collection.find({"userId": userId}) # This returns a cursor to the documents
67
 
68
  # Convert the cursor to a list so we can reuse it
69
  points_list = list(point_cursor)
@@ -78,7 +77,7 @@ def get_all_points_func(userId) -> IndividualUserLevel:
78
 
79
 
80
  # Create the IndividualUserLevel object with totalPoints and individualPoints
81
- points = IndividualUserLevel(totalpoints=totalPoints,levelName=particularLevelInfo[0].levelName,minPoints=particularLevelInfo[0].minPoints,maxPoints=particularLevelInfo[0].maxPoints, individualPoints=individualPoints)
82
 
83
  return points
84
 
@@ -87,7 +86,6 @@ def get_all_points_func(userId) -> IndividualUserLevel:
87
 
88
 
89
  def get_all_simple_points_func(userId) -> SimpleIndividualUserLevel:
90
- # MongoDB URI and configuration
91
  db_uri = MONGO_URI
92
  db_name = "crayonics"
93
  collection_name = "Points"
@@ -96,25 +94,17 @@ def get_all_simple_points_func(userId) -> SimpleIndividualUserLevel:
96
  collection = db[collection_name]
97
  dreamJob = get_dream_job(userId=userId)
98
 
99
- # Fetch all documents from the collection
100
- point_cursor = collection.find({"userId": userId}) # This returns a cursor to the documents
101
-
102
- # Convert the cursor to a list so we can reuse it
103
  try:
104
  points_list = list(point_cursor)
105
 
106
- # Calculate the total points
107
  totalPoints = sum([point['numOfPoints'] for point in points_list])
108
  particularLevelInfo = get_particular_level(dreamJob=dreamJob,totalPoints=totalPoints)
109
- # Create the individual points list
110
 
111
- # Create the IndividualUserLevel object with totalPoints and individualPoints
112
- points = SimpleIndividualUserLevel(totalpoints=totalPoints)
113
  except:
114
  totalPoints = 0
115
- # Create the individual points list
116
 
117
- # Create the IndividualUserLevel object with totalPoints and individualPoints
118
  points = SimpleIndividualUserLevel(totalpoints=totalPoints)
119
 
120
  return points
 
5
  from gamification.levelLogic import get_all_levels_func
6
  from concurrent.futures import ThreadPoolExecutor
7
  executor = ThreadPoolExecutor(max_workers=5)
8
+
9
+
10
  # utils
11
  def get_particular_level(totalPoints,dreamJob)->UserLevel:
12
  # query db and get the results of all the level probably re use a function
 
40
  db = client[db_name]
41
 
42
  collection = db[collection_name]
 
43
 
44
  if document!=None:
45
  doc = document.model_dump()
 
54
 
55
 
56
  def get_all_points_func(userId) -> IndividualUserLevel:
 
57
  db_uri = MONGO_URI
58
  db_name = "crayonics"
59
  collection_name = "Points"
 
62
  collection = db[collection_name]
63
  dreamJob = get_dream_job(userId=userId)
64
 
65
+ point_cursor = collection.find({"userId": userId})
 
66
 
67
  # Convert the cursor to a list so we can reuse it
68
  points_list = list(point_cursor)
 
77
 
78
 
79
  # Create the IndividualUserLevel object with totalPoints and individualPoints
80
+ points = IndividualUserLevel(totalpoints=totalPoints,levelName=particularLevelInfo[0].levelName,minPoints=particularLevelInfo[0].minPoints,maxPoints=particularLevelInfo[0].maxPoints,levelNumber=particularLevelInfo[0].levelNumber, individualPoints=individualPoints)
81
 
82
  return points
83
 
 
86
 
87
 
88
  def get_all_simple_points_func(userId) -> SimpleIndividualUserLevel:
 
89
  db_uri = MONGO_URI
90
  db_name = "crayonics"
91
  collection_name = "Points"
 
94
  collection = db[collection_name]
95
  dreamJob = get_dream_job(userId=userId)
96
 
97
+ point_cursor = collection.find({"userId": userId})
 
 
 
98
  try:
99
  points_list = list(point_cursor)
100
 
 
101
  totalPoints = sum([point['numOfPoints'] for point in points_list])
102
  particularLevelInfo = get_particular_level(dreamJob=dreamJob,totalPoints=totalPoints)
 
103
 
104
+ points = SimpleIndividualUserLevel(totalpoints=totalPoints,levelName=particularLevelInfo[0].levelName,maxPoints=particularLevelInfo[0].maxPoints,minPoints=particularLevelInfo[0].minPoints,levelNumber=particularLevelInfo[0].levelNumber)
 
105
  except:
106
  totalPoints = 0
 
107
 
 
108
  points = SimpleIndividualUserLevel(totalpoints=totalPoints)
109
 
110
  return points