Skip to content

Commit

Permalink
Merge pull request #68 from opportunity-hack/develop
Browse files Browse the repository at this point in the history
Hearts API Endpoint
  • Loading branch information
gregv authored Jul 23, 2024
2 parents 0fb0535 + 1c34e82 commit 0fe1029
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ scripts/nonprofits_round_3_from_pdf.csv
scripts/slagenciesfy1819.pdf
scripts/generated_image.png
test/data/*.xlsx
.env.prod
2 changes: 2 additions & 0 deletions api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
#from api.newsletters import subscription_views
from api.certificates import certificate_views
from api.users import users_views
from api.hearts import hearts_views
from api.problemstatements import problem_statement_views

from common.utils import safe_get_env_var
Expand Down Expand Up @@ -142,6 +143,7 @@ def add_headers(response):
app.register_blueprint(certificate_views.bp)
#app.register_blueprint(subscription_views.bp)
app.register_blueprint(users_views.bp)
app.register_blueprint(hearts_views.bp)
app.register_blueprint(problem_statement_views.bp)

return app
36 changes: 36 additions & 0 deletions api/hearts/hearts_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
from model.user import User
from services import hearts_service
from common.utils import safe_get_env_var
from common.auth import auth, auth_user

from flask import (
Blueprint,
request,
g
)

bp_name = 'api-hearts'
bp_url_prefix = '/api/hearts'
bp = Blueprint(bp_name, __name__, url_prefix=bp_url_prefix)


# Used to provide profile details - user must be logged in
@bp.route("/", methods=["GET"])
@auth.require_user
def get_hearts():
print("get_hearts")
res = hearts_service.get_hearts_for_all_users()
print(f"res: {res}")
return {"hearts": res}

@bp.route("/", methods=["POST"])
#@auth.require_user
def save_hearts():
print("save_hearts")
if auth_user and auth_user.user_id:
print(f"request.get_json(): {request.get_json()}")
# u: User | None = users_service.save_profile_metadata(auth_user.user_id, request.get_json())
#return vars(u) if u is not None else None
return "Biuttera"
else:
return "Biutter"
30 changes: 30 additions & 0 deletions model/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class User:
badges = []
teams = []
hackathons = []
history = {}

@classmethod
def deserialize(cls, d):
Expand All @@ -36,6 +37,35 @@ def deserialize(cls, d):
u.role = d['role'] if 'role' in d else ''
u.company = d['company'] if 'company' in d else ''
u.why = d['why'] if 'why' in d else ''

# Handle history in a generic way
'''
"history": {
"how": {
"code_reliability": 2,
"customer_driven_innovation_and_design_thinking": 1,
"iterations_of_code_pushed_to_production": 1.5,
"standups_completed": 2.5
},
"what": {
"code_quality": 0.5,
"design_architecture": 0.5,
"documentation": 0.5,
"observability": 0,
"productionalized_projects": 0.5,
"requirements_gathering": 0.5,
"unit_test_coverage": 0,
"unit_test_writing": 0
}
},
'''
if 'history' in d:
if 'how' in d['history']:
u.how = d['history']['how']
if 'what' in d['history']:
u.what = d['history']['what']
u.history = d['history']

return u

def serialize(self):
Expand Down
33 changes: 33 additions & 0 deletions services/hearts_service.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
from db.db import fetch_users


def get_hearts_for_all_users():
users = fetch_users()

result = []

# Result should have slackUsername, totalHearts, heartTypes (how or what) and heartCount
# This is all within the "history" key for each user
# User is type model.user.User
for user in users:
total_hearts = 0

if user.history:
print(f"User history: {user.history}")
'''
Example of user history:
{'what': {'unit_test_coverage': 0, 'documentation': 0.5, 'productionalized_projects': 0.5, 'unit_test_writing': 0, 'observability': 0, 'code_quality': 0.5, 'requirements_gathering': 0.5, 'design_architecture': 0.5}, 'how': {'iterations_of_code_pushed_to_production': 1.5, 'code_reliability': 2, 'standups_completed': 2.5, 'customer_driven_innovation_and_design_thinking': 1}}
'''
# Result should have slackUsername, totalHearts, heartTypes (how or what) and heartCount
# Count the total hearts
for key in user.history:
for subkey in user.history[key]:
total_hearts += user.history[key][subkey]

result.append({
"slackUsername": user.name,
"totalHearts": total_hearts,
"heartTypes": list(user.history.keys()),
"history": user.history
})
return result

0 comments on commit 0fe1029

Please sign in to comment.