def overwrite_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)))
    if len(r) < 1:
        return http_badrequest(_('No reservation with that id.'))
    reservation = r[0]

    if request.user.id == reservation.user_id:
        return http_forbidden(_('You may not overwrite your own reservations.'))
    elif reservation.is_solid():
        return http_forbidden(_('You may not overwrite solid reservations.'))

    # Dispatch to do_make_reservation which does all necessary testing for
    # overwrites anyway.
    return do_make_reservation(reservation.start, reservation.end, reservation.resource_id, request.user)
def overwrite_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)))
    if len(r) < 1:
        return http_badrequest(_('No reservation with that id.'))
    reservation = r[0]

    if request.user.id == reservation.user_id:
        return http_forbidden(
            _('You may not overwrite your own reservations.'))
    elif reservation.is_solid():
        return http_forbidden(_('You may not overwrite solid reservations.'))

    # Dispatch to do_make_reservation which does all necessary testing for
    # overwrites anyway.
    return do_make_reservation(reservation.start, reservation.end,
                               reservation.resource_id, request.user)
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 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})