Example #1
0
def view_shed_page(request, id, contextArg):#contextArg is a dict to be added to the content dict
    if request.user.is_anonymous():
        return HttpResponseRedirect("/accounts/login")
    else:
        if id is not None:
            try:
                shedObj = shedUtil.getShedFromID(id)
            except ObjectDoesNotExist:
                context = {}
                context['object'] = 'shed'
                context.update(content.genBaseLoggedIn(request))
                return render_to_response("dne.html", context)
        else:
            context = {}
            context['object'] = 'shed'
            context.update(content.genBaseLoggedIn(request))
            return render_to_response("dne.html", context)
        owner = shedUtil.getOwnerOfShed(shedObj)
        name = shedUtil.getNameOfShed(shedObj)
        admins = shedUtil.getAllAdminsOfShed(shedObj)
        isAdmin = False
        for admin in admins:
            if admin == profileUtil.getProfileFromUser(request.user):
                isAdmin = True
        members = shedUtil.getAllMembersOfShed(shedObj)
        tools = toolUtil.getAllToolsInShed(shedObj)
        userProfile = profileUtil.getProfileFromUser(request.user)
        meetsMinRep = userProfile.reputation >= shedObj.minimumReputation
        shedMembership = shedUtil.checkForMembership(userProfile, id)
        actions = actionUtil.getProfileAction(profileUtil.getProfileFromUser(request.user))
        actionRequest = None
        pendingRequest = False
        for action in actions:
            if action.shed == shedObj:
                actionRequest = action
        if actionRequest:
            if actionRequest.currrentState == "userShedRequest" or actionRequest.currrentState == "acceptDeny":
                pendingRequest = True
        context = {}
        context.update(csrf(request))
        context['shed'] = shedObj
        context['owner'] = owner
        context['currentUser'] = profileUtil.getProfileFromUser(request.user)
        context['name'] = name
        context['admins'] = admins
        context['members'] = members
        context['tools'] = tools
        context['meetsMin'] = meetsMinRep
        context['alreadyMember'] = shedMembership
        context['isAdmin'] = isAdmin
        context['pendingRequest'] = pendingRequest
        context.update(content.genBaseLoggedIn(request))
        if contextArg:
            context.update(contextArg)
        return render_to_response('shed_page.html', context)
Example #2
0
def add_shed_admin(request, id, username):
    if request.user.is_anonymous():
        return HttpResponseRedirect('/accounts/login')
    else:
        userProfile = profileUtil.getProfileFromUser(request.user)
        shedObj = shedUtil.getShedFromID(id)
        newAdmin = profileUtil.getProfileFromUser(User.objects.get(username=username))
        admins = shedUtil.getAllAdminsOfShed(shedObj)
        isAdmin = False
        for admin in admins:
            if admin == userProfile:
                isAdmin = True
        if isAdmin:
            shedObj.admins.add(newAdmin)
            notifUtil.createInfoNotif(shedObj, newAdmin, "You have been made an admin of the shed " + shedObj.name + "! ")
            return HttpResponseRedirect("/sheds/" + str(shedObj.id) + "/add_admin/added/success")
        else:
            return HttpResponseRedirect('/')
Example #3
0
def remove_shed_member(request, id, username):
    if request.user.is_anonymous():
        return HttpResponseRedirect('/accounts/login')
    else:
        userProfile = profileUtil.getProfileFromUser(request.user)
        shedObj = shedUtil.getShedFromID(id)
        banUser = profileUtil.getProfileFromUser(User.objects.get(username=username))
        admins = shedUtil.getAllAdminsOfShed(shedObj)
        userIsAdmin = False
        banUserIsAdmin = False
        for admin in admins:
            if admin == userProfile:
                userIsAdmin = True
            elif admin == banUser:
                banUserIsAdmin = True
        if userIsAdmin:
            if banUserIsAdmin:
                if shedObj.owner == userProfile:
                    shedUtil.removeAdminFromShed(shedObj, banUser)
                    notifUtil.createBadInfoNotif(shedObj, banUser, "You have been removed as an admin from the shed " + shedObj.name + ". ")
                    shedUtil.removeMemberFromShed(shedObj, banUser)
                    notifUtil.createBadInfoNotif(shedObj, banUser, "You have been kicked from the shed " + shedObj.name + ". ")
                    shedTools = toolUtil.getAllToolsInShed(shedObj)
                    for tool in shedTools:
                        if tool.owner == banUser:
                            shedUtil.removeToolFromShed(shedObj, tool)
                            shedUtil.addToolToShed(banUser.personalShed, tool)
                    return HttpResponseRedirect("/sheds/" + str(shedObj.id) + "/remove_member/kicked/success")
                else:
                    return HttpResponseRedirect('/')
            else:
                shedUtil.removeMemberFromShed(shedObj, banUser)
                shedObj.bannedUsers.add(banUser)
                notifUtil.createBadInfoNotif(shedObj, banUser, "You have been kicked from the shed " + shedObj.name + ". ")
                shedTools = toolUtil.getAllToolsInShed(shedObj)
                for tool in shedTools:
                    if tool.owner == banUser:
                        shedUtil.removeToolFromShed(shedObj, tool)
                        shedUtil.addToolToShed(banUser.personalShed, tool)
                return HttpResponseRedirect("/sheds/" + str(shedObj.id) + "/remove_member/kicked/success")
        else:
            return HttpResponseRedirect('/')
Example #4
0
def leave_shed(request, id):
    if request.user.is_anonymous():
        return HttpResponseRedirect('/accounts/login')
    else:
        userProfile = profileUtil.getProfileFromUser(request.user)
        shedObj = shedUtil.getShedFromID(id)
        if shedObj.owner == userProfile:
            return HttpResponseRedirect('/')
        else:
            admins = shedUtil.getAllAdminsOfShed(shedObj)
            for admin in admins:
                if admin == userProfile:
                    shedUtil.removeAdminFromShed(shedObj, userProfile)
            shedUtil.removeMemberFromShed(shedObj, userProfile)
            shedTools = toolUtil.getAllToolsInShed(shedObj)
            for tool in shedTools:
                if tool.owner == userProfile:
                    shedUtil.removeToolFromShed(shedObj, tool)
                    shedUtil.addToolToShed(userProfile.personalShed, tool)
            return HttpResponseRedirect('/sheds/' + str(shedObj.id) + '/leave/success')
Example #5
0
def confirm_remove_shed_member(request, id, username):
    if request.user.is_anonymous():
        return HttpResponseRedirect('/accounts/login')
    else:
        userProfile = profileUtil.getProfileFromUser(request.user)
        shedObj = shedUtil.getShedFromID(id)
        banUser = profileUtil.getProfileFromUser(User.objects.get(username=username))
        admins = shedUtil.getAllAdminsOfShed(shedObj)
        isAdmin = False
        for admin in admins:
            if admin == userProfile:
                isAdmin = True
        if isAdmin:
            context = {}
            context['currentUser'] = userProfile
            context['shed'] = shedObj
            context['banned'] = banUser
            context.update(content.genBaseLoggedIn(request))
            return render_to_response("remove_member_confirm.html", context)
        else:
            return HttpResponseRedirect('/')
Example #6
0
 def test_RemoveAdminFromShed(self):
     shedUtils.removeAdminFromShed (self.getShed, profUtils.getAllProfiles()[2])
     self.getShed = Shed.objects.get (name = "Lightsaber Tools")
     self.assertNotIn (profUtils.getAllProfiles()[2], shedUtils.getAllAdminsOfShed(self.getShed)) #taking him out and then making sure he's not there
Example #7
0
 def test_AddAdminToShed(self):
     """
     """
     shedUtils.addAdminToShed (self.getShed, profUtils.getAllProfiles()[2])
     self.getShed = Shed.objects.get (name = "Lightsaber Tools")
     self.assertIn (profUtils.getAllProfiles()[2], shedUtils.getAllAdminsOfShed (self.getShed))  #adding an admin and then making sure he's there
Example #8
0
def processActions():
    #Re-process all action objects in the database
    for actionInstance in actionUtil.getAllActions():
        #states allow system to process and respond to all actions asynchronously
        #Tool borrow state machine
        if actionUtil.isToolRequest(actionInstance):

            if actionInstance.currrentState == "":  #entry point
                #proceed to next state where the owner is asked if this user can borrow his tool
                actionInstance.currrentState = "userBorrowRequest"
                actionInstance.save()
                processActions()

            if actionInstance.currrentState == "userBorrowRequest":
                #proceed to next state
                actionInstance.currrentState = "acceptDecline"
                actionInstance.save()
                #send a request notification to the user who's tool is being requested
                #the response options will be "Accept" or "Deny"
                question = actionInstance.requester.user.username + " has requested to borrow your " + \
                                actionInstance.tool.name + " from " + actionInstance.tool.myShed.name + ". "
                userOptions = "Accept,Deny" #adding options       
                utilities.notificationUtilities.createResponseNotif(actionInstance, actionInstance.tool.owner, \
                                                            question, options = userOptions)
                processActions()

            elif actionInstance.currrentState == "acceptDecline":
                #if the owner of the tool has responded to the tool request notification:
                if utilities.notificationUtilities.notifHasResponse(actionUtil.getRequestNotifOfAction(actionInstance)):
                   # if the owner of the tool accepted the tool request:
                    if utilities.notificationUtilities.getNotifResponse(actionUtil.getRequestNotifOfAction(actionInstance)) == 'Accept':
                        #update the tool's borrowedTime field
                        actionInstance.tool.borrowedTime = timezone.now()
                        #update tool's borrower field
                        toolUtil.updateToolBorrower(actionInstance.tool,actionInstance.requester)
                        #set tool to be unavailable to borrow by other users
                        toolUtil.updateToolAvailability(actionInstance.tool, False)
                        #move tool location to requester's shed
                        targetShed = shedUtil.getShedByName(profileUtil.getUserOfProfile(actionInstance.requester).username + "'s Shed")[0]
                        #save the name of the shed that the tool used to be in
                        actionInstance.workSpace = actionInstance.tool.myShed.name
                        #remove the tool from it's old location first
                        shedUtil.removeToolFromShed(actionInstance.tool.myShed, actionInstance.tool)
                        #then add the tool to the requester's personal shed
                        shedUtil.addToolToShed(targetShed, actionInstance.tool)
                        toolUtil.updateToolLocation(actionInstance.tool, targetShed.location)
                        #delete the borrow request notification from the database so it no longer displays
                        actionUtil.getRequestNotifOfAction(actionInstance).delete()
                        #proceed to next state
                        response = "Your request to borrow " + \
                                        actionInstance.tool.name + " from " + actionInstance.workSpace + \
                                        " has been accepted!"
                        utilities.notificationUtilities.createInfoNotif(actionInstance, actionInstance.requester, response)
                        actionInstance.currrentState = "borrowed"
                        actionInstance.save()

                    # the owner of the tool declined the borrowing of the tool
                    else:
                        #delete the borrow request notification from the database so it no longer displays
                        actionUtil.getRequestNotifOfAction(actionInstance).delete()
                        #send an info notification to the requester saying he was denied
                        response = "Your request to borrow " + \
                                        actionInstance.tool.name + " from " + actionInstance.tool.myShed.name + \
                                        " has been denied."
                        utilities.notificationUtilities.createInfoNotif(actionInstance,actionInstance.requester,response)
                        #proceed to next state
                        actionInstance.currrentState = "idle"
                        actionInstance.save()

            elif actionInstance.currrentState == "borrowed":
                #check if borrowedTime of tool was older than [maxBorrowTime] days ago
                if toolUtil.toolIsOverdraft(actionInstance.tool):
                    #notify requester that they are overdraft and they should return [tool]
                    message = "Uh oh...your " + actionInstance.tool.name + " is overdrafted!"
                    utilities.notificationUtilities.createInfoNotif(actionInstance, actionInstance.requester, message)
                    #move to overdraft state
                    actionInstance.currentState = "overdraft"
                    actionInstance.save()
                    processActions()

            elif actionInstance.currrentState == "overdraft":
                #disable the user from borrowing any more tools
                actionInstance.tool.borrower.canBorrow = False
                actionInstance.save()
                processActions()

            #moving into the "markedReturned" state is handled by the UI
            elif actionInstance.currrentState == "markedReturned":
                actionInstance.currrentState = "confirmReturned"
                actionInstance.save()
                question = "Your " + actionInstance.tool.name + " has been marked as returned to " + \
                                actionInstance.workSpace + ".  Has this tool been returned?"
                userOptions = "Accept,Deny" #adding options       
                utilities.notificationUtilities.createResponseNotif(actionInstance, actionInstance.tool.owner, \
                                                            question, options = userOptions)
                processActions()

            elif actionInstance.currrentState == "confirmReturned":
                if utilities.notificationUtilities.notifHasResponse(actionUtil.getRequestNotifOfAction(actionInstance)):
                    # if the owner of the tool denied that it has been returned:
                    if utilities.notificationUtilities.getNotifResponse(actionUtil.getRequestNotifOfAction(actionInstance)) == "Deny":
                        #delete the borrow request notification from the database so it no longer displays
                        actionUtil.getRequestNotifOfAction(actionInstance).delete()
                        #send a passive aggressive notif
                        response = "The owner of " + \
                                        actionInstance.tool.name + " has indicated that you failed to return the tool to " \
                                        + actionInstance.workSpace + ".  Please do not mark a tool as returned unless " \
                                        + "it has actually been returned to its shed."
                        utilities.notificationUtilities.createBadInfoNotif(actionInstance, actionInstance.requester, response)
                        actionInstance.currrentState = "borrowed"
                        actionInstance.save()

                    # the owner of the tool confirmed it has been returned
                    else:
                        #delete the borrow request notification from the database so it no longer displays
                        actionUtil.getRequestNotifOfAction(actionInstance).delete()
                        #proceed to next state
                        actionInstance.currrentState = "returned"
                        actionInstance.save()
                        processActions()

            elif actionInstance.currrentState == "returned":
                #notify tool owner that his tool has been returned
                # message = "Your " + actionInstance.tool.name + " has been returned to " + \
                #                 actionInstance.workSpace + ". "
                # utilities.notificationUtilities.createInfoNotif(actionInstance, actionInstance.tool.owner, message)
                #reduce user reputation by 5 for every day the tool is late!
                timeSinceBorrowed = timezone.now() - actionInstance.tool.borrowedTime
                for day in range(timeSinceBorrowed.days):
                    actionInstance.tool.borrower.reputation -= 5
                #update the tool's borrower field
                actionInstance.tool.borrower = None
                #set tool to be unavailable to borrow by other users
                toolUtil.updateToolAvailability(actionInstance.tool, True)
                #remove the tool from personal shed and move it back to the shed it was borrowed from
                shedUtil.removeToolFromShed(actionInstance.tool.myShed, actionInstance.tool)
                oldShed = shedUtil.getShedByName(actionInstance.workSpace)[0]
                shedUtil.addToolToShed(oldShed, actionInstance.tool)
                toolUtil.updateToolLocation(actionInstance.tool, oldShed.location)
                #move to idle state
                actionInstance.currentState = "idle"
                actionInstance.save()

            elif actionInstance.currrentState == "idle":
                #delete action object
                actionInstance.delete()

    
        elif actionUtil.isShedRequest(actionInstance):

            #this state will be entered when the "Join Shed" button is clicked
            if actionInstance.currrentState == "userShedRequest":
                #send shed request notif to all admins of shed and the shed owner
                adminList = shedUtil.getAllAdminsOfShed(actionInstance.shed)
                #print(adminList)
                for admin in adminList:
                    #newAction = actionUtil.createShedRequestAction(actionInstance.shed,actionInstance.requester)
                    content = actionInstance.requester.user.username +  ' has requested to join your shed "' + \
                                actionInstance.shed.name + '."'
                    userOptions = "Accept,Deny"
                    utilities.notificationUtilities.createResponseNotif(actionInstance,admin,content,userOptions)
                    #newAction.currrentState = 'acceptDeny'
                    #newAction.save()
                #move to acceptDeny state
                actionInstance.currrentState = "acceptDeny"
                actionInstance.save()

            elif actionInstance.currrentState == "acceptDeny":
                #if the notification has been responded to
                if utilities.notificationUtilities.notifHasResponse(actionUtil.getRequestNotifOfAction(actionInstance)):
                    #if the admin responded 'Accept'
                    if utilities.notificationUtilities.getNotifResponse(actionUtil.getRequestNotifOfAction(actionInstance)) == 'Accept':
                        #add the guy to the shed
                        shedUtil.addMemberToShed(actionInstance.shed, actionInstance.requester)
                        #delete the notif that asked about accepting and denying
                        message = "You have been approved to join " + actionInstance.shed.name + "!"
                        utilities.notificationUtilities.createInfoNotif(actionInstance,actionInstance.requester,message)
                        actionUtil.getRequestNotifOfAction(actionInstance).delete()
                        actionInstance.currrentState = "idle"
                        actionInstance.save()
                    else:
                        #send an info notification to the requester saying he was denied
                        message = "You have been denied from joining " + actionInstance.shed.name + "."
                        utilities.notificationUtilities.createInfoNotif(actionInstance,actionInstance.requester,message)
                        actionUtil.getRequestNotifOfAction(actionInstance).delete()
                        #proceed to next state
                        actionInstance.currrentState = "idle"
                        actionInstance.save()

            elif actionInstance.currrentState == "idle":
                #delete the action object from the database
                actionInstance.delete()