Skip to content
This repository has been archived by the owner on Sep 5, 2019. It is now read-only.

Commit

Permalink
Expired reservation sessions are now automatically removed
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis Krienbühl committed Aug 28, 2015
1 parent 16f0338 commit 9c4e65a
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
30 changes: 27 additions & 3 deletions onegov/town/models/resource.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
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,
PersonLinkExtension
)


class DeletableMixin(object):
class SharedMethods(object):

def deletable(self, libres_context):
scheduler = self.get_scheduler(libres_context)
Expand All @@ -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'}
6 changes: 4 additions & 2 deletions onegov/town/views/reservation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
from webob import exc




def get_reservation_form_class(allocation, request):
return ReservationForm.for_allocation(allocation)

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit 9c4e65a

Please sign in to comment.