Spaces:
Running
Running
added some routes
Browse files- app.py +4 -1
- gamification/__init__.py +0 -0
- gamification/logic.py +210 -0
- gamification/objects.py +41 -0
- gamification/routes.py +75 -0
app.py
CHANGED
@@ -8,11 +8,14 @@ from jwtcoding import *
|
|
8 |
from fastapi.responses import JSONResponse
|
9 |
import docx
|
10 |
import fitz
|
|
|
11 |
from scraper import scrapeCourse
|
12 |
import asyncio
|
13 |
from google import genai
|
14 |
from typing import Optional,List
|
15 |
from pydantic import BaseModel
|
|
|
|
|
16 |
load_dotenv()
|
17 |
|
18 |
CX = os.getenv("SEARCH_ENGINE_ID")
|
@@ -21,8 +24,8 @@ PINECONE_API_KEY=os.getenv("PINECONE_API_KEY")
|
|
21 |
GEMINI_API_KEY=os.getenv("GEMINI_API_KEY")
|
22 |
MONGO_URI=os.getenv("MONGO_URI")
|
23 |
app = FastAPI()
|
|
|
24 |
|
25 |
-
import re
|
26 |
|
27 |
class UserBody(BaseModel):
|
28 |
firstName: Optional[str] = None
|
|
|
8 |
from fastapi.responses import JSONResponse
|
9 |
import docx
|
10 |
import fitz
|
11 |
+
from gamification.routes import gamification
|
12 |
from scraper import scrapeCourse
|
13 |
import asyncio
|
14 |
from google import genai
|
15 |
from typing import Optional,List
|
16 |
from pydantic import BaseModel
|
17 |
+
import re
|
18 |
+
|
19 |
load_dotenv()
|
20 |
|
21 |
CX = os.getenv("SEARCH_ENGINE_ID")
|
|
|
24 |
GEMINI_API_KEY=os.getenv("GEMINI_API_KEY")
|
25 |
MONGO_URI=os.getenv("MONGO_URI")
|
26 |
app = FastAPI()
|
27 |
+
app.mount('/gamification',gamification)
|
28 |
|
|
|
29 |
|
30 |
class UserBody(BaseModel):
|
31 |
firstName: Optional[str] = None
|
gamification/__init__.py
ADDED
File without changes
|
gamification/logic.py
ADDED
@@ -0,0 +1,210 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
from gamification.objects import UserFeedback, UserLevel, UserPoints
|
3 |
+
from bson import ObjectId
|
4 |
+
from pymongo import MongoClient
|
5 |
+
import os
|
6 |
+
from typing import List
|
7 |
+
from dotenv import load_dotenv
|
8 |
+
load_dotenv()
|
9 |
+
MONGO_URI = os.getenv("MONGO_URI")
|
10 |
+
|
11 |
+
|
12 |
+
# levels
|
13 |
+
def create_level_func(document:UserLevel,)->bool:
|
14 |
+
"""
|
15 |
+
Creates a UserLevel document in the MongoDB collection.
|
16 |
+
|
17 |
+
:param UserLevel: Actually accepts an Instance of UserLevel and deserializes it here
|
18 |
+
:param maxPoints: Maximum Amount of points for this level.
|
19 |
+
:param minPoints: Minimum Amount of points for this level.
|
20 |
+
:param levelNumber: Number for this level.
|
21 |
+
:param careerPath: Career Path Special for this level.
|
22 |
+
"""
|
23 |
+
db_uri = MONGO_URI
|
24 |
+
db_name = "crayonics"
|
25 |
+
collection_name="Level"
|
26 |
+
client = MongoClient(db_uri)
|
27 |
+
db = client[db_name]
|
28 |
+
collection = db[collection_name]
|
29 |
+
should_proceed= (collection.find_one(document.model_dump()))
|
30 |
+
# Insert the document
|
31 |
+
if should_proceed==None:
|
32 |
+
if document!=None:
|
33 |
+
result = collection.insert_one(document.model_dump())
|
34 |
+
return True
|
35 |
+
else:
|
36 |
+
client.close()
|
37 |
+
return False
|
38 |
+
else:
|
39 |
+
# The exact document already exists so it can't be created again
|
40 |
+
return False
|
41 |
+
|
42 |
+
|
43 |
+
|
44 |
+
|
45 |
+
def get_all_levels_func() -> List[UserLevel]:
|
46 |
+
# MongoDB URI and configuration
|
47 |
+
db_uri = MONGO_URI
|
48 |
+
db_name = "crayonics"
|
49 |
+
collection_name="Level"
|
50 |
+
client = MongoClient(db_uri)
|
51 |
+
db = client[db_name]
|
52 |
+
collection = db[collection_name]
|
53 |
+
|
54 |
+
# Fetch all documents from the collection
|
55 |
+
levels_cursor = collection.find() # This returns a cursor to the documents
|
56 |
+
|
57 |
+
# Convert the cursor to a list of UserLevel objects
|
58 |
+
levels = [UserLevel(**level) for level in levels_cursor]
|
59 |
+
|
60 |
+
return levels
|
61 |
+
|
62 |
+
|
63 |
+
|
64 |
+
def delete_level_func(level_id,)->bool:
|
65 |
+
db_uri = MONGO_URI
|
66 |
+
db_name = "crayonics"
|
67 |
+
collection_name="Level"
|
68 |
+
client = MongoClient(db_uri)
|
69 |
+
db = client[db_name]
|
70 |
+
collection = db[collection_name]
|
71 |
+
if isinstance(level_id, str):
|
72 |
+
level_id = ObjectId(level_id)
|
73 |
+
result = collection.delete_one(filter={"_id":level_id})
|
74 |
+
if result.acknowledged==True:
|
75 |
+
if result.deleted_count ==0:
|
76 |
+
return False
|
77 |
+
else:
|
78 |
+
return True
|
79 |
+
else:
|
80 |
+
return False
|
81 |
+
|
82 |
+
|
83 |
+
|
84 |
+
|
85 |
+
def edit_level_func(level_id, **kwargs):
|
86 |
+
"""
|
87 |
+
Edit a UserLevel document in the MongoDB collection.
|
88 |
+
|
89 |
+
:param level_id: The ObjectId or unique identifier of the UserLevel to edit.
|
90 |
+
:param maxPoints: Maximum Amount of points for this level.
|
91 |
+
:param minPoints: Minimum Amount of points for this level.
|
92 |
+
:param levelNumber: Number for this level.
|
93 |
+
:param careerPath: Career Path Special for this level.
|
94 |
+
"""
|
95 |
+
db_uri = MONGO_URI
|
96 |
+
db_name = "crayonics"
|
97 |
+
collection_name="Level"
|
98 |
+
client = MongoClient(db_uri)
|
99 |
+
db = client[db_name]
|
100 |
+
collection = db[collection_name]
|
101 |
+
|
102 |
+
|
103 |
+
# Ensure that `level_id` is an ObjectId if using ObjectId as the identifier
|
104 |
+
if isinstance(level_id, str):
|
105 |
+
level_id = ObjectId(level_id)
|
106 |
+
|
107 |
+
# Prepare the update data
|
108 |
+
update_data = {key: value for key, value in kwargs.items() if value is not None}
|
109 |
+
|
110 |
+
|
111 |
+
# Perform the update operation in the collection
|
112 |
+
result = collection.update_one(
|
113 |
+
{"_id": level_id}, # Find the document by its unique identifier (ObjectId or other)
|
114 |
+
{"$set": update_data} # Update the document with the new values
|
115 |
+
)
|
116 |
+
|
117 |
+
return result.modified_count
|
118 |
+
|
119 |
+
|
120 |
+
|
121 |
+
|
122 |
+
|
123 |
+
|
124 |
+
|
125 |
+
|
126 |
+
|
127 |
+
# points
|
128 |
+
|
129 |
+
def create_points_func(document:UserPoints)->bool:
|
130 |
+
|
131 |
+
db_uri = MONGO_URI
|
132 |
+
db_name = "crayonics"
|
133 |
+
collection_name="Points"
|
134 |
+
client = MongoClient(db_uri)
|
135 |
+
db = client[db_name]
|
136 |
+
collection = db[collection_name]
|
137 |
+
# Insert the document
|
138 |
+
|
139 |
+
if document!=None:
|
140 |
+
result = collection.insert_one(document.model_dump())
|
141 |
+
return True
|
142 |
+
else:
|
143 |
+
client.close()
|
144 |
+
return False
|
145 |
+
|
146 |
+
|
147 |
+
|
148 |
+
def get_all_points_func() -> List[UserFeedback]:
|
149 |
+
# MongoDB URI and configuration
|
150 |
+
db_uri = MONGO_URI
|
151 |
+
db_name = "crayonics"
|
152 |
+
collection_name="Points"
|
153 |
+
client = MongoClient(db_uri)
|
154 |
+
db = client[db_name]
|
155 |
+
collection = db[collection_name]
|
156 |
+
|
157 |
+
# Fetch all documents from the collection
|
158 |
+
point_cursor = collection.find() # This returns a cursor to the documents
|
159 |
+
|
160 |
+
# Convert the cursor to a list of UserLevel objects
|
161 |
+
points = [UserFeedback(**point) for point in point_cursor]
|
162 |
+
|
163 |
+
return points
|
164 |
+
|
165 |
+
|
166 |
+
|
167 |
+
|
168 |
+
|
169 |
+
|
170 |
+
|
171 |
+
|
172 |
+
|
173 |
+
|
174 |
+
|
175 |
+
# feedback
|
176 |
+
def create_feedback_func(document:UserFeedback)->bool:
|
177 |
+
db_uri = MONGO_URI
|
178 |
+
db_name = "crayonics"
|
179 |
+
collection_name="Feedback"
|
180 |
+
client = MongoClient(db_uri)
|
181 |
+
db = client[db_name]
|
182 |
+
collection = db[collection_name]
|
183 |
+
# Insert the document
|
184 |
+
if document!=None:
|
185 |
+
result = collection.insert_one(document.model_dump())
|
186 |
+
return True
|
187 |
+
else:
|
188 |
+
client.close()
|
189 |
+
return False
|
190 |
+
|
191 |
+
|
192 |
+
def get_all_feedback_func() -> List[UserFeedback]:
|
193 |
+
# MongoDB URI and configuration
|
194 |
+
db_uri = MONGO_URI
|
195 |
+
db_name = "crayonics"
|
196 |
+
collection_name="Feedback"
|
197 |
+
client = MongoClient(db_uri)
|
198 |
+
db = client[db_name]
|
199 |
+
collection = db[collection_name]
|
200 |
+
|
201 |
+
# Fetch all documents from the collection
|
202 |
+
feedback_cursor = collection.find() # This returns a cursor to the documents
|
203 |
+
|
204 |
+
# Convert the cursor to a list of UserLevel objects
|
205 |
+
feedbacks = [UserFeedback(**feedback) for feedback in feedback_cursor]
|
206 |
+
|
207 |
+
return feedbacks
|
208 |
+
|
209 |
+
|
210 |
+
|
gamification/objects.py
ADDED
@@ -0,0 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from pydantic import BaseModel
|
2 |
+
from bson import ObjectId
|
3 |
+
class UserLevel(BaseModel):
|
4 |
+
maxPoints:int
|
5 |
+
minPoints:int
|
6 |
+
levelName:str
|
7 |
+
careerPath:str
|
8 |
+
levelNumber:int
|
9 |
+
# To convert MongoDB ObjectId to string, if needed
|
10 |
+
class Config:
|
11 |
+
json_encoders = {
|
12 |
+
ObjectId: str
|
13 |
+
}
|
14 |
+
|
15 |
+
class UserPoints(BaseModel):
|
16 |
+
points:int
|
17 |
+
reason:str
|
18 |
+
userId:str
|
19 |
+
class Config:
|
20 |
+
json_encoders = {
|
21 |
+
ObjectId: str
|
22 |
+
}
|
23 |
+
class UserFeedback(BaseModel):
|
24 |
+
userId:str
|
25 |
+
feedback:str
|
26 |
+
rating:int
|
27 |
+
|
28 |
+
class Config:
|
29 |
+
json_encoders = {
|
30 |
+
ObjectId: str
|
31 |
+
}
|
32 |
+
|
33 |
+
class IndividualUserLevel(BaseModel):
|
34 |
+
totalpoints:int
|
35 |
+
userId:str
|
36 |
+
levelId:str
|
37 |
+
class Config:
|
38 |
+
json_encoders = {
|
39 |
+
ObjectId: str
|
40 |
+
}
|
41 |
+
|
gamification/routes.py
ADDED
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI,HTTPException
|
2 |
+
from gamification.objects import *
|
3 |
+
from gamification.logic import *
|
4 |
+
from pydantic import BaseModel
|
5 |
+
from typing import Optional
|
6 |
+
from bson import ObjectId
|
7 |
+
|
8 |
+
class EditableUserLevel(BaseModel):
|
9 |
+
levelId : str
|
10 |
+
maxPoints : Optional[int]= None
|
11 |
+
minPoints : Optional[int]= None
|
12 |
+
levelName : Optional[str]= None
|
13 |
+
careerPath : Optional[str]= None
|
14 |
+
levelNumber : Optional[int]= None
|
15 |
+
# To convert MongoDB ObjectId to string, if needed
|
16 |
+
class Config:
|
17 |
+
json_encoders = {
|
18 |
+
ObjectId: str
|
19 |
+
}
|
20 |
+
|
21 |
+
|
22 |
+
gamification = FastAPI()
|
23 |
+
|
24 |
+
|
25 |
+
|
26 |
+
|
27 |
+
@gamification.post("/create-feedback",tags=["user"])
|
28 |
+
def create_feedback(user_feedback:UserFeedback)->bool:
|
29 |
+
try:
|
30 |
+
result = create_feedback_func(user_feedback)
|
31 |
+
return {"message":result}
|
32 |
+
except Exception as e:
|
33 |
+
raise HTTPException(status_code=500,detail=f"{e}")
|
34 |
+
|
35 |
+
@gamification.post("/get-leaderboard",tags=["user"])
|
36 |
+
def get_leaderboard_details():
|
37 |
+
pass
|
38 |
+
|
39 |
+
|
40 |
+
@gamification.get("/get-feedback",tags=["admin"])
|
41 |
+
def get_feedback()->List[UserFeedback]:
|
42 |
+
feedbacks = get_all_feedback_func()
|
43 |
+
return feedbacks
|
44 |
+
|
45 |
+
|
46 |
+
|
47 |
+
@gamification.post("/create-level",tags=["admin"])
|
48 |
+
def create_level(level_obj:UserLevel)->bool:
|
49 |
+
result = create_level_func(document=level_obj)
|
50 |
+
return {"message":result}
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
@gamification.get("/get-level",tags=["admin","user"])
|
55 |
+
def get_level_details_and_information()->List[UserLevel]:
|
56 |
+
levels= get_all_levels_func()
|
57 |
+
return levels
|
58 |
+
|
59 |
+
|
60 |
+
@gamification.patch("/edit-level",tags=["admin"])
|
61 |
+
def edit_level(level_obj:EditableUserLevel)->int:
|
62 |
+
result = edit_level_func(level_id=level_obj.levelId,levelName=level_obj.levelName,careerPath=level_obj.careerPath,minPoints=level_obj.minPoints,maxPoints=level_obj.maxPoints,levelNumber=level_obj.levelNumber)
|
63 |
+
|
64 |
+
return {"message":result}
|
65 |
+
|
66 |
+
|
67 |
+
|
68 |
+
|
69 |
+
@gamification.delete("/delete-level/{levelId}",tags=["admin"])
|
70 |
+
def delete_level(levelId)->bool:
|
71 |
+
try:
|
72 |
+
result = delete_level_func(level_id=levelId)
|
73 |
+
return {"message":result}
|
74 |
+
except Exception as e:
|
75 |
+
raise HTTPException(status_code=500,detail=f"{e}")
|