def delete_reservation(request):
    r_id = request.POST.get('id', None)
    if r_id is None:
        return http_badrequest(_('No request id provided.'))
    r = list(Reservation.objects.filter(id=int(r_id)))
    for reservation in r:
        if request.user.id != reservation.user_id:
            return http_forbidden(_('You may only delete your own events.'))
    for reservation in r:
            reservation.delete_and_report()

    return http_json_response({'status': 'success', 'deleted': len(r)})
def delete_reservation(request):
    r_id = request.POST.get('id', None)
    if r_id is None:
        return http_badrequest(_('No request id provided.'))
    r = list(Reservation.objects.filter(id=int(r_id)))
    for reservation in r:
        if request.user.id != reservation.user_id:
            return http_forbidden(_('You may only delete your own events.'))
    for reservation in r:
        reservation.delete_and_report()

    return http_json_response({'status': 'success', 'deleted': len(r)})
def get_reservations(request, resource_id):
    start_time = int(request.GET.get('start'))
    end_time = int(request.GET.get('end'))
    resource_id = int(resource_id)
    if not resource_id or not start_time or not end_time:
        return http_badrequest('')

    start_datetime = from_timestamp(start_time)
    # We need to add a day to endtime because of djangoisms
    # see https://docs.djangoproject.com/en/dev/ref/models/querysets/#range
    end_datetime = from_timestamp(end_time) + timedelta(days=1)
    reservations = Reservation.objects.filter(
        deleted=False,
        resource=resource_id,
        start__range=(start_datetime, end_datetime))
    reservations = filter(lambda r: r.valid_user(), reservations)
    add_reservation_annotations(request.user, reservations)

    return http_json_response(reservations_to_json_struct(reservations))
def get_reservations(request, resource_id):
    start_time = int(request.GET.get('start'))
    end_time = int(request.GET.get('end'))
    resource_id = int(resource_id)
    if not resource_id or not start_time or not end_time:
        return http_badrequest('')

    start_datetime = from_timestamp(start_time)
    # We need to add a day to endtime because of djangoisms
    # see https://docs.djangoproject.com/en/dev/ref/models/querysets/#range
    end_datetime = from_timestamp(end_time) + timedelta(days=1)
    reservations = Reservation.objects.filter(deleted=False,
                                              resource=resource_id,
                                              start__range=(start_datetime,
                                                            end_datetime))
    reservations = filter(lambda r: r.valid_user(), reservations)
    add_reservation_annotations(request.user, reservations)

    return http_json_response(reservations_to_json_struct(reservations))
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})