Skip to content

Commit

Permalink
Merge pull request #86 from AndrewHUNGNguyen/develop
Browse files Browse the repository at this point in the history
Having preliminary code for praise backend API for review
  • Loading branch information
gregv authored Sep 17, 2024
2 parents dfe6419 + b0d0bf0 commit cda68b3
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 4 deletions.
23 changes: 22 additions & 1 deletion api/messages/messages_service.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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"]

Expand Down
30 changes: 27 additions & 3 deletions api/messages/messages_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os

import logging
import json
from common.auth import auth, auth_user

from flask import (
Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
14 changes: 14 additions & 0 deletions common/utils/firebase.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

0 comments on commit cda68b3

Please sign in to comment.