Example #1
0
def reserve(request, id):
    bookable = get_object_or_404(Bookable, pk=id)

    if not request.user.has_perm('booking.add_reservation'):
        messages.error(request,
                       "You do not have permission to add a reservation.")
        return redirect(host, id)

    if request.method == 'POST':

        reservation = Reservation(
            bookable=bookable,
            owner=get_object_or_404(User, pk=request.POST['owner'])
            if request.POST['owner'] else request.user,
            start=make_aware(
                datetime.strptime(request.POST['start'], "%Y-%m-%d")),
            comment=request.POST['comment'])

        try:
            reservation.save()
        except ValidationError as e:
            form = ReservationForm(instance=reservation)
            form.errors.update(e)
            return render(request, 'www/booking/reserve.html', {
                'form': form,
                'bookable': bookable
            })

        mail.send_reservation_confirmation(reservation)

        messages.success(request, 'Reservation created.')
        return redirect(host, id)

    form = ReservationForm(initial={'owner': request.user})

    return render(request, 'www/booking/reserve.html', {
        'form': form,
        'bookable': bookable
    })
Example #2
0
def save_reservation(desk_id, period, user):
    r = Reservation()
    r.user = user
    r.desk = Desk.objects.select_for_update().get(pk = desk_id)
    period.save()
    period.final_price = period.get_final_price()
    period.save()
    r.period_id = period.id
    for resv in Reservation.objects.select_for_update().filter(desk__id=desk_id):
        if resv.period.intersects_with(period):
            raise Exception("Te biurko zostało już zarezerwowane przez kogoś innego")
    r.save()
    log.debug("rezerwacja: ")
    log.debug(r)
Example #3
0
def add_reservation(request, room_id):
    if request.method == 'POST':
        date = request.POST.get('date')
        current_date = datetime.datetime.strptime(date, '%Y-%m-%d').date()
        comment = request.POST.get('comment')
        today = datetime.date.today()
        reservations = Reservation.objects.filter(date=date).filter(room=room_id)
        if len(reservations) > 0:
            return HttpResponse("Sala jest zajęta na dany dzień")
        if current_date < today:
            return HttpResponse("Na cebulaka tylko do przodu rezerwujemy")
        new_reservation = Reservation()
        new_reservation.date =current_date
        new_reservation.room_id=room_id
        new_reservation.comment=comment
        new_reservation.save()
        return redirect(reverse('main-page'))
Example #4
0
def create_reservation(user, resource, time):
    start = later(time)
    end = later(time + 1)
    res = Reservation(user=user, resource=resource, start=start, end=end)
    res.save()
    return res
Example #5
0
def create_reservation(user, resource, start, end):
    res = Reservation(user=user, resource=resource, start=start, end=end)
    res.save()
    return res
Example #6
0
def create_reservation(user, resource, start, end):
    res = Reservation(user=user, resource=resource, start=start, end=end)
    res.save()
    return res
Example #7
0
def create_reservation(user, resource, time):
    start = later(time)
    end = later(time + 1)
    res = Reservation(user=user, resource=resource, start=start, end=end)
    res.save()
    return res
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})