Skip to content

Commit

Permalink
Simplify the recurrence->allocation relationship configuration (casca…
Browse files Browse the repository at this point in the history
…ding cannot work there) and add a function to remove orphan recurrences. Fixes #52
  • Loading branch information
href committed Oct 30, 2013
1 parent 4766fdd commit 0db0286
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
23 changes: 12 additions & 11 deletions seantis/reservation/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ def unblock_periods(reservation):
query.delete('fetch')


@serialized
def remove_orphan_recurrences():
orphans = Session.query(Recurrence).filter(~Recurrence.allocations.any())
orphans.delete(synchronize_session=False)


def grouped_reservation_view(query):
"""Takes a query of reserved slots joined with allocations and uses it
to return reservation uuid, allocation id and the start and end dates
Expand Down Expand Up @@ -354,7 +360,9 @@ def extinguish_resource(resource_uuid):
scheduler.managed_reservations().delete('fetch')
scheduler.managed_reserved_slots().delete('fetch')
scheduler.managed_allocations().delete('fetch')
#XXX delete recurrences?
scheduler.managed_blocked_periods().delete('fetch')

remove_orphan_recurrences()


class Scheduler(object):
Expand Down Expand Up @@ -426,7 +434,7 @@ def managed_reserved_slots(self):
return query

def managed_blocked_periods(self):
"""" The block_periods managed by this scheduler / resource. """
""" The block_periods managed by this scheduler / resource. """
query = Session.query(BlockedPeriod)
query = query.filter(BlockedPeriod.resource == self.uuid)

Expand Down Expand Up @@ -855,18 +863,11 @@ def remove_allocation(self, id=None, group=None, recurrence_id=None):
allocation.pending_reservations[0]
)

# remove recurrence as well if this is the last allocation
if len(allocations) == 1 and allocations[0].recurrence:
allocation = allocations[0]
if len(allocation.recurrence.allocations) == 1:
recurrence_id = allocation.recurrence_id
assert recurrence_id

for allocation in allocations:
if not allocation.is_transient:
Session.delete(allocation)
if recurrence_id:
Session.delete(Session.query(Recurrence).get(recurrence_id))

remove_orphan_recurrences()

def _reserve_sanity_check(self, dates, quota):
"""Check the request for saneness. If any requested date cannot be
Expand Down
10 changes: 5 additions & 5 deletions seantis/reservation/models/allocation.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from sqlalchemy.schema import UniqueConstraint
from sqlalchemy.orm import object_session
from sqlalchemy.orm import relation
from sqlalchemy.orm import backref
from sqlalchemy.orm.util import has_identity

from seantis.reservation import ORMBase
Expand Down Expand Up @@ -75,11 +76,10 @@ class Allocation(TimestampMixin, ORMBase, OtherModels):
_end = Column(types.DateTime(), nullable=False)
_raster = Column(types.Integer(), nullable=False)

recurrence_id = Column(types.Integer(),
ForeignKey('recurrences.id',
onupdate='cascade',
ondelete='cascade'))
recurrence = relation('Recurrence', lazy='joined')
recurrence_id = Column(types.Integer(), ForeignKey('recurrences.id'))
recurrence = relation('Recurrence', lazy='joined', backref=backref(
'allocations', lazy='joined'
))

__table_args__ = (
Index('mirror_resource_ix', 'mirror_of', 'resource'),
Expand Down
2 changes: 0 additions & 2 deletions seantis/reservation/models/recurrence.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from seantis.reservation import ORMBase
from seantis.reservation.models.other import OtherModels
from seantis.reservation.models.timestamp import TimestampMixin
from sqlalchemy.orm import relation
from sqlalchemy.schema import Column
from sqlalchemy.types import Integer
from sqlalchemy.types import String
Expand All @@ -16,4 +15,3 @@ class Recurrence(TimestampMixin, ORMBase, OtherModels):

id = Column(Integer, primary_key=True, autoincrement=True)
rrule = Column(String)
allocations = relation('Allocation', lazy='joined')
35 changes: 35 additions & 0 deletions seantis/reservation/tests/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -1776,3 +1776,38 @@ def test_remove_reservation_from_session_removes_blocked_periods(self):
self.assertEqual(sc2.managed_blocked_periods().count(), 1)
sc1.remove_reservation_from_session(session_id, token)
self.assertEqual(sc2.managed_blocked_periods().count(), 0)

@serialized
def test_extinguish_resource(self):
sc = Scheduler(new_uuid())

dates = [
(datetime(2013, 10, 29, 8, 0), datetime(2013, 10, 29, 9, 0)),
(datetime(2013, 10, 30, 8, 0), datetime(2013, 10, 30, 9, 0))
]

sc.allocate(dates, rrule='RRULE:FREQ=DAILY;COUNT=1')
sc.approve_reservation(sc.reserve(reservation_email, (
datetime(2013, 10, 29, 8, 30),
datetime(2013, 10, 29, 9, 0),

)))

block_token = new_uuid()
sc.block_period(
datetime(2013, 10, 29, 8, 0),
datetime(2013, 10, 29, 8, 30),
block_token
)

self.assertEqual(sc.managed_allocations().count(), 2)
self.assertEqual(sc.managed_reserved_slots().count(), 1)
self.assertEqual(sc.managed_blocked_periods().count(), 1)
self.assertEqual(Session.query(Recurrence).count(), 1)

db.extinguish_resource(sc.uuid)

self.assertEqual(sc.managed_allocations().count(), 0)
self.assertEqual(sc.managed_reserved_slots().count(), 0)
self.assertEqual(sc.managed_blocked_periods().count(), 0)
self.assertEqual(Session.query(Recurrence).count(), 0)

0 comments on commit 0db0286

Please sign in to comment.