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 logout_of_area(request, door_id):
    try:
        badge_number = int(request.POST.get('badge_number', ''))
        user = User.objects.get(badge_number=badge_number)
    except (User.DoesNotExist, ValueError):
        return render(request, 'area_access/badge_not_found.html')
    record = user.area_access_record()
    # Allow the user to log out of any area, even if this is a logout tablet for a different area.
    if record:
        record.end = timezone.now()
        record.save()
        # Shorten the user's area reservation since the user is now leaving
        shorten_reservation(user, record.area)
        busy_tools = UsageEvent.objects.filter(end=None, user=user)
        if busy_tools:
            return render(
                request, 'area_access/logout_warning.html', {
                    'area': record.area,
                    'name': user.first_name,
                    'tools_in_use': busy_tools
                })
        else:
            return render(request, 'area_access/logout_success.html', {
                'area': record.area,
                'name': user.first_name
            })
    else:
        return render(request, 'area_access/not_logged_in.html')
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 self_log_out(request, user_id):
    user = get_object_or_404(User, id=user_id)
    if able_to_self_log_out_of_area(user):
        record = user.area_access_record()
        if record is None:
            return HttpResponseBadRequest('You are not logged into any areas.')
        record.end = timezone.now()
        record.save()
        # Shorten the user's area reservation since the user is now leaving
        shorten_reservation(request.user, record.area)
    return redirect(reverse('landing'))
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 log_out_user(user: User):
	record = user.area_access_record()
	# Allow the user to log out of any area, even if this is a logout tablet for a different area.
	if record:
		record.end = timezone.now()
		record.save()
		# Shorten the user's area reservation since the user is now leaving
		shorten_reservation(user, record.area)
		# Stop charging area access if staff is leaving the area
		staff_charge = user.get_staff_charge()
		if staff_charge:
			try:
				staff_area_access = AreaAccessRecord.objects.get(staff_charge=staff_charge, end=None)
				staff_area_access.end = timezone.now()
				staff_area_access.save()
			except AreaAccessRecord.DoesNotExist:
				pass
Exemplo n.º 7
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)