From 9c4e65a2cba550571472c6219ed19182158893b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Krienb=C3=BChl?= Date: Fri, 28 Aug 2015 10:48:33 +0200 Subject: [PATCH] Expired reservation sessions are now automatically removed --- HISTORY.rst | 3 +++ onegov/town/models/resource.py | 30 +++++++++++++++++++++++++++--- onegov/town/views/reservation.py | 6 ++++-- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index e28700c..deed9b8 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -4,6 +4,9 @@ Changelog Unreleased ~~~~~~~~~~ +- Expired reservation sessions are now automatically removed. + [href] + - Adds the ability to create reservations and to accept/reject them. [href] diff --git a/onegov/town/models/resource.py b/onegov/town/models/resource.py index 3e2ba5d..a827ea8 100644 --- a/onegov/town/models/resource.py +++ b/onegov/town/models/resource.py @@ -1,4 +1,6 @@ +from libres.db.models import Reservation from onegov.libres.models import Resource +from onegov.form.models import FormSubmission from onegov.town.models.extensions import ( HiddenFromPublicExtension, ContactExtension, @@ -6,7 +8,7 @@ ) -class DeletableMixin(object): +class SharedMethods(object): def deletable(self, libres_context): scheduler = self.get_scheduler(libres_context) @@ -19,12 +21,34 @@ def deletable(self, libres_context): return True + def remove_expired_reservation_sessions(self, libres_context, + expiration_date=None): + session = libres_context.get_service('session_provider').session() + scheduler = self.get_scheduler(libres_context) + + find = scheduler.queries.find_expired_reservation_sessions + remove = scheduler.queries.remove_expired_reservation_sessions + + expired_sessions = find(expiration_date) + + query = session.query(Reservation).with_entities(Reservation.token) + query = query.filter(Reservation.session_id.in_(expired_sessions)) + tokens = set(r[0] for r in query.all()) + + remove(expiration_date) + + query = session.query(FormSubmission) + query = query.filter(FormSubmission.name == None) + query = query.filter(FormSubmission.id.in_(tokens)) + + query.delete('fetch') + class DaypassResource(Resource, HiddenFromPublicExtension, - ContactExtension, PersonLinkExtension, DeletableMixin): + ContactExtension, PersonLinkExtension, SharedMethods): __mapper_args__ = {'polymorphic_identity': 'daypass'} class RoomResource(Resource, HiddenFromPublicExtension, - ContactExtension, PersonLinkExtension, DeletableMixin): + ContactExtension, PersonLinkExtension, SharedMethods): __mapper_args__ = {'polymorphic_identity': 'room'} diff --git a/onegov/town/views/reservation.py b/onegov/town/views/reservation.py index 5d5d960..31d0963 100644 --- a/onegov/town/views/reservation.py +++ b/onegov/town/views/reservation.py @@ -17,8 +17,6 @@ from webob import exc - - def get_reservation_form_class(allocation, request): return ReservationForm.for_allocation(allocation) @@ -57,6 +55,10 @@ def handle_reserve_allocation(self, request, form): except LibresError as e: utils.show_libres_error(e, request) else: + # while we re at it, remove all expired sessions + resource.remove_expired_reservation_sessions( + request.app.libres_context) + # though it's possible for a token to have multiple reservations, # it is not something that can happen here -> therefore one! reservation = scheduler.reservations_by_token(token).one()