Exemple #1
0
def borrowRequest(request, tool_id):
    """
    Private tool only. Sends a notification to both tool owner and requester.
    Tool owner will receive a notification that has an Accept/Deny message.
    Requester receives a reminder message.
    """
    if request.user.is_authenticated():
        form = BorrowForm(request.POST)
        if form.is_valid():
            return_date = form.cleaned_data['return_date']
            tool = Tool.objects.get(pk=tool_id)
            tool.availability = 2
            tool.save()
            current_time = timezone.now()
            n1 = Notification(6, request.user.id, request.user.id, \
                tool_id, tool.shed_id, 1, current_time.strftime("%Y-%m-%d;%H-%M-%S"), \
                return_date, request.user.fname + ' ' + request.user.lname, \
                request.user.fname + ' ' + request.user.lname, tool.name, tool.shed_string)
            log_notification(n1)
            n2 = Notification(1, tool.owner_id, request.user.id, tool_id, tool.shed_id, \
                1, current_time.strftime("%Y-%m-%d;%H-%M-%S"), return_date, \
                tool.owner_string, request.user.fname + ' ' + request.user.lname, tool.name, tool.shed_string, \
                form.cleaned_data['msg'])
            log_notification(n2)
            messages.success(request, 'The tool has been requested.')
            return redirect('/../tools/viewTools/')
        else:
            messages.error(request , 'There is invalid information.')
            return redirect('/../tools/viewTools/')
        
    else:
        return HttpResponseRedirect('/login')
Exemple #2
0
def borrowTool(request, tool_id):
    """
    Public Tool only. Borrows a tool, changes the information of the
    tool. A notification is sent to the requester and the tool's shed's
    owner.
    """
    if request.user.is_authenticated():
        #Public Tool borrowing
        form = BorrowForm(request.POST)
        tool = Tool.objects.get(pk=tool_id)
        
        borrower = Owner.objects.get(pk=request.user.id)
        tool_owner = Owner.objects.get(pk=tool.owner_id)

        new_borrow_count = borrower.borrow_counter + 1
        new_lend_count = tool_owner.lend_counter + 1

        shed = Shed.objects.get(pk=tool.shed_id)

        new_usage_count = tool.usage_count + 1

        if tool.availability == 1:
            if form.is_valid():
                tool = Tool.objects.get(pk=tool_id)
                #Can get rid of boolean column, as 01-01-2001 will indicate availability
                setattr(tool, 'availability', 0)
                setattr(tool, 'return_date', form.cleaned_data['return_date'])
                setattr(tool, 'borrower_id', request.user.id)
                setattr(tool, 'borrower_string', Owner.objects.get(pk=request.user.id).fname + ' ' + Owner.objects.get(pk=request.user.id).lname)
                setattr(tool, 'usage_count', new_usage_count)
                current_time = timezone.now()

                
                setattr(tool_owner, 'lend_counter', new_lend_count)
                n1 = Notification(5, request.user.id, request.user.id, tool.id, tool.shed_id, 1, \
                    current_time.strftime("%Y-%m-%d;%H-%M-%S;%Z"), tool.return_date, \
                    request.user.fname + ' ' + request.user.lname, request.user.fname + ' ' + request.user.lname, \
                    tool.name, tool.shed_string)

                setattr(borrower, 'borrow_counter', new_borrow_count)


                log_notification(n1)
                n2 = Notification(0, shed.owner_id, request.user.id, tool.id, tool.shed_id, 1, \
                    current_time.strftime("%Y-%m-%d;%H-%M-%S;%Z"), tool.return_date, \
                    shed.owner_string, request.user.fname + ' ' + request.user.lname, tool.name,
                    tool.shed_string)
                log_notification(n2)

                tool_owner.save()
                borrower.save()
                tool.save()
                messages.success(request, 'The tool has been borrowed.')
                return redirect('/../tools/viewTools/')
            else:
                messages.error(request, 'The information you have entered is not valid.')
                return redirect('/../tools/viewTools/')
        else:
            messages.error(request, 'The tool has already been borrowed.')
            return redirect('/../tools/viewTools/')
    else:
        return HttpResponseRedirect('/login')
def borrow_button(context, tool):
    form = BorrowForm(initial=dict(tool=tool))
    return render_crispy_form(form, context=context)