Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Handle adding volunteers separate from mentors and judges #83

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 126 additions & 1 deletion api/messages/messages_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ def update_npo_application( application_id, json, propel_id):
logger.info(f"Clearing cache for application_id={application_id}")

clear_cache()

return Message(
"Updated NPO Application"
)
Expand Down Expand Up @@ -835,6 +835,81 @@ def update_npo(json):
)


@limits(calls=100, period=ONE_MINUTE)
def single_add_volunteer(event_id, json, propel_id):
db = get_db()
logger.info("Single Add Volunteer")
logger.info("JSON: " + str(json))
send_slack_audit(action="single_add_volunteer", 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"]

# This will help to make sure we don't always have all data in the Google Sheets/Forms and can add it later
fields_that_should_always_be_present = ["name", "timestamp"]

if volunteer_type == "mentors":
logger.info("Adding Mentor")
# Get the mentor block
mentor_block = doc.get().to_dict()["mentors"]
# Add the new mentor
mentor = json
mentor["created_by"] = admin_name
mentor["created_timestamp"] = datetime.now().isoformat()
mentor_block.append(mentor)
# Update the mentor block with the new mentor
doc.update({
"mentors": mentor_block
})
elif volunteer_type == "judges":
logger.info("Adding Judge")
# Get the judge block
judge_block = doc.get().to_dict()["judges"]
# Add the new judge
judge = json
judge["created_by"] = admin_name
judge["created_timestamp"] = datetime.now().isoformat()
judge_block.append(judge)
# Update the judge block with the new judge
doc.update({
"judges": judge_block
})
elif volunteer_type == "volunteers":
logger.info("Adding Volunteer")
# Get the volunteer block
volunteer_block = doc.get().to_dict()["volunteers"]
# Add the new volunteer
volunteer = json
volunteer["created_by"] = admin_name
volunteer["created_timestamp"] = datetime.now().isoformat()
volunteer_block.append(volunteer)
# Update the volunteer block with the new volunteer
doc.update({
"volunteers": volunteer_block
})

# Clear cache
get_single_hackathon_event.cache_clear()

return Message(
"Added Hackathon Volunteer"
)

@limits(calls=100, period=ONE_MINUTE)
def bulk_add_volunteers(event_id, json, propel_id):
db = get_db()
Expand Down Expand Up @@ -910,6 +985,30 @@ def bulk_add_volunteers(event_id, json, propel_id):
doc.update({
"judges": judge_block
})
elif volunteer_type == "volunteers":
logger.info("Adding Volunteers")
# Get the volunteer block
volunteer_block = doc.get().to_dict()["volunteers"]
# Add the new volunteers
for volunteer in volunteers:
# Do some sanity checks
if "name" not in volunteer:
logger.info("Skipping volunteer with no name")
continue
if "timestamp" not in volunteer:
logger.info("Skipping volunteer with no timestamp")
continue
if volunteer["name"] == "" or volunteer["timestamp"] == "":
logger.info("Skipping volunteer with empty name, timestamp")
continue

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

# Clear cache
get_single_hackathon_event.cache_clear()
Expand Down Expand Up @@ -1007,6 +1106,32 @@ def update_hackathon_volunteers(event_id, json, propel_id):
doc.update({
"judges": judge_block
})
elif volunteer_type == "volunteers":
# Get the volunteer block
volunteer_block = doc.get().to_dict()["volunteers"]
# Find the volunteer
for volunteer in volunteer_block:
# Check if fields_that_should_always_be_present are present and if not add empty string
for field in fields_that_should_always_be_present:
if field not in volunteer:
volunteer[field] = ""

if volunteer["name"] == name and volunteer["timestamp"] == timestamp:
# For each field in volunteer, update with the new value from json
for key in volunteer.keys():
if key in json:
if key == "timestamp" or key == "name":
continue
volunteer[key] = json[key]
volunteer["updated_timestamp"] = datetime.now().isoformat()
volunteer["updated_by"] = admin_name

logger.info(f"Found volunteer {volunteer}")
break
# Update the volunteer block with the json for this volunteer
doc.update({
"volunteers": volunteer_block
})

# Clear cache
get_single_hackathon_event.cache_clear()
Expand Down
8 changes: 7 additions & 1 deletion api/messages/messages_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
get_single_npo,
get_single_hackathon_event,
bulk_add_volunteers,
single_add_volunteer,
get_single_hackathon_id,
save_hackathon,
update_hackathon_volunteers,
Expand Down Expand Up @@ -147,7 +148,12 @@ 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("/hackathon/<event_id>/volunteers", methods=["POST"])
@auth.require_user
@auth.require_org_member_with_permission("volunteer.admin", req_to_org_id=getOrgId)
def add_single_volunteer(event_id):
if auth_user and auth_user.user_id:
return vars(single_add_volunteer(event_id, request.get_json(), auth_user.user_id))

@bp.route("/hackathons", methods=["GET"])
def list_hackathons():
Expand Down
Loading