def reserve_tool(request): tool = Tool.objects.get(id=request.POST["tool_id"]) customer = User.objects.get(id=request.POST["customer_id"]) project = Project.objects.get(id=request.POST["project_id"]) back = request.POST["back"] error_dictionary = { "back": back, "tool": tool, "project": project, "customer": customer } """ Create a reservation for a user. """ try: date = parse_date(request.POST["date"]) start = localize( datetime.combine(date, parse_time(request.POST["start"]))) end = localize(datetime.combine(date, parse_time(request.POST["end"]))) except: error_dictionary[ "message"] = "Please enter a valid date, start time, and end time for the reservation." return render(request, "kiosk/error.html", error_dictionary) # Create the new reservation: reservation = Reservation() reservation.project = project reservation.user = customer reservation.creator = customer reservation.reservation_item = tool reservation.start = start reservation.end = end reservation.short_notice = determine_insufficient_notice(tool, start) policy_problems, overridable = check_policy_to_save_reservation( cancelled_reservation=None, new_reservation=reservation, user_creating_reservation=customer, explicit_policy_override=False, ) # If there was a problem in saving the reservation then return the error... if policy_problems: error_dictionary["message"] = policy_problems[0] return render(request, "kiosk/error.html", error_dictionary) # All policy checks have passed. if project is None and not customer.is_staff: error_dictionary[ "message"] = "You must specify a project for your reservation" return render(request, "kiosk/error.html", error_dictionary) reservation.additional_information, reservation.self_configuration = extract_configuration( request) # Reservation can't be short notice if the user is configuring the tool themselves. if reservation.self_configuration: reservation.short_notice = False reservation.save_and_notify() return render(request, "kiosk/success.html", { "new_reservation": reservation, "customer": customer })
def modify_reservation(request, start_delta, end_delta): """ Cancel the user's old reservation and create a new one. Reservations are cancelled and recreated so that reservation abuse can be tracked if necessary. This function should be called by other views and should not be tied directly to a URL. """ try: reservation_to_cancel = Reservation.objects.get(pk=request.POST.get('id')) except Reservation.DoesNotExist: return HttpResponseNotFound("The reservation that you wish to modify doesn't exist!") explicit_policy_override = False try: explicit_policy_override = request.POST['explicit_policy_override'] == 'true' except: pass response = check_policy_to_cancel_reservation(reservation_to_cancel, request.user) # Do not move the reservation if the user was not authorized to cancel it. if response.status_code != HTTPStatus.OK: return response # Record the current time so that the timestamp of the cancelled reservation and the new reservation match exactly. now = timezone.now() # Cancel the user's original reservation. reservation_to_cancel.cancelled = True reservation_to_cancel.cancellation_time = now reservation_to_cancel.cancelled_by = request.user # Create a new reservation for the user. new_reservation = Reservation() new_reservation.title = reservation_to_cancel.title new_reservation.creator = request.user new_reservation.additional_information = reservation_to_cancel.additional_information # A change in start time will only be provided if the reservation is being moved. new_reservation.start = reservation_to_cancel.start new_reservation.self_configuration = reservation_to_cancel.self_configuration new_reservation.short_notice = False if start_delta: new_reservation.start += start_delta if new_reservation.self_configuration: # Reservation can't be short notice since the user is configuring the tool themselves. new_reservation.short_notice = False elif new_reservation.tool: new_reservation.short_notice = determine_insufficient_notice(reservation_to_cancel.tool, new_reservation.start) # A change in end time will always be provided for reservation move and resize operations. new_reservation.end = reservation_to_cancel.end + end_delta new_reservation.reservation_item = reservation_to_cancel.reservation_item new_reservation.project = reservation_to_cancel.project new_reservation.user = reservation_to_cancel.user new_reservation.creation_time = now policy_problems, overridable = check_policy_to_save_reservation(cancelled_reservation=reservation_to_cancel, new_reservation=new_reservation, user_creating_reservation=request.user, explicit_policy_override=explicit_policy_override) if policy_problems: reservation_action = "resize" if start_delta is None else "move" return render(request, 'calendar/policy_dialog.html', {'policy_problems': policy_problems, 'overridable': overridable and request.user.is_staff, 'reservation_action': reservation_action}) else: # All policy checks passed, so save the reservation. new_reservation.save_and_notify() reservation_to_cancel.descendant = new_reservation reservation_to_cancel.save_and_notify() return reservation_success(request, new_reservation)
def make_reservation(request): """ Create a reservation for a user. """ try: date = parse_date(request.POST['date']) start = localize( datetime.combine(date, parse_time(request.POST['start']))) end = localize(datetime.combine(date, parse_time(request.POST['end']))) except: return render( request, 'mobile/error.html', { 'message': 'Please enter a valid date, start time, and end time for the reservation.' }) item_type = ReservationItemType(request.POST['item_type']) item = get_object_or_404(item_type.get_object_class(), id=request.POST.get('item_id')) # Create the new reservation: reservation = Reservation() reservation.user = request.user reservation.creator = request.user reservation.reservation_item = item reservation.start = start reservation.end = end if item_type == ReservationItemType.TOOL: reservation.short_notice = determine_insufficient_notice(item, start) else: reservation.short_notice = False policy_problems, overridable = check_policy_to_save_reservation( cancelled_reservation=None, new_reservation=reservation, user_creating_reservation=request.user, explicit_policy_override=False) # If there was a problem in saving the reservation then return the error... if policy_problems: return render(request, 'mobile/error.html', {'message': policy_problems[0]}) # All policy checks have passed. try: reservation.project = Project.objects.get( id=request.POST['project_id']) except: if not request.user.is_staff: return render( request, 'mobile/error.html', {'message': 'You must specify a project for your reservation'}) reservation.additional_information, reservation.self_configuration = extract_configuration( request) # Reservation can't be short notice if the user is configuring the tool themselves. if reservation.self_configuration: reservation.short_notice = False reservation.save_and_notify() return render(request, 'mobile/reservation_success.html', {'new_reservation': reservation})