Spaces:
Running
Running
removed the returning levels
Browse files- app.py +64 -37
- gamification/pointLogic.py +2 -0
app.py
CHANGED
|
@@ -486,50 +486,77 @@ db = client.crayonics
|
|
| 486 |
collection = db['Points']
|
| 487 |
|
| 488 |
# A function to handle changes
|
| 489 |
-
def handle_change(change):
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
if
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
| 497 |
-
|
| 498 |
-
|
| 499 |
-
|
| 500 |
-
|
| 501 |
-
|
| 502 |
-
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
# ('lastName', 1),
|
| 506 |
-
# ('totalpoints', -1)
|
| 507 |
-
# ])
|
| 508 |
-
# print("index",index)
|
| 509 |
-
else:
|
| 510 |
-
if change['operationType'] == 'insert':
|
| 511 |
-
# Extract the full document
|
| 512 |
-
full_document = change['fullDocument']
|
| 513 |
-
|
| 514 |
-
# Extract the userId and numOfPoints
|
| 515 |
|
| 516 |
-
|
|
|
|
| 517 |
leveleduser = get_all_users(userId=user_id)
|
| 518 |
points = get_all_simple_points_func(userId=user_id)
|
| 519 |
tempDreamJob = get_dream_job(userId=user_id)
|
| 520 |
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
| 521 |
create_leaderboard_ranking(LeaderBoardRanking(userId=user_id,firstName=leveleduser['first_name'],lastName=leveleduser['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
| 522 |
-
|
| 523 |
-
dockey = str(change['documentKey']['_id'])
|
| 524 |
-
user_id = get_user_id_from_docKey(dockId=dockey)
|
| 525 |
-
leveleduser = get_all_users(user_id=user_id)
|
| 526 |
-
points = get_all_simple_points_func(userId=user_id)
|
| 527 |
-
tempDreamJob = get_dream_job(userId=user_id)
|
| 528 |
-
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
| 529 |
-
create_leaderboard_ranking(LeaderBoardRanking(userId=user_id,firstName=leveleduser['first_name'],lastName=leveleduser['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
| 530 |
-
|
| 531 |
|
| 532 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
|
| 534 |
# Function to run the change stream in a separate thread (non-blocking)
|
| 535 |
def watch_change_stream():
|
|
|
|
| 486 |
collection = db['Points']
|
| 487 |
|
| 488 |
# A function to handle changes
|
| 489 |
+
def handle_change(change=None,new_point=None):
|
| 490 |
+
import logging
|
| 491 |
+
logging.basicConfig(level=logging.INFO)
|
| 492 |
+
logger = logging.getLogger(__name__)
|
| 493 |
+
if new_point!=None:
|
| 494 |
+
logger.info(f"Extra info: {new_point}")
|
| 495 |
+
if "LeaderBoard" not in collections:
|
| 496 |
+
print("No leaderboard so creating one now")
|
| 497 |
+
users = get_all_users()
|
| 498 |
+
for user in users:
|
| 499 |
+
print("inserting user",f"user id {user['_id']}")
|
| 500 |
+
points = get_all_simple_points_func(userId=str(user['_id']))
|
| 501 |
+
tempDreamJob = get_dream_job(userId=str(user['_id']))
|
| 502 |
+
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
| 503 |
+
create_leaderboard_ranking(LeaderBoardRanking(userId=str(user['_id']),firstName=user['first_name'],lastName=user['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
| 504 |
+
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 505 |
|
| 506 |
+
# implement logic that takes new_point['userId'] and does the same thing update would have
|
| 507 |
+
user_id =new_point.get('userId')
|
| 508 |
leveleduser = get_all_users(userId=user_id)
|
| 509 |
points = get_all_simple_points_func(userId=user_id)
|
| 510 |
tempDreamJob = get_dream_job(userId=user_id)
|
| 511 |
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
| 512 |
create_leaderboard_ranking(LeaderBoardRanking(userId=user_id,firstName=leveleduser['first_name'],lastName=leveleduser['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
| 513 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 514 |
|
| 515 |
+
elif new_point==None and change!=None:
|
| 516 |
+
print("Change detected in point making changes immediately")
|
| 517 |
+
logger.info(f"Change detected: {change}")
|
| 518 |
+
# add everybodies points and add it to the leaderboard table
|
| 519 |
+
collections = db.list_collection_names()
|
| 520 |
+
if "LeaderBoard" not in collections:
|
| 521 |
+
print("No leaderboard so creating one now")
|
| 522 |
+
users = get_all_users()
|
| 523 |
+
for user in users:
|
| 524 |
+
print("inserting user",f"user id {user['_id']}")
|
| 525 |
+
points = get_all_simple_points_func(userId=str(user['_id']))
|
| 526 |
+
tempDreamJob = get_dream_job(userId=str(user['_id']))
|
| 527 |
+
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
| 528 |
+
|
| 529 |
+
create_leaderboard_ranking(LeaderBoardRanking(userId=str(user['_id']),firstName=user['first_name'],lastName=user['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
| 530 |
+
leaderBoardcollection = db['LeaderBoard']
|
| 531 |
+
# index = leaderBoardcollection.create_index([
|
| 532 |
+
# ('lastName', 1),
|
| 533 |
+
# ('totalpoints', -1)
|
| 534 |
+
# ])
|
| 535 |
+
# print("index",index)
|
| 536 |
+
else:
|
| 537 |
+
if change['operationType'] == 'insert':
|
| 538 |
+
# Extract the full document
|
| 539 |
+
full_document = change['fullDocument']
|
| 540 |
+
|
| 541 |
+
# Extract the userId and numOfPoints
|
| 542 |
+
|
| 543 |
+
user_id =full_document.get('userId')
|
| 544 |
+
leveleduser = get_all_users(userId=user_id)
|
| 545 |
+
points = get_all_simple_points_func(userId=user_id)
|
| 546 |
+
tempDreamJob = get_dream_job(userId=user_id)
|
| 547 |
+
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
| 548 |
+
create_leaderboard_ranking(LeaderBoardRanking(userId=user_id,firstName=leveleduser['first_name'],lastName=leveleduser['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
| 549 |
+
elif change['operationType'] == 'update':
|
| 550 |
+
dockey = str(change['documentKey']['_id'])
|
| 551 |
+
user_id = get_user_id_from_docKey(dockId=dockey)
|
| 552 |
+
leveleduser = get_all_users(user_id=user_id)
|
| 553 |
+
points = get_all_simple_points_func(userId=user_id)
|
| 554 |
+
tempDreamJob = get_dream_job(userId=user_id)
|
| 555 |
+
dreamJob = tempDreamJob if type(tempDreamJob)==str else "IncompleteProfile"
|
| 556 |
+
create_leaderboard_ranking(LeaderBoardRanking(userId=user_id,firstName=leveleduser['first_name'],lastName=leveleduser['last_name'],totalpoints=points.totalpoints,lastUpdated=datetime.now(),careerPath=dreamJob,))
|
| 557 |
+
|
| 558 |
+
|
| 559 |
+
print(f"Change detected: {dumps(change)}")
|
| 560 |
|
| 561 |
# Function to run the change stream in a separate thread (non-blocking)
|
| 562 |
def watch_change_stream():
|
gamification/pointLogic.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
|
|
|
|
| 2 |
from gamification.objects import UserPoints ,SimpleIndividualUserLevel,IndividualUserLevel,UserLevel
|
| 3 |
from gamification.imports import *
|
| 4 |
from gamification.levelLogic import get_all_levels_func
|
|
@@ -41,6 +42,7 @@ def create_points_func(document:UserPoints)->bool:
|
|
| 41 |
doc = document.model_dump()
|
| 42 |
doc['earnedAt']=datetime.now()
|
| 43 |
result = collection.insert_one(doc)
|
|
|
|
| 44 |
return True
|
| 45 |
else:
|
| 46 |
client.close()
|
|
|
|
| 1 |
|
| 2 |
+
from app import handle_change
|
| 3 |
from gamification.objects import UserPoints ,SimpleIndividualUserLevel,IndividualUserLevel,UserLevel
|
| 4 |
from gamification.imports import *
|
| 5 |
from gamification.levelLogic import get_all_levels_func
|
|
|
|
| 42 |
doc = document.model_dump()
|
| 43 |
doc['earnedAt']=datetime.now()
|
| 44 |
result = collection.insert_one(doc)
|
| 45 |
+
handle_change(new_point={"userId",document.userId})
|
| 46 |
return True
|
| 47 |
else:
|
| 48 |
client.close()
|