[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,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")

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