def test_getAllActions (self): self.assertTrue ((self.genBorrowReq in actUtils.getAllActions()) and (self.genJoinReq in actUtils.getAllActions()))
def test_getAllActions(self): self.assertTrue((self.genBorrowReq in actUtils.getAllActions()) and (self.genJoinReq in actUtils.getAllActions()))
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()
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()