From 7baa274d02842d12c2c21b3db0ea10791a639c3d Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Thu, 2 Dec 2021 21:30:42 +0100 Subject: [PATCH] feat(backend): Remove deleted user from all futur services --- backend/flaschengeist_events/__init__.py | 1 + backend/flaschengeist_events/event_controller.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/backend/flaschengeist_events/__init__.py b/backend/flaschengeist_events/__init__.py index a1f386f..c32e388 100644 --- a/backend/flaschengeist_events/__init__.py +++ b/backend/flaschengeist_events/__init__.py @@ -20,3 +20,4 @@ class EventPlugin(Plugin): def __init__(self, cfg): super(EventPlugin, self).__init__(cfg) from . import routes + from .event_controller import clear_services diff --git a/backend/flaschengeist_events/event_controller.py b/backend/flaschengeist_events/event_controller.py index a59dfa8..4ee543e 100644 --- a/backend/flaschengeist_events/event_controller.py +++ b/backend/flaschengeist_events/event_controller.py @@ -1,6 +1,7 @@ from datetime import datetime, timedelta, timezone from enum import IntEnum from typing import Optional, Tuple +from flaschengeist.models import UtcDateTime from werkzeug.exceptions import BadRequest, Conflict, NotFound from sqlalchemy.exc import IntegrityError @@ -8,6 +9,7 @@ from sqlalchemy.orm.util import was_deleted from flaschengeist import logger from flaschengeist.database import db +from flaschengeist.plugins import before_delete_user from flaschengeist.utils.scheduler import scheduled from . import EventPlugin @@ -27,6 +29,19 @@ class NotifyType(IntEnum): INVITATION_REJECTED = 0x11 +@before_delete_user +def clear_services(user): + """ + This is called when an user got deleted so it cleans future services. + It removes the deleted user from all future events. + """ + logger.debug(f"Clear deleted user {user.userid} from future events.") + _, jobs = get_jobs(user, UtcDateTime.current_utc()) + for job in jobs: + job.services = list(filter(lambda s: s.user_ != user, job.services)) + db.session.commit() + + def update(): db.session.commit()