Skip to content

Commit

Permalink
[feat] bulk add volunteers
Browse files Browse the repository at this point in the history
  • Loading branch information
gregv committed Aug 31, 2024
1 parent 002d19c commit 7328b28
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
87 changes: 86 additions & 1 deletion api/messages/messages_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -787,6 +787,91 @@ def update_npo(json):
"Updated NPO"
)


@limits(calls=100, period=ONE_MINUTE)
def bulk_add_volunteers(event_id, json, propel_id):
db = get_db()
logger.info("Bulk Add Volunteers")
logger.info("JSON: " + str(json))
send_slack_audit(action="bulk_add_volunteers", message="Adding", 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"]
volunteers = json["volunteers"]
# We will use timestamp and name to find the volunteer

if volunteer_type == "mentors":
logger.info("Adding Mentors")
# Get the mentor block
mentor_block = doc.get().to_dict()["mentors"]
# Add the new mentors
for mentor in volunteers:
# Do some sanity checks
if "name" not in mentor:
logger.info("Skipping mentor with no name")
continue
if "timestamp" not in mentor:
logger.info("Skipping mentor with no timestamp")
continue
if mentor["name"] == "" or mentor["timestamp"] == "":
logger.info("Skipping mentor with empty name, timestamp or email")
continue

mentor["created_by"] = admin_name
mentor["created_timestamp"] = datetime.now().isoformat()
mentor_block.append(mentor)
# Update the mentor block with the new mentors
doc.update({
"mentors": mentor_block
})
elif volunteer_type == "judges":
logger.info("Adding Judges")
# Get the judge block
judge_block = doc.get().to_dict()["judges"]
# Add the new judges
for judge in volunteers:
# Do some sanity checks
if "name" not in judge:
logger.info("Skipping judge with no name")
continue
if "timestamp" not in judge:
logger.info("Skipping judge with no timestamp")
continue
if judge["name"] == "" or judge["timestamp"] == "":
logger.info("Skipping judge with empty name, timestamp")
continue

judge["created_by"] = admin_name
judge["created_timestamp"] = datetime.now().isoformat()
judge_block.append(judge)
# Update the judge block with the new judges
doc.update({
"judges": judge_block
})

# Clear cache
get_single_hackathon_event.cache_clear()

return Message(
"Added Hackathon Volunteers"
)


@limits(calls=50, period=ONE_MINUTE)
def update_hackathon_volunteers(event_id, json, propel_id):
db = get_db()
Expand Down Expand Up @@ -840,7 +925,7 @@ def update_hackathon_volunteers(event_id, json, propel_id):
doc.update({
"mentors": mentor_block
})
elif volunteer_type == "judge":
elif volunteer_type == "judges":
# Get the judge block
judge_block = doc.get().to_dict()["judges"]
# Find the judge
Expand Down
10 changes: 10 additions & 0 deletions api/messages/messages_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
get_npo_list,
get_single_npo,
get_single_hackathon_event,
bulk_add_volunteers,
get_single_hackathon_id,
save_hackathon,
update_hackathon_volunteers,
Expand Down Expand Up @@ -125,6 +126,15 @@ def update_hackathon_volunteers_mentors_judges(event_id):
return vars(update_hackathon_volunteers(event_id, request.get_json(), auth_user.user_id))


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



@bp.route("/hackathons", methods=["GET"])
def list_hackathons():
arg = request.args.get("current")
Expand Down

0 comments on commit 7328b28

Please sign in to comment.