示例#1
0
def request_share(request, tid):
    # View To borrow a tool , check if the tool is in the shed or home
    #Check if user is logged in..................................
    try:
        user_zip = models.User.objects.get(
            user_name=request.session['user_name']).zipcode
    except KeyError:
        return HttpResponseRedirect('/')

    #Check if shed exists
    try:
        shed_zip = models.Shed.objects.get(shed_zip=user_zip)
    except ObjectDoesNotExist:
        return HttpResponseRedirect('/shed')

    borrow_user = models.User.objects.get(
        user_name=request.session['user_name'])
    borrow_tool = models.Tools.objects.get(id=tid)

    if borrow_tool.tool_atHome == False:
        #Redirect to Share from tool page.
        return HttpResponseRedirect('/shedshare/%s' % tid)

    elif borrow_tool.tool_atHome == True:
        form = forms.Send_Message(request.POST or None)
        context = {
            'form': form,
        }

        if form.is_valid():
            message_obj = models.Message()
            message_obj.days = form.cleaned_data['days']
            if 0 < message_obj.days < 15:
                message_obj.user_borrower = borrow_user.user_name
                message_obj.user_owner = borrow_tool.tool_owner
                message_obj.messages = form.cleaned_data['message']
                message_obj.tool = borrow_tool.tool_name
                message_obj.tid = tid
                message_obj.save()
                return HttpResponseRedirect('/up')
                req_obj = models.Notifications()
                req_obj.user_from = borrow_user.user_name
                req_obj.user_to = borrow_tool.tool_owner
                req_obj.notification_text = borrow_user.last_name + " has requested to borrow your tool " + borrow_tool.tool_name
                req_obj.tid = borrow_tool.id
                req_obj.save()

                return HttpResponseRedirect('/up')
            else:
                context['error'] = "Please enter a number less than 15"

        return render(request, 'startshare.html', context)

        return HttpResponseRedirect('/request')

    return render(request, 'startshare.html', share_context)
示例#2
0
def makeReservation(request, tid):

    try:
        user_zip = models.User.objects.get(
            user_name=request.session['user_name']).zipcode
    except KeyError:
        return HttpResponseRedirect('/')

    #check if a shed exists.
    try:
        shed = models.Shed.objects.get(shed_zip=user_zip)
    except ObjectDoesNotExist:
        return HttpResponseRedirect('/shed')

    tool = models.Tools.objects.get(id=tid)
    deadline = tool.tool_deadline
    Date = deadline + timedelta(days=1)
    if tool.tool_atHome == 1:
        reservation_form = forms.ReserveTool(request.POST or None)
        context = {
            'home_form': reservation_form,
            'home': True,
            'nextDate': Date
        }

        if reservation_form.is_valid():
            reserveMessage_obj = models.ReserveMessages()
            reserveMessage_obj.reserver = request.session['user_name']
            reserveMessage_obj.owner = tool.tool_owner
            reserveMessage_obj.message = reservation_form.cleaned_data[
                'messages']
            reserveMessage_obj.tid = tool.id
            reserveMessage_obj.days = reservation_form.cleaned_data['days']
            if (0 < reserveMessage_obj.days < 15):
                reserveMessage_obj.save()
                user = models.User.objects.get(
                    user_name=request.session['user_name'])
                req_obj = models.Notifications()
                req_obj.user_from = request.session['user_name']
                req_obj.user_to = tool.tool_owner
                req_obj.notification_text = user.last_name + " has requested to reserve the tool " + tool.tool_name + "."
                req_obj.tid = tool.id
                req_obj.save()
                return HttpResponseRedirect('/toolsinzip')
            else:
                context['error'] = "Please enter a number less than 15"

    else:
        url = '/reserveShed/' + str(tid)
        return HttpResponseRedirect(url)

    return render(request, 'reserve.html', context)
示例#3
0
def ShedShare(request, tid):
    #Function to get the number of days and accept a borrow request for a tool from a shed

    #Check if user is logged in..................................
    try:
        user_zip = models.User.objects.get(
            user_name=request.session['user_name']).zipcode
    except KeyError:
        return HttpResponseRedirect('/')

    form = forms.Days(request.POST or None)
    context = {'form': form, "hide": False}
    if form.is_valid():
        days = form.cleaned_data['days']
        if (0 < days < 15):
            shareTool_obj = models.ShareTool()
            shareTool_obj.tid = tid
            shareTool_obj.days = days
            tool = models.Tools.objects.get(id=tid)
            if tool.tool_available == 1:
                shareTool_obj.owner = tool.tool_owner
                shareTool_obj.borrower = models.User.objects.get(
                    user_name=request.session['user_name']).user_name
                shareTool_obj.deadline = datetime.today() + timedelta(
                    days=days)
                shareTool_obj.save()
                tool.tool_borrower = shareTool_obj.borrower
                tool.tool_available = 0
                tool.tool_deadline = str(shareTool_obj.deadline)[:10]
                tool.save()
                req_obj = models.Notifications()
                req_obj.user_from = null
                req_obj.user_to = models.User.objects.get(
                    user_name=request.session['user_name']).user_name
                req_obj.notification_text = "Your request to borrow the tool " + borrow_tool.tool_name + " has been accepted."
                req_obj.tid = tid
                req_obj.save()
                context = {}
                context = {
                    "hide": True,
                    "message": "The tool is yours now",
                    "owner": tool.tool_owner
                }
        else:
            context['error'] = "Please input a number less than 15"
    return render(request, 'shedshare.html', context)
示例#4
0
def addAdmin(request):
    try:
        user_zip = models.User.objects.get(
            user_name=request.session['user_name']).zipcode
    except KeyError:
        return HttpResponseRedirect('/')
    #check if a shed exists.
    try:
        shed_zip = models.Shed.objects.get(shed_zip=user_zip)
    except ObjectDoesNotExist:
        return HttpResponseRedirect('/shed')

    users = models.User.objects.filter(zipcode=user_zip)

    users_list = []
    for user in users:
        try:
            models.BannedUsers.objects.get(user_name=user.user_name)
        except:
            inner_list = [user.user_name, user.address, user.isAdmin]
            users_list.append(inner_list)

    context = {'user_list': users_list}

    if (request.GET.get('addAdmin')):
        user_id = request.GET.get('user_id')
        print(user_id)
        try:
            user = models.User.objects.get(user_name=user_id)
            user.isAdmin = 1
            user.save()
            page = request.get_full_path()
            page = page.rpartition('?')[0]
            return HttpResponseRedirect(page)
        except:
            pass

    if (request.GET.get('deleteAdmin')):
        user_id = request.GET.get('user_id')
        number = models.User.objects.all().filter(zipcode=user_zip).exclude(
            isAdmin=0).count()
        context['flag'] = 0

        if 1 < number:
            try:
                user = models.User.objects.get(user_name=user_id)
            except:
                pass
            user.isAdmin = 0
            user.save()
            req_obj = models.Notifications()
            req_obj.user_from = None
            req_obj.user_to = user.user_name
            req_obj.notification_text = "You are no longer an admin of the shed"
            req_obj.tid = None
            req_obj.save()
            page = request.get_full_path()
            page = page.rpartition('?')[0]
            if user.user_name == request.session['user_name']:
                request.session['isAdmin'] = False
                return HttpResponseRedirect('/up')
            return HttpResponseRedirect(page)
        else:
            context['flag'] = 1
            context[
                'mssg'] = "You are the only Admin in the sharezone. Add another Admin to remove yourself."

        return render(request, 'add_admin.html', context)

    return render(request, 'add_admin.html', context)
示例#5
0
def borrowed_tools(request):

    #check if user is logged in.
    try:
        user_zip = models.User.objects.get(
            user_name=request.session['user_name']).zipcode
    except KeyError:
        return HttpResponseRedirect('/')

    #check if shed Exists
    try:
        shed_zip = models.Shed.objects.get(shed_zip=user_zip)
    except ObjectDoesNotExist:
        return HttpResponseRedirect('/shed')

    user = models.User.objects.get(user_name=request.session['user_name'])
    tools = models.Tools.objects.all().filter(tool_borrower=user)

    the_tool_list = []
    for tool in tools:
        address = models.Shed.objects.get(shed_zip=user_zip).shed_address
        inner_list = [
            tool.id, tool.tool_name, tool.tool_image, tool.tool_description,
            tool.tool_special, tool.tool_category, tool.tool_owner,
            tool.tool_deadline,
            models.User.objects.get(
                user_name=tool.tool_owner).pickup_arrangement,
            models.User.objects.get(user_name=tool.tool_owner).email, address
        ]
        the_tool_list.append(inner_list)

    context = {'tool_list': the_tool_list}

    if (request.GET.get('Return')):
        request_id = request.GET.get('values')
        tool = models.Tools.objects.get(id=request_id)
        tool.tool_borrower = ''
        tool.tool_available = 1
        tool.tool_deadline = datetime.today()
        tool.save()
        #if tool is reserved, make the reservation into a borrow
        if tool.tool_isReserved == 1:
            print("if called")
            tool.tool_isReserved = 0
            print("reserved")
            tool.tool_available = 0
            print("available")
            try:
                tool.tool_deadline = datetime.today() + timedelta(
                    days=int(tool.tool_nextDays))
            except:
                tool.tool_deadline = datetime.today() + timedelta(
                    days=tool.tool_nextDays)
            tool.tool_borrower = tool.tool_nextBorrower

            #Create the notification.
            req_obj = models.Notifications()
            req_obj.user_to = tool.tool_nextBorrower = ""
            req_obj.notification_text = " The tool " + tool.tool_name + " is now your."
            req_obj.tid = tool.id
            req_obj.save()
            tool.tool_nextBorrower = ""

            # To actually create the shareTool for Statistics
            shareTool_obj = models.ShareTool()
            shareTool_obj.tid = request_id
            shareTool_obj.days = tool.tool_nextDays
            shareTool_obj.borrower = tool.tool_borrower
            shareTool_obj.owner = tool.tool_owner
            try:
                shareTool_obj.deadline = datetime.today() + timedelta(
                    days=int(tool.tool_nextDays))
            except:
                shareTool_obj.deadline = datetime.today() + timedelta(
                    days=tool.tool_nextDays)

            tool.tool_deadline = str(shareTool_obj.deadline)[:10]
            tool.tool_nextDays = 0
            shareTool_obj.save()
            tool.save()
        return HttpResponseRedirect('/borrowed_tools')

    return render(request, 'return_tool.html', context)
示例#6
0
def User_Messages(request):
    #Check if user is logged in..................................
    try:
        user_zip = models.User.objects.get(
            user_name=request.session['user_name']).zipcode
    except KeyError:
        return HttpResponseRedirect('/')

    #Check if shed exists
    try:
        shed_zip = models.Shed.objects.get(shed_zip=user_zip)
    except ObjectDoesNotExist:
        return HttpResponseRedirect('/shed')

    current_user = models.User.objects.get(
        user_name=request.session['user_name'])

    Message = models.Message.objects.all().filter(
        user_owner=current_user).exclude(replied=True)
    message_list = []
    for message in Message:
        if models.Tools.objects.get(id=message.tid).tool_available == 1:
            inner_list = [
                str(message.tid), message.user_borrower, message.messages,
                message.tool, message.id, message.days
            ]
            message_list.append(inner_list)

    ReserveMessage = models.ReserveMessages.objects.all().filter(
        owner=current_user).exclude(replied=True)
    rmessage_list = []
    for message in ReserveMessage:
        if models.Tools.objects.get(id=message.tid).tool_isReserved == 0:
            inner_list = [
                str(message.tid), message.reserver, message.message,
                models.Tools.objects.get(id=message.tid).tool_name,
                message.days, message.id
            ]
            rmessage_list.append(inner_list)

    context = {'list': message_list, 'rlist': rmessage_list}

    notifs = models.Notifications.objects.all().filter(
        user_to=request.session['user_name'])
    for notif in notifs:
        notif.active = False
        notif.save()

    if (request.GET.get('Accept')):
        #if he accepts the request.
        value = request.GET.get('value')
        Amessage = models.Message.objects.get(id=value)
        #First handle the message
        Amessage.replied = True
        #Then Mark the tool as unavailable.
        tool = models.Tools.objects.get(id=Amessage.tid)
        tool.tool_borrower = Amessage.user_borrower

        Amessage.save()

        #Creating the share in the sharetool model
        if tool.tool_available == 1:
            tool.tool_available = 0
            shareTool_obj = models.ShareTool()
            shareTool_obj.tid = tool.id
            shareTool_obj.days = Amessage.days
            shareTool_obj.borrower = Amessage.user_borrower
            shareTool_obj.owner = models.User.objects.get(
                user_name=request.session['user_name']).user_name
            shareTool_obj.deadline = datetime.today() + timedelta(
                days=Amessage.days)
            tool.tool_deadline = str(shareTool_obj.deadline)[:10]
            shareTool_obj.save()
            tool.save()

            req_obj = models.Notifications()
            req_obj.user_from = models.User.objects.get(
                user_name=request.session['user_name']).user_name
            req_obj.user_to = Amessage.user_borrower
            req_obj.notification_text = models.User.objects.get(
                user_name=request.session['user_name']
            ).last_name + " has approved your request to borrow the tool " + tool.tool_name + "."
            req_obj.tid = tool.id
            req_obj.save()
            return HttpResponseRedirect('/messages')

    if (request.GET.get('Reject')):
        value = request.GET.get('value')
        Rmessage = models.Message.objects.get(id=value)
        Rmessage.replied = True
        Rmessage.save()
        tool = models.Tools.objects.get(id=Rmessage.tid)

        req_obj = models.Notifications()
        req_obj.user_from = models.User.objects.get(
            user_name=request.session['user_name']).user_name
        req_obj.user_to = Rmessage.user_borrower
        req_obj.notification_text = models.User.objects.get(
            user_name=request.session['user_name']
        ).last_name + " has rejected your request to borrow the tool " + tool.tool_name + "."
        req_obj.tid = tool.id
        req_obj.save()

        return HttpResponseRedirect('/messages')

        #Handle Accept or reject of Reservation messages

    if (request.GET.get('rAccept')):
        value = request.GET.get('value')
        Amessage = models.ReserveMessages.objects.get(id=value)
        Amessage.replied = True
        tool = models.Tools.objects.get(id=Amessage.tid)
        Amessage.save()
        if tool.tool_isReserved == 0:
            tool.tool_isReserved = 1
            tool.tool_nextBorrower = Amessage.reserver
            tool.tool_nextDays = Amessage.days
            tool.save()
            page = request.get_full_path()
            page = page.rpartition('?')[0]

            req_obj = models.Notifications()
            req_obj.user_from = models.User.objects.get(
                user_name=request.session['user_name']).user_name
            req_obj.user_to = Amessage.reserver
            req_obj.notification_text = models.User.objects.get(
                user_name=request.session['user_name']
            ).last_name + " has approved your request to reserve the tool " + tool.tool_name + "."
            req_obj.tid = tool.id
            req_obj.save()

            return HttpResponseRedirect(page)

    if (request.GET.get('rReject')):
        value = request.GET.get('value')
        Rmessage = models.ReserveMessages.objects.get(id=value)
        Rmessage.replied = True
        Rmessage.save()
        tool = models.Tools.objects.get(id=Rmessage.tid)
        page = request.get_full_path()
        page = page.rpartition('?')[0]

        req_obj = models.Notifications()
        req_obj.user_from = models.User.objects.get(
            user_name=request.session['user_name']).user_name
        req_obj.user_to = Rmessage.reserver
        req_obj.notification_text = models.User.objects.get(
            user_name=request.session['user_name']
        ).last_name + " has rejected your request to reserve the tool " + tool.tool_name + "."
        req_obj.tid = tool.id
        req_obj.save()

        return HttpResponseRedirect(page)

    return render(request, 'messages.html', context)