From fcfe28b81e99b3228ae9af05a4852f5f8f39bdd9 Mon Sep 17 00:00:00 2001 From: Jon Wood Date: Sat, 25 May 2024 22:23:15 +0000 Subject: [PATCH] Some hateful code to deal with a hateful problem --- apps/volunteer/main.py | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/apps/volunteer/main.py b/apps/volunteer/main.py index f3787a8b1..3ecdb5a55 100644 --- a/apps/volunteer/main.py +++ b/apps/volunteer/main.py @@ -119,26 +119,36 @@ def init_workshop_shifts(): # late to do anything about that right now. role = Role.query.filter_by(name="Workshop Steward").first() + venues = {} with db.session.no_autoflush: for proposal in proposals: + # Because VolunteerVenues are complete distinct from Venues, despite being the same physical location. + # + # Also you can end up with multiple new venues with the same name all trying to insert at once and then + # breaking unless you keep track of which ones you already inserted. This all sucks. + if proposal.scheduled_venue.name in venues: + venue = venues[proposal.scheduled_venue.name] + else: + venue = VolunteerVenue.query.filter_by(name=proposal.scheduled_venue.name).first() + if venue is None: + location = to_shape(proposal.scheduled_venue.location) + mapref = f"https://map.emfcamp.org/#20.82/{location.y}/{location.x}" + venue = VolunteerVenue(name=proposal.scheduled_venue.name, mapref=mapref) + db.session.add(venue) + venues[proposal.scheduled_venue.name] = venue + shift = Shift.query.filter_by(proposal=proposal, role=role).first() if shift is None: - shift = Shift(proposal=proposal, role=role) - - # Because VolunteerVenues are complete distinct from Venues, despite being the same physical location. - venue = VolunteerVenue.query.filter_by(name=proposal.scheduled_venue.name).first() - if venue is None: - location = to_shape(proposal.scheduled_venue.location) - mapref = f"https://map.emfcamp.org/#20.82/{location.y}/{location.x}" - venue = VolunteerVenue(name=proposal.scheduled_venue.name, mapref=mapref) + shift = Shift(proposal=proposal, role=role, venue=venue) shift.start = proposal.scheduled_time - time_before_start shift.end = proposal.scheduled_time + time_after_start - shift.venue = venue shift.min_needed = 1 shift.max_needed = 1 db.session.add(shift) + db.session.commit() + db.session.commit() return redirect(url_for(".schedule"))