[events] Hotfix: delete an event with registered jobs

This commit is contained in:
Ferdinand Thiessen 2021-11-11 15:22:55 +01:00
parent 3d833fb6af
commit f7e07fdade
2 changed files with 34 additions and 13 deletions

View File

@ -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,17 +203,15 @@ 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)
else:
if service:
service.value = value
else:
@ -221,6 +220,25 @@ def assign_to_job(job: Job, user, value):
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")

View File

@ -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