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})