From b0d0bf0ddf75de4ff82e257e8040230fbc2ed413 Mon Sep 17 00:00:00 2001 From: Andrew Nguyen Date: Mon, 16 Sep 2024 16:18:44 -0700 Subject: [PATCH] Having preliminary code for praise backend API for review --- api/messages/messages_service.py | 23 ++++++++++++++++++++++- api/messages/messages_views.py | 30 +++++++++++++++++++++++++++--- common/utils/firebase.py | 14 ++++++++++++++ 3 files changed, 63 insertions(+), 4 deletions(-) diff --git a/api/messages/messages_service.py b/api/messages/messages_service.py index a5b385b..6a82627 100644 --- a/api/messages/messages_service.py +++ b/api/messages/messages_service.py @@ -1,6 +1,6 @@ from common.utils import safe_get_env_var from common.utils.slack import send_slack_audit, create_slack_channel, send_slack, invite_user_to_channel -from common.utils.firebase import get_hackathon_by_event_id, upsert_news, get_github_contributions_for_user +from common.utils.firebase import get_hackathon_by_event_id, upsert_news, upsert_praise, get_github_contributions_for_user from common.utils.openai_api import generate_and_save_image_to_cdn from common.utils.github import create_github_repo from api.messages.message import Message @@ -31,6 +31,7 @@ logger = logging.getLogger("myapp") logger.setLevel(logging.DEBUG) +logger.setLevel(logging.DEBUG) google_recaptcha_key = safe_get_env_var("GOOGLE_CAPTCHA_SECRET_KEY") @@ -1235,6 +1236,26 @@ def save_news(json): return Message("Saved News") +def save_praise(json): + logger.debug(f"Attempting to save the praise with the json object {json}") + # Take in Slack message and summarize it using GPT-3.5 + # Make sure these fields exist praise_receiver, praise_channel, praise_message + check_fields = ["praise_receiver", "praise_channel", "praise_message"] + for field in check_fields: + if field not in json: + logger.error(f"Missing field {field} in {json}") + return Message("Missing field") + + logger.debug(f"Detected required fields, attempting to save praise") + upsert_praise(json) + + logger.info("Updated praise successfully") + + #get_news.cache_clear() + #logger.info("Cleared cache for get_news") + + return Message("Saved praise") + async def save_lead(json): token = json["token"] diff --git a/api/messages/messages_views.py b/api/messages/messages_views.py index ad8dbfe..8a34abb 100644 --- a/api/messages/messages_views.py +++ b/api/messages/messages_views.py @@ -1,5 +1,6 @@ import os - +import logging +import json from common.auth import auth, auth_user from flask import ( @@ -43,10 +44,12 @@ save_npo_application, get_npo_applications, update_npo_application, - get_github_profile + get_github_profile, + save_praise ) - +logger = logging.getLogger("myapp") +logger.setLevel(logging.DEBUG) bp_name = 'api-messages' @@ -262,6 +265,27 @@ async def store_lead(): else: return "OK", 200 +# -------------------- Praises routes begin here --------------------------- # +@bp.route("/praise", methods=["POST"]) +def store_praise(): + # Check header for token + # if token is valid, store news + # else return 401 + + token = request.headers.get("X-Api-Key") + + # Check BACKEND_NEWS_TOKEN + if token == None or token != os.getenv("BACKEND_PRAISE_TOKEN"): + return "Unauthorized", 401 + else: + logger.debug(f"Hre is the request object {request.get_json()}") + # try: + # logger.debug(f"Here is the request object: {request.get_json()}") + # except Exception as e: + # logger.error(f"Error logging request object: {e}") + return vars(save_praise(request.get_json())) + +# -------------------- Praises routes end here --------------------------- # # -------------------- Problem Statement routes to be deleted --------------------------- # @auth.require_user diff --git a/common/utils/firebase.py b/common/utils/firebase.py index 039a665..006e5fb 100644 --- a/common/utils/firebase.py +++ b/common/utils/firebase.py @@ -1035,3 +1035,17 @@ def upsert_news(news): logger.info("news does not exist") db.collection("news").add(news) + +def upsert_praise(praise): + db = get_db() # this connects to our Firestore database + logger.info(f"Adding praise {praise}") + + #Check if the vibe field exists. + if "praise_gif" not in praise: + logger.info("vibe field not found, setting praise['vibe'] to None") + praise["praise_gif"] = None + + logger.info(f"Adding refined praise {praise} into the database") + + db.collection("praises").add(praise) + logger.info("praise successfully saved")