Ejemplo n.º 1
0
def begin_staff_area_charge(request):
    charge = request.user.get_staff_charge()
    if not charge:
        return HttpResponseBadRequest(
            'You do not have a staff charge in progress, so you cannot begin an area access charge.'
        )
    if AreaAccessRecord.objects.filter(staff_charge=charge,
                                       end=None).count() > 0:
        return HttpResponseBadRequest(
            'You cannot create an area access charge when one is already in progress.'
        )
    try:
        area = Area.objects.get(id=request.POST['area'])
        check_billing_to_project(charge.project, charge.customer, area)
    except ProjectChargeException as e:
        return HttpResponseBadRequest(e.msg)
    except:
        return HttpResponseBadRequest('Invalid area')
    area_access = AreaAccessRecord()
    area_access.area = area
    area_access.staff_charge = charge
    area_access.customer = charge.customer
    area_access.project = charge.project
    area_access.save()
    return redirect(reverse('staff_charges'))
Ejemplo n.º 2
0
def enable_tool(request, tool_id, user_id, project_id, staff_charge):
    """ Enable a tool for a user. The user must be qualified to do so based on the lab usage policy. """

    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)
    operator = request.user
    user = get_object_or_404(User, id=user_id)
    project = get_object_or_404(Project, id=project_id)
    staff_charge = staff_charge == "true"
    bypass_interlock = request.POST.get("bypass", 'False') == 'True'
    response = check_policy_to_enable_tool(tool, operator, user, project,
                                           staff_charge)
    if response.status_code != HTTPStatus.OK:
        return response

    # All policy checks passed so enable the tool for the user.
    if tool.interlock and not tool.interlock.unlock():
        if bypass_interlock and interlock_bypass_allowed(user):
            pass
        else:
            return interlock_error("Enable", user)

    # Start staff charge before tool usage
    if staff_charge:
        new_staff_charge = StaffCharge()
        new_staff_charge.staff_member = request.user
        new_staff_charge.customer = user
        new_staff_charge.project = project
        new_staff_charge.save()
        # If the tool requires area access, start charging area access time
        if tool.requires_area_access:
            area_access = AreaAccessRecord()
            area_access.area = tool.requires_area_access
            area_access.staff_charge = new_staff_charge
            area_access.customer = new_staff_charge.customer
            area_access.project = new_staff_charge.project
            area_access.save()

    # Create a new usage event to track how long the user uses the tool.
    new_usage_event = UsageEvent()
    new_usage_event.operator = operator
    new_usage_event.user = user
    new_usage_event.project = project
    new_usage_event.tool = tool
    new_usage_event.save()

    return response