Skip to content

Commit

Permalink
[admin] Volunteer edit ability
Browse files Browse the repository at this point in the history
  • Loading branch information
gregv committed Aug 31, 2024
1 parent 1670041 commit 002d19c
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 5 deletions.
86 changes: 86 additions & 0 deletions api/messages/messages_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,92 @@ def update_npo(json):
"Updated NPO"
)

@limits(calls=50, period=ONE_MINUTE)
def update_hackathon_volunteers(event_id, json, propel_id):
db = get_db()
logger.info("Update Hackathon Volunteers")
logger.info("JSON: " + str(json))
send_slack_audit(action="update_hackathon_volunteers", message="Updating", payload=json)

# Since we know this user is an admin, prefix all vars with admin_
admin_email, admin_user_id, admin_last_login, admin_profile_image, admin_name, admin_nickname = get_propel_user_details_by_id(propel_id)

# Query for event_id column
doc = db.collection('hackathons').where("event_id", "==", event_id).stream()
doc = list(doc)
doc = doc[0] if len(doc) > 0 else None

# Convert from DocumentSnapshot to DocumentReference
if isinstance(doc, firestore.DocumentSnapshot):
doc = doc.reference

# If we don't find the event, return
if doc is None:
return Message("No Hackathon Found")

volunteer_type = json["type"]
timestamp = json["timestamp"]
name = json["name"]
# We will use timestamp and name to find the volunteer

if volunteer_type == "mentors":
logger.info("Updating Mentor")
# Get the mentor block
mentor_block = doc.get().to_dict()["mentors"]

# Find the mentor
for mentor in mentor_block:
logger.info(f"Comparing {mentor['name']} with {name} and {mentor['timestamp']} with {timestamp}")
if mentor["name"] == name and mentor["timestamp"] == timestamp:
# For each field in mentor, update with the new value from json
for key in mentor.keys():
if key in json:
if key == "timestamp" or key == "name": # Don't want to update these since they are primary keys
continue
mentor[key] = json[key]

mentor["updated_timestamp"] = datetime.now().isoformat()
mentor["updated_by"] = admin_name

logger.info(f"Found mentor {mentor}")
break
# Update the mentor block with the json for this mentor
doc.update({
"mentors": mentor_block
})
elif volunteer_type == "judge":
# Get the judge block
judge_block = doc.get().to_dict()["judges"]
# Find the judge
for judge in judge_block:
if judge["name"] == name and judge["timestamp"] == timestamp:
# For each field in judge, update with the new value from json
for key in judge.keys():
if key in json:
if key == "timestamp" or key == "name":
continue
judge[key] = json[key]
judge["updated_timestamp"] = datetime.now().isoformat()
judge["updated_by"] = admin_name

logger.info(f"Found judge {judge}")
break
# Update the judge block with the json for this judge
doc.update({
"judges": judge_block
})

# Clear cache
get_single_hackathon_event.cache_clear()

return Message(
"Updated Hackathon Volunteers"
)






@limits(calls=50, period=ONE_MINUTE)
def save_hackathon(json):
Expand Down
17 changes: 12 additions & 5 deletions api/messages/messages_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
get_single_hackathon_event,
get_single_hackathon_id,
save_hackathon,
update_hackathon_volunteers,
get_teams_list,
save_team,
unjoin_team,
Expand All @@ -47,6 +48,10 @@
bp_url_prefix = '/api/messages'
bp = Blueprint(bp_name, __name__, url_prefix=bp_url_prefix)

def getOrgId(req):
# Get the org_id from the req
return req.headers.get("X-Org-Id")


@bp.route("/public")
def public():
Expand Down Expand Up @@ -112,6 +117,13 @@ def submit_npo_application():
def add_hackathon():
return vars(save_hackathon(request.get_json()))

@bp.route("/hackathon/<event_id>/volunteers", methods=["PATCH"])
@auth.require_user
@auth.require_org_member_with_permission("volunteer.admin", req_to_org_id=getOrgId)
def update_hackathon_volunteers_mentors_judges(event_id):
if auth_user and auth_user.user_id:
return vars(update_hackathon_volunteers(event_id, request.get_json(), auth_user.user_id))


@bp.route("/hackathons", methods=["GET"])
def list_hackathons():
Expand Down Expand Up @@ -280,11 +292,6 @@ def get_profile_by_id(id):
return get_user_by_id_old(id)


def getOrgId(req):
# Get the org_id from the req
return req.headers.get("X-Org-Id")


# Used to provide profile details - user must be logged in
@bp.route("/admin/profiles", methods=["GET"])
@auth.require_org_member_with_permission("profile.admin", req_to_org_id=getOrgId)
Expand Down

0 comments on commit 002d19c

Please sign in to comment.