From f7e07fdadec4ffdd0490ff08a5dfd033e7e1badc Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 11 Nov 2021 15:22:55 +0100 Subject: [PATCH] [events] Hotfix: delete an event with registered jobs --- .../plugins/events/event_controller.py | 42 +++++++++++++------ flaschengeist/plugins/events/routes.py | 5 ++- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/flaschengeist/plugins/events/event_controller.py b/flaschengeist/plugins/events/event_controller.py index 3211363..1bc8b43 100644 --- a/flaschengeist/plugins/events/event_controller.py +++ b/flaschengeist/plugins/events/event_controller.py @@ -153,8 +153,9 @@ def delete_event(event_id): Raises: NotFound if not found """ - event = get_event(event_id) - logger.info(f"{type(event)} {event.__str__}") + event = get_event(event_id, True) + for job in event.jobs: + delete_job(job) db.session.delete(event) db.session.commit() @@ -202,25 +203,42 @@ def update(): def delete_job(job: Job): + for service in job.services: + unassign_job(service=service, notify=True) db.session.delete(job) db.session.commit() -def assign_to_job(job: Job, user, value): +def assign_job(job: Job, user, value): + assert value > 0 service = Service.query.get((job.id, user.id_)) - if value < 0: - if not service: - raise BadRequest - db.session.delete(service) + if service: + service.value = value else: - if service: - service.value = value - else: - service = Service(user_=user, value=value, job_=job) - db.session.add(service) + service = Service(user_=user, value=value, job_=job) + db.session.add(service) db.session.commit() +def unassign_job(job: Job = None, user=None, service=None, notify=False): + if service is None: + assert(job is not None and user is not None) + service = Service.query.get((job.id, user.id_)) + else: + user = service.user_ + if not service: + raise BadRequest + + event_id = service.job_.event_id_ + + db.session.delete(service) + db.session.commit() + if notify: + EventPlugin.plugin.notify( + user, "Your assignmet was cancelled", {"event_id": event_id} + ) + + @scheduled def assign_backups(): logger.debug("Notifications") diff --git a/flaschengeist/plugins/events/routes.py b/flaschengeist/plugins/events/routes.py index 7fccccf..fce9416 100644 --- a/flaschengeist/plugins/events/routes.py +++ b/flaschengeist/plugins/events/routes.py @@ -415,7 +415,10 @@ def update_job(event_id, job_id, current_session: Session): user != current_session.user_ and not current_session.user_.has_permission(permissions.ASSIGN_OTHER) ): raise Forbidden - event_controller.assign_to_job(job, user, value) + if value > 0: + event_controller.assign_job(job, user, value) + else: + event_controller.unassign_job(job, user, notify=user != current_session.user_) except (KeyError, ValueError): raise BadRequest