def test_simple_add(self): # Add one slot together with a timespan allocation = Allocation(raster=15, resource=uuid()) allocation.start = datetime(2011, 1, 1, 15) allocation.end = datetime(2011, 1, 1, 15, 59) allocation.group = uuid() reservation = uuid() slot = ReservedSlot(resource=allocation.resource) slot.start = allocation.start slot.end = allocation.end slot.allocation = allocation slot.reservation = reservation allocation.reserved_slots.append(slot) # Ensure that the same slot cannot be doubly used anotherslot = ReservedSlot(resource=allocation.resource) anotherslot.start = allocation.start anotherslot.end = allocation.end anotherslot.allocation = allocation anotherslot.reservation = reservation Session.add(anotherslot) self.assertRaises(IntegrityError, Session.flush)
def approve_reservation(self, token): """ This function approves an existing reservation and writes the reserved slots accordingly. Returns a list with the reserved slots. """ reservation = self.reservation_by_token(token).one() # write out the slots slots_to_reserve = [] if reservation.target_type == u'group': dates = self.dates_by_group(reservation.target) else: dates = ((reservation.start, reservation.end),) # the reservation quota is simply implemented by multiplying the # dates which are approved dates = dates * reservation.quota for start, end in dates: for allocation in self.reservation_targets(start, end): for slot_start, slot_end in \ allocation.all_slots(start, end): slot = ReservedSlot() slot.start = slot_start slot.end = slot_end slot.resource = allocation.resource slot.reservation_token = token # the slots are written with the allocation allocation.reserved_slots.append(slot) slots_to_reserve.append(slot) # the allocation may be a fake one, in which case we # must make it realz yo if allocation.is_transient: Session.add(allocation) reservation.status = u'approved' if not slots_to_reserve: raise NotReservableError notify(ReservationApprovedEvent(reservation, self.language)) return slots_to_reserve