def test_sv_ts_sv(self): utc_now = common.utc_now() local_now = common.as_local_time(utc_now) ts = common.to_timestamp(local_now) utc_from_ts = common.from_timestamp(ts) local_from_ts = common.as_local_time(utc_from_ts) self.assert_approx_equal(utc_now, utc_from_ts) self.assert_approx_equal(local_now, local_from_ts) self.assertEqual(utc_now, local_now) self.assertEqual(utc_from_ts, local_from_ts)
def is_solid(self): if self.deleted: return False if self.in_past(): return True conflicting_reservations = Reservation.objects.filter( user=self.user, deleted=False, resource=self.resource, start__lt=self.start, start__gt=utc_now()) return conflicting_reservations.count() == 0
def test_utc_sv_conversions(self): now = common.utc_now() local_now = common.as_local_time(now) self.assertEqual(now, local_now) self.assertNotEquals(now.tzname(), local_now.tzname()) ts_sv = common.to_timestamp(local_now) ts_utc = common.to_timestamp(now) self.assertEqual(ts_sv, ts_utc) self.assert_approx_equal(common.from_timestamp(ts_sv), common.from_timestamp(ts_utc))
def later(hours_later): return utc_now() + timedelta(hours=hours_later)
def earlier(hours_earlier): return utc_now() - timedelta(hours=hours_earlier)
def test_utc_conversions(self): now = common.utc_now() now_ts = common.to_timestamp(now) now_from_ts = common.from_timestamp(now_ts) self.assert_approx_equal(now, now_from_ts)
def in_past(self): self.start < utc_now()
def do_make_reservation(start, end, resource_id, user): if not user.profile.completed(): return http_forbidden(_('You must complete your profile before making reservations.')) if user.profile.is_banned: return http_forbidden(_('You are banned. Stated reason: ') + user.profile.ban_reason + _(' Contact FRRyd for further information.')) interval = end - start max_interval = timedelta(hours=settings.MAX_RESERVATION_LENGTH) if (interval > max_interval): return http_forbidden(_('You may not reserve the resource for such a long time.')) now = utc_now() if (now > start) or (now > end): return http_forbidden(_('Start and end times must be in the future.')) if not (start < end): return http_forbidden(_('Start time must be before end time.')) outstanding_reservations = Reservation.objects.filter( deleted=False, user=user, end__gt=now, resource=resource_id).count() if outstanding_reservations > 1: return http_forbidden(_('You may only make two reservations per resource.')) possibly_concurrent_reservations = Reservation.objects.filter( deleted=False, user=user, end__gt=now) possibly_concurrent_reservations = filter(lambda r: r.valid_user(), possibly_concurrent_reservations) concurrent_reservations = filter(lambda r: r.would_overlap(start, end), possibly_concurrent_reservations) if len(concurrent_reservations) > 0: return http_forbidden(_('You may not reserve two resources at the same time.')) possibly_overlapping_reservations = Reservation.objects.filter( deleted=False, end__gt=now, resource=resource_id) possibly_overlapping_reservations = filter(lambda r: r.valid_user(), possibly_overlapping_reservations) # Check if solid reservations prevent this one to be made # Or if preliminary reservations prevent a preliminary reservation # from being made. for r in possibly_overlapping_reservations: if r.would_overlap(start, end): if outstanding_reservations > 0: return http_forbidden(_("You can't overwrite a reservation with a preliminary reservation.")) elif r.is_solid(): return http_forbidden(_('Somebody has already made a reservation here!')) # Mark overwriten preliminary bookings as deleted. overwritten_reservations = [] for r in possibly_overlapping_reservations: if r.would_overlap(start, end): r.delete_and_report() overwritten_reservations.append(r) new_reservation = Reservation(user=user, start=start, end=end) new_reservation.resource_id = resource_id new_reservation.save() # Document overwritten reservations for r in overwritten_reservations: doc_object = OverwriteLog( deleted_reservation=r, replacing_reservation=new_reservation) doc_object.save() return http_json_response({'status': 'success', 'id': new_reservation.id})
def do_make_reservation(start, end, resource_id, user): if not user.profile.completed(): return http_forbidden( _('You must complete your profile before making reservations.')) if user.profile.is_banned: return http_forbidden( _('You are banned. Stated reason: ') + user.profile.ban_reason + _(' Contact FRRyd for further information.')) interval = end - start max_interval = timedelta(hours=settings.MAX_RESERVATION_LENGTH) if (interval > max_interval): return http_forbidden( _('You may not reserve the resource for such a long time.')) now = utc_now() if (now > start) or (now > end): return http_forbidden(_('Start and end times must be in the future.')) if not (start < end): return http_forbidden(_('Start time must be before end time.')) outstanding_reservations = Reservation.objects.filter( deleted=False, user=user, end__gt=now, resource=resource_id).count() if outstanding_reservations > 1: return http_forbidden( _('You may only make two reservations per resource.')) possibly_concurrent_reservations = Reservation.objects.filter( deleted=False, user=user, end__gt=now) possibly_concurrent_reservations = filter( lambda r: r.valid_user(), possibly_concurrent_reservations) concurrent_reservations = filter(lambda r: r.would_overlap(start, end), possibly_concurrent_reservations) if len(concurrent_reservations) > 0: return http_forbidden( _('You may not reserve two resources at the same time.')) possibly_overlapping_reservations = Reservation.objects.filter( deleted=False, end__gt=now, resource=resource_id) possibly_overlapping_reservations = filter( lambda r: r.valid_user(), possibly_overlapping_reservations) # Check if solid reservations prevent this one to be made # Or if preliminary reservations prevent a preliminary reservation # from being made. for r in possibly_overlapping_reservations: if r.would_overlap(start, end): if outstanding_reservations > 0: return http_forbidden( _("You can't overwrite a reservation with a preliminary reservation." )) elif r.is_solid(): return http_forbidden( _('Somebody has already made a reservation here!')) # Mark overwriten preliminary bookings as deleted. overwritten_reservations = [] for r in possibly_overlapping_reservations: if r.would_overlap(start, end): r.delete_and_report() overwritten_reservations.append(r) new_reservation = Reservation(user=user, start=start, end=end) new_reservation.resource_id = resource_id new_reservation.save() # Document overwritten reservations for r in overwritten_reservations: doc_object = OverwriteLog(deleted_reservation=r, replacing_reservation=new_reservation) doc_object.save() return http_json_response({'status': 'success', 'id': new_reservation.id})