Exemplo n.º 1
0
def disable_tool(request, tool_id):
    if not settings.ALLOW_CONDITIONAL_URLS:
        return HttpResponseBadRequest(
            "Tool control is only available on campus.")

    tool = get_object_or_404(Tool, id=tool_id)
    if tool.get_current_usage_event() is None:
        return HttpResponse()
    downtime = timedelta(minutes=quiet_int(request.POST.get("downtime")))
    bypass_interlock = request.POST.get("bypass", 'False') == 'True'
    response = check_policy_to_disable_tool(tool, request.user, downtime)
    if response.status_code != HTTPStatus.OK:
        return response

    # All policy checks passed so disable the tool for the user.
    if tool.interlock and not tool.interlock.lock():
        if bypass_interlock and interlock_bypass_allowed(request.user):
            pass
        else:
            return interlock_error("Disable", request.user)

    # Shorten the user's tool reservation since we are now done using the tool
    shorten_reservation(user=request.user,
                        item=tool,
                        new_end=timezone.now() + downtime)

    # End the current usage event for the tool
    current_usage_event = tool.get_current_usage_event()
    current_usage_event.end = timezone.now() + downtime

    # Collect post-usage questions
    dynamic_form = DynamicForm(tool.post_usage_questions, tool.id)

    try:
        current_usage_event.run_data = dynamic_form.extract(request)
    except RequiredUnansweredQuestionsException as e:
        if request.user.is_staff and request.user != current_usage_event.operator and current_usage_event.user != request.user:
            # if a staff is forcing somebody off the tool and there are required questions, send an email and proceed
            current_usage_event.run_data = e.run_data
            email_managers_required_questions_disable_tool(
                current_usage_event.operator, request.user, tool, e.questions)
        else:
            return HttpResponseBadRequest(str(e))

    dynamic_form.charge_for_consumables(current_usage_event.user,
                                        current_usage_event.operator,
                                        current_usage_event.project,
                                        current_usage_event.run_data, request)
    dynamic_form.update_counters(current_usage_event.run_data)

    current_usage_event.save()
    user: User = request.user
    if user.charging_staff_time():
        existing_staff_charge = user.get_staff_charge()
        if (existing_staff_charge.customer == current_usage_event.user and
                existing_staff_charge.project == current_usage_event.project):
            response = render(request, "staff_charges/reminder.html",
                              {"tool": tool})

    return response
Exemplo n.º 2
0
def disable_tool(request):
    logger = getLogger(__name__)

    tool = Tool.objects.get(id=request.POST['tool_id'])
    customer = User.objects.get(id=request.POST['customer_id'])
    downtime = timedelta(minutes=quiet_int(request.POST.get('downtime')))
    response = check_policy_to_disable_tool(tool, customer, downtime)
    if response.status_code != HTTPStatus.OK:
        dictionary = {
            'message': response.content,
            'delay': 10,
        }
        return render(request, 'kiosk/acknowledgement.html', dictionary)
    try:
        current_reservation = Reservation.objects.get(start__lt=timezone.now(),
                                                      end__gt=timezone.now(),
                                                      cancelled=False,
                                                      missed=False,
                                                      shortened=False,
                                                      user=customer,
                                                      tool=tool)
        # Staff are exempt from mandatory reservation shortening when tool usage is complete.
        if customer.is_staff is False:
            # Shorten the user's reservation to the current time because they're done using the tool.
            new_reservation = deepcopy(current_reservation)
            new_reservation.id = None
            new_reservation.pk = None
            new_reservation.end = timezone.now()
            new_reservation.save()
            current_reservation.shortened = True
            current_reservation.descendant = new_reservation
            current_reservation.save()
    except Reservation.DoesNotExist:
        pass

    # All policy checks passed so disable the tool for the user.
    if tool.interlock and not tool.interlock.lock():
        logger.error(
            "The interlock command for this tool failed. The error message returned: "
            + str(tool.interlock.most_recent_reply))
        raise Exception(
            "The interlock command for this tool failed. The error message returned: "
            + str(tool.interlock.most_recent_reply))
    # End the current usage event for the tool and save it.
    current_usage_event = tool.get_current_usage_event()
    current_usage_event.end = timezone.now() + downtime

    # Collect post-usage questions
    current_usage_event.run_data = DynamicForm(
        tool.post_usage_questions).extract(request)
    current_usage_event.save()

    dictionary = {
        'message': 'You are no longer using the {}'.format(tool),
        'badge_number': customer.badge_number,
    }
    return render(request, 'kiosk/acknowledgement.html', dictionary)
Exemplo n.º 3
0
def do_disable_tool(request, tool_id):
    tool = Tool.objects.get(id=tool_id)
    customer = User.objects.get(id=request.POST["customer_id"])
    downtime = timedelta(minutes=quiet_int(request.POST.get("downtime")))
    bypass_interlock = request.POST.get("bypass", "False") == "True"
    response = check_policy_to_disable_tool(tool, customer, downtime)
    if response.status_code != HTTPStatus.OK:
        dictionary = {"message": response.content, "delay": 10}
        return render(request, "kiosk/acknowledgement.html", dictionary)

    # All policy checks passed so try to disable the tool for the user.
    if tool.interlock and not tool.interlock.lock():
        if bypass_interlock and interlock_bypass_allowed(customer):
            pass
        else:
            return interlock_error("Disable", customer)

    # Shorten the user's tool reservation since we are now done using the tool
    shorten_reservation(user=customer,
                        item=tool,
                        new_end=timezone.now() + downtime)

    # End the current usage event for the tool and save it.
    current_usage_event = tool.get_current_usage_event()
    current_usage_event.end = timezone.now() + downtime

    # Collect post-usage questions
    dynamic_form = DynamicForm(tool.post_usage_questions)

    try:
        current_usage_event.run_data = dynamic_form.extract(request)
    except RequiredUnansweredQuestionsException as e:
        if customer.is_staff and customer != current_usage_event.operator and current_usage_event.user != customer:
            # if a staff is forcing somebody off the tool and there are required questions, send an email and proceed
            current_usage_event.run_data = e.run_data
            email_managers_required_questions_disable_tool(
                current_usage_event.operator, customer, tool, e.questions)
        else:
            dictionary = {"message": str(e), "delay": 10}
            return render(request, "kiosk/acknowledgement.html", dictionary)

    dynamic_form.charge_for_consumables(
        current_usage_event.user,
        current_usage_event.operator,
        current_usage_event.project,
        current_usage_event.run_data,
        request,
    )
    dynamic_form.update_tool_counters(current_usage_event.run_data, tool.id)

    current_usage_event.save()
    dictionary = {
        "message": "You are no longer using the {}".format(tool),
        "badge_number": customer.badge_number
    }
    return render(request, "kiosk/acknowledgement.html", dictionary)
Exemplo n.º 4
0
def disable_tool(request, tool_id):


	if not settings.ALLOW_CONDITIONAL_URLS:
		return HttpResponseBadRequest('Tool control is only available on campus.')

	tool = get_object_or_404(Tool, id=tool_id)
	if tool.get_current_usage_event() is None:
		return HttpResponse()
	current_usage_event = tool.get_current_usage_event()
	downtime = timedelta(minutes=quiet_int(request.POST.get('downtime')))
	response = check_policy_to_disable_tool(tool, request.user, downtime)
	if response.status_code != HTTPStatus.OK:
		return response
	try:
		current_reservation = Reservation.objects.get(start__lt=timezone.now(), end__gt=timezone.now(), cancelled=False, missed=False, shortened=False, user=current_usage_event.user, tool=tool)
		# Staff are exempt from mandatory reservation shortening when tool usage is complete.
		if request.user.is_staff is False:
			# Shorten the user's reservation to the current time because they're done using the tool.
			new_reservation = deepcopy(current_reservation)
			new_reservation.id = None
			new_reservation.pk = None
			new_reservation.end = timezone.now() + downtime
			new_reservation.save()
			current_reservation.shortened = True
			current_reservation.descendant = new_reservation
			current_reservation.save()
	except Reservation.DoesNotExist:
		pass

	# All policy checks passed so disable the tool for the user.
	if tool.interlock and not tool.interlock.lock():
		error_message = f"The interlock command for the {tool} failed. The error message returned: {tool.interlock.most_recent_reply}"
		logger.error(error_message)
		return HttpResponseServerError(error_message)

	# End the current usage event for the tool
	current_usage_event.end = timezone.now() + downtime

	# Collect post-usage questions
	dynamic_form = DynamicForm(tool.post_usage_questions)
	current_usage_event.run_data = dynamic_form.extract(request)
	dynamic_form.charge_for_consumable(current_usage_event.user, current_usage_event.operator, current_usage_event.project, current_usage_event.run_data)

	current_usage_event.save()
	if request.user.charging_staff_time():
		existing_staff_charge = request.user.get_staff_charge()
		if existing_staff_charge.customer == current_usage_event.user and existing_staff_charge.project == current_usage_event.project:
			response = render(request, 'staff_charges/reminder.html', {'tool': tool})

	return response
Exemplo n.º 5
0
def disable_tool(request, tool_id):
    if not settings.ALLOW_CONDITIONAL_URLS:
        return HttpResponseBadRequest(
            "Tool control is only available on campus.")

    tool = get_object_or_404(Tool, id=tool_id)
    if tool.get_current_usage_event() is None:
        return HttpResponse()
    downtime = timedelta(minutes=quiet_int(request.POST.get("downtime")))
    response = check_policy_to_disable_tool(tool, request.user, downtime)
    if response.status_code != HTTPStatus.OK:
        return response

    # Shorten the user's tool reservation since we are now done using the tool
    shorten_reservation(user=request.user,
                        item=tool,
                        new_end=timezone.now() + downtime)

    # All policy checks passed so disable the tool for the user.
    if tool.interlock and not tool.interlock.lock():
        error_message = f"The interlock command for the {tool} failed. The error message returned: {tool.interlock.most_recent_reply}"
        tool_control_logger.error(error_message)
        return HttpResponseServerError(error_message)

    # End the current usage event for the tool
    current_usage_event = tool.get_current_usage_event()
    current_usage_event.end = timezone.now() + downtime

    # Collect post-usage questions
    dynamic_form = DynamicForm(tool.post_usage_questions, tool.id)
    current_usage_event.run_data = dynamic_form.extract(request)
    dynamic_form.charge_for_consumables(
        current_usage_event.user,
        current_usage_event.operator,
        current_usage_event.project,
        current_usage_event.run_data,
    )
    dynamic_form.update_counters(current_usage_event.run_data)

    current_usage_event.save()
    user: User = request.user
    if user.charging_staff_time():
        existing_staff_charge = user.get_staff_charge()
        if (existing_staff_charge.customer == current_usage_event.user and
                existing_staff_charge.project == current_usage_event.project):
            response = render(request, "staff_charges/reminder.html",
                              {"tool": tool})

    return response
Exemplo n.º 6
0
def disable_tool(request):
    tool = Tool.objects.get(id=request.POST['tool_id'])
    customer = User.objects.get(id=request.POST['customer_id'])
    downtime = timedelta(minutes=quiet_int(request.POST.get('downtime')))
    response = check_policy_to_disable_tool(tool, customer, downtime)
    if response.status_code != HTTPStatus.OK:
        dictionary = {
            'message': response.content,
            'delay': 10,
        }
        return render(request, 'kiosk/acknowledgement.html', dictionary)

    # Shorten the user's tool reservation since we are now done using the tool
    shorten_reservation(user=customer,
                        item=tool,
                        new_end=timezone.now() + downtime)

    # All policy checks passed so disable the tool for the user.
    if tool.interlock and not tool.interlock.lock():
        raise Exception(
            "The interlock command for this tool failed. The error message returned: "
            + str(tool.interlock.most_recent_reply))
    # End the current usage event for the tool and save it.
    current_usage_event = tool.get_current_usage_event()
    current_usage_event.end = timezone.now() + downtime

    # Collect post-usage questions
    dynamic_form = DynamicForm(tool.post_usage_questions)
    current_usage_event.run_data = dynamic_form.extract(request)
    dynamic_form.charge_for_consumables(current_usage_event.user,
                                        current_usage_event.operator,
                                        current_usage_event.project,
                                        current_usage_event.run_data)

    current_usage_event.save()
    dictionary = {
        'message': 'You are no longer using the {}'.format(tool),
        'badge_number': customer.badge_number,
    }
    return render(request, 'kiosk/acknowledgement.html', dictionary)
Exemplo n.º 7
0
def disable_tool(request, tool_id):

    if not settings.ALLOW_CONDITIONAL_URLS:
        return HttpResponseBadRequest(
            'Tool control is only available on campus. We\'re working to change that! Thanks for your patience.'
        )

    tool = get_object_or_404(Tool, id=tool_id)
    if tool.get_current_usage_event() is None:
        return HttpResponse()
    downtime = timedelta(minutes=quiet_int(request.POST.get('downtime')))
    response = check_policy_to_disable_tool(tool, request.user, downtime)
    if response.status_code != HTTPStatus.OK:
        return response
    confirm = request.POST.get('confirm') == 'true'
    try:
        current_reservation = Reservation.objects.get(start__lt=timezone.now(),
                                                      end__gt=timezone.now(),
                                                      cancelled=False,
                                                      missed=False,
                                                      shortened=False,
                                                      user=request.user,
                                                      tool=tool)
        # Staff are exempt from mandatory reservation shortening when tool usage is complete.
        if request.user.is_staff is False:
            if confirm:
                # Shorten the user's reservation to the current time because they're done using the tool.
                new_reservation = deepcopy(current_reservation)
                new_reservation.id = None
                new_reservation.pk = None
                new_reservation.end = timezone.now() + downtime
                new_reservation.save()
                current_reservation.shortened = True
                current_reservation.descendant = new_reservation
                current_reservation.save()
            else:
                return render(
                    request, 'tool_control/confirm_tool_disable.html', {
                        'reservation': current_reservation,
                        'tool': tool,
                        'downtime': downtime.total_seconds() / 60
                    })
    except Reservation.DoesNotExist:
        pass

    # All policy checks passed so disable the tool for the user.
    if tool.interlock and not tool.interlock.lock():
        raise Exception(
            "The interlock command for this tool failed. The error message returned: "
            + str(tool.interlock.most_recent_reply))
    # End the current usage event for the tool and save it.
    current_usage_event = tool.get_current_usage_event()
    current_usage_event.end = timezone.now() + downtime
    current_usage_event.save()
    if request.user.charging_staff_time():
        existing_staff_charge = request.user.get_staff_charge()
        if existing_staff_charge.customer == current_usage_event.user and existing_staff_charge.project == current_usage_event.project:
            response = render(request, 'staff_charges/reminder.html',
                              {'tool': tool})

    return response