示例#1
0
 def __init__(self, helperHandler, notificationHelper):
     self.helperHandler = helperHandler
     self.validCodes = self.helperHandler.extractQRCodesFromFile()
     self.validLocations = self.helperHandler.getValidLocationCodes()
     self.userDao = UserDAO()
     self.relationdao = RelationshipDAO()
     self.containerdao = ContainerDAO()
     self.locationdao = LocationDao()
     self.notificationHelper = notificationHelper
示例#2
0
 def __init__(self, helperHandler):
     self.helperHandler = helperHandler
     self.beams_client = PushNotifications(
         instance_id='7032df3e-e5a8-494e-9fc5-3b9f05a68e3c',
         secret_key=
         '8AC9B8AABB93DFE452B2EFC2714FCF923841B6740F97207F4512F240264FF493')
     self.userDao = UserDAO()
     self.containerDao = ContainerDAO()
     self.authDao = AuthDao()
     self.locationDao = LocationDao()
示例#3
0
class ContainerHandler:
    def __init__(self, helperHandler, notificationHelper):
        self.helperHandler = helperHandler
        self.validCodes = self.helperHandler.extractQRCodesFromFile()
        self.validLocations = self.helperHandler.getValidLocationCodes()
        self.userDao = UserDAO()
        self.relationdao = RelationshipDAO()
        self.containerdao = ContainerDAO()
        self.locationdao = LocationDao()
        self.notificationHelper = notificationHelper

    def validateQRCode(self, qrcode, isLocation):
        # make sure valid qrcode
        if isLocation == False:
            res = self.containerdao.selectContainer(qrcode)
        elif isLocation == True:
            res = self.locationdao.selectByLocationQRcode(qrcode)
        if res[0] == False and isLocation == False:
            raise Exception('That is not a valid QR code.')
        elif res[0] == False and isLocation == True:
            raise Exception('That is not a valid Location.')

    def addContainer(self, request, containerDao):
        return self.containerCRUDS(request, containerDao, "insertContainer")

    def getContainer(self, request, containerDao):
        return self.containerCRUDS(request, containerDao, "selectContainer")

    def deleteContainer(self, request, containerDao):
        return self.containerCRUDS(request, containerDao, "deleteContainer")

    def updateContainer(self, request, containerDao):
        return self.containerCRUDS(request, containerDao, "updateContainer")

    #function values 0->add 1->get 2->delete
    def containerCRUDS(self, request, containerDao, function):
        containerDic = None
        t = "json"
        keys = ["qrcode", "auth_token", "email"]
        if function == ("selectContainer" or "updateContainer"):
            t = "args"
        try:
            containerDic = self.helperHandler.handleRequestAndAuth(
                request=request, keys=keys, t=t, hasAuth=True)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        container = Container()
        container.dictToContainer(containerDic)
        if (function == "selectContainer"):
            function = "self.containerdao." + function + "(containerDic['qrcode'])"

        else:
            function = "self.containerdao." + function + "(container)"
        res = eval(function)
        if (type(res[1]) == type(container)):
            res = res[0], res[1].containerToDict()
        return self.helperHandler.handleResponse(res)

    def allContainers(self, request, containerDao):
        containerDic = None
        keys = ['email', 'auth_token']
        containers = []
        try:
            containerDic = self.helperHandler.handleRequestAndAuth(
                request, keys, t="args", hasAuth=True)
            res = self.containerdao.selectAll()
            self.helperHandler.falseQueryCheck(res)
            #BinCounts = self.containerdao.totalContainersInBins()
            #self.helperHandler.falseQueryCheck(BinCounts)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        #Bin = BinCounts[1]
        for r in res[1]:
            tempCont = r.containerToDict()
            containers.append(tempCont)
        res = res[0], containers
        return self.helperHandler.handleResponse(res)

    def checkoutContainer(self, userContainer):
        try:
            self.validateQRCode(userContainer['qrcode'], False)
            userContainer['description'] = None
        except Exception as e:
            raise Exception(e)
        return userContainer

    def secretCheckout(self, userContainer):
        try:
            userContainer['status'] = "Pending Return"
            res = self.relationdao.selectAllByStatus(userContainer['email'],
                                                     userContainer['status'])
            self.helperHandler.falseQueryCheck(res)
            rel = (res[1][len(res[1]) - 1]
                   )  # retrieves the most recent pending return
            userContainer = rel.relationshipToDict()
            userContainer['status'] = "Checked Out"
            userContainer['email'] = "*****@*****.**"
        except Exception as e:
            raise Exception(e)
        return userContainer

    #Accepts list val in format  val = (email, qrcode, status, statusUpdateTime)
    def addRelationship(self, request, containerDao, relationshipDAO):
        userContainer = None
        hasAuth = True
        if "/checkoutContainer" in str(request):
            keys = [
                'email', 'qrcode', 'status', 'auth_token', 'location_qrcode'
            ]
            func = "self.checkoutContainer(userContainer)"  # ask the database team if they are check for pendings that will switch to returned for older user
        elif "/secretCheckout" in str(request):
            keys = ['email']
            func = "self.secretCheckout(userContainer)"
            hasAuth = False
        try:
            userContainer = self.helperHandler.handleRequestAndAuth(
                request=request, keys=keys, hasAuth=hasAuth)
            userContainer = eval(func)
            userContainer['statusUpdateTime'] = datetime.now().strftime(
                '%Y-%m-%d %H:%M:%S')
            old_code = relationshipDAO.getRecentUser(userContainer['qrcode'])

            #If user has already checked out the container they cannot check it out
            if "/checkoutContainer" in str(request):
                logging.info("Entering isCheckedOut testing")
                try:
                    containerStatus = relationshipDAO.isCheckedOut(
                        userContainer['email'], userContainer['qrcode'])
                    if (containerStatus[0] == True):
                        logging.info("Container is checked out already")
                        return json.dumps({
                            "success":
                            False,
                            "message":
                            "You already have this container Checked Out"
                        })
                except Exception as e:
                    logging.info("Exiting checked out testing")
            relationship = Relationship()
            relationship.dictToRelationship(userContainer)
            res = self.relationdao.insertRelationship(relationship)
            self.helperHandler.falseQueryCheck(res)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        # send notification
        if old_code[0] is True:
            self.notificationHelper.sendNotification(old_code[1])
        return self.helperHandler.handleResponse(res)

    def getContainersForUser(self, request, containerDao, isSorted):
        relationship = None
        if "/secretGetRelationships" in str(request):
            keys = ['email']
            hasAuth = False
        else:
            keys = ['email', 'auth_token']
            hasAuth = True
        try:
            relationship = self.helperHandler.handleRequestAndAuth(
                request=request, keys=keys, t="args", hasAuth=hasAuth)
            res = self.relationdao.selectAllByEmail(relationship['email'])
            self.helperHandler.falseQueryCheck(res)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        #print(res)
        res[1].reverse()
        dictList = []
        for item in res[1]:
            item = item.relationshipToDict()
            dictList.append(item)
        res = (True, dictList)
        if isSorted is True:
            sortDict = {
                'All': dictList,
                'Checked_Out': [],
                'Pending_Return': [],
                'Verified_Return': [],
                'Damaged_Lost': []
            }
            for item in dictList:
                #print(item['status'].replace(' ', '_'))
                sortDict[item['status'].replace(' ', '_')].append(item)
            res = (True, sortDict)
        return self.helperHandler.handleResponse(res)

    def reportContainer(self, userContainer):
        try:
            userContainer['location_qrcode'] = None
        except Exception as e:
            raise Exception(e)
        return userContainer

    def checkinContainer(self, userContainer):
        try:
            self.validateQRCode(userContainer['location_qrcode'], True)
            userContainer['statusUpdateTime'] = datetime.now().strftime(
                '%Y-%m-%d %H:%M:%S')
        except Exception as e:
            raise Exception(e)
        return userContainer

    def undoReport(self, userContainer):
        try:
            userContainer['status'] = 'Checked Out'
            userContainer['description'] = None
        except Exception as e:
            raise Exception(e)
        return userContainer

    def updateRelationship(
        self, request, containerDao
    ):  # we are going to do loction than the container so get loction for the front end here
        userContainer = None
        if "/checkinContainer" in str(request):
            keys = [
                'email', 'qrcode', 'status', 'auth_token', 'location_qrcode'
            ]
            func = "self.checkinContainer(userContainer)"
        elif "/reportContainer" in str(request):
            keys = ['email', 'qrcode', 'status', 'auth_token', 'description']
            func = "self.reportContainer(userContainer)"
        elif "/undoReportContainer" in str(request):
            keys = ['email', 'qrcode', 'auth_token']
            func = "self.undoReport(userContainer)"
        try:
            userContainer = self.helperHandler.handleRequestAndAuth(
                request, keys)
            userContainer = eval(func)
            self.validateQRCode(userContainer['qrcode'], False)

            #call method in relationdao that checks if current container is checked out, if not return json dumps false
            if "/checkinContainer" in str(request):
                try:
                    containerStatus = self.relationdao.isCheckedOut(
                        userContainer['email'], userContainer['qrcode'])
                    if (containerStatus[0] == False):
                        logging.info("Container not Checked Out")
                        return json.dumps({
                            "success":
                            False,
                            "message":
                            "Cannot return a Container that is Pending Return/Verified Return"
                        })
                except Exception as e:
                    logging.info("Exiting isPendingReturn testing")

            rel = self.relationdao.selectActiveQRcode(userContainer["qrcode"])
            if "/checkinContainer" in str(request):
                reward = self.addPoints(rel[1][0])
            self.helperHandler.falseQueryCheck(rel)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        #print(dictOfUserAttrib)
        #change over to search by qrcode and active = 1
        relationship = rel[1][0]
        relDict = relationship.relationshipToDict()
        if "/undoReportContainer" in str(
                request) and relDict['status'] != "Damaged Lost":
            return json.dumps({
                "success": False,
                "message": "Container is not Damaged/Lost"
            })
        for key in userContainer:
            if key != "auth_token" and key != "email":
                relDict[key] = userContainer[key]
        relationship.dictToRelationship(relDict)
        res = self.relationdao.updateRelationship(relationship)
        if "/checkinContainer" in str(request):
            res = reward
        return self.helperHandler.handleResponse(res)

    def addPoints(self, rel):
        try:
            f = '%Y-%m-%d %H:%M:%S'
            relDict = rel.relationshipToDict()
            res = self.userDao.selectUser(relDict['email'])
            self.helperHandler.falseQueryCheck(res)
            user = res[1]
            userDict = user.userToDict()
            points = userDict['points']
            checkoutTime = str(relDict['statusUpdateTime'])
            authtimets = datetime.strptime(checkoutTime, f)
            timepassed = datetime.now() - authtimets
            if (timepassed.total_seconds() /
                    3600) >= 48 and relDict['status'] == "Checked Out":
                points = points + 5
                userDict['points'] = points
                reward = self.rewardCheck(15, userDict)
            elif (timepassed.total_seconds() /
                  3600) < 48 and relDict['status'] == "Checked Out":
                points = points + 15
                userDict['points'] = points
                reward = self.rewardCheck(15, userDict)
            else:
                reward = ""
            user.dictToUser(userDict)
            res = self.userDao.updateUser(user)
            self.helperHandler.falseQueryCheck(res)
        except Exception as e:
            raise Exception(e)
        return (True, reward)

    def rewardCheck(self, newPoints, userDic):
        if userDic['points'] // 500 > 0 and userDic['points'] % 500 < newPoints:
            userDic['reward_date'] = datetime.now().strftime(
                '%Y-%m-%d %H:%M:%S')
            return {'newReward': True, 'points': newPoints}
        else:
            return {'newReward': False, 'points': newPoints}

    def deleteRelationship(self, request, relationshipDao, hasAuth):
        relDict = None
        #auth_token not required for testing
        keys = ['email', 'qrcode']
        try:
            relDict = self.helperHandler.handleRequestAndAuth(request,
                                                              keys,
                                                              hasAuth=hasAuth)
            self.validateQRCode(relDict['qrcode'], False)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        #get relationship object based on email and qrcode
        rel = self.relationdao.selectRelationship(relDict['email'],
                                                  relDict['qrcode'])
        if rel[0] is False:
            return self.helperHandler.handleResponse(rel)
        relationship = rel[1]
        relDict = relationship.relationshipToDict()
        #delete relationship from table
        res = self.relationdao.deleteRelationship(relationship)
        return self.helperHandler.handleResponse(res)
# to get all containers for admin

    def GetRelationships(self, request, relationshipDao, hasAuth):
        relDict = None
        keys = ['email', 'auth_token']
        try:
            relDict = self.helperHandler.handleRequestAndAuth(request,
                                                              keys,
                                                              t="args",
                                                              hasAuth=True)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        if '/getallContainers' in str(request):
            rel = self.relationdao.selectAll()
            if rel[0] is False:
                return self.helperHandler.handleResponse(rel)
            relDict = []
            for item in rel[1]:
                relDict.append(item.relationshipToDict())
            rel = (True, relDict)
        elif '/getCounts' in str(request):
            sitedic = {
                "In Stock": self.containerdao.totalContainersInStock()[1],
                "Checked Out":
                self.containerdao.totalContainersCheckedOut()[1],
                "In Bin": self.containerdao.totalContainersInBins()[1],
                "Damaged Lost":
                self.containerdao.totalContainersDamagedLost()[1]
            }
            rel = [True, sitedic]
            if rel[0] is False:
                return self.helperHandler.handleResponse(rel)
        elif '/getCurrent' in str(request):
            rel = self.containerdao.selectRecentStatus()
            if rel[0] is False:
                return self.helperHandler.handleResponse(rel)

        return self.helperHandler.handleResponse(rel)
示例#4
0
# script to insert all QR Codes into the database tables

from container import Container
from containerDAO import ContainerDAO

from location import Location
from locationDAO import LocationDao

import mysql.connector
dao = ContainerDAO()
#dao2 = LocationDAO()


class insertQRcodes:
    def main():
        with open('containers.txt') as f:
            #print(f.read())
            for qrcode in f:
                c = Container(qrcode.rstrip("\n"))
                dao.insertContainer(c)
                print("successfully inserted", qrcode.rstrip("\n"))
        """
        with open('locations.txt') as f2:
            #print(f2.read())
            for qrcode in f2:
                l = Location(location_qrcode,description,lastPickup,containers)
                dao2.insertLocation(l)
                print("successfully inserted", location_qrcode.rstrip("\n"))
        """

    main()
示例#5
0
class unitTestContainerDAO(unittest.TestCase):
    """
    Test the containerDAO class methods using the unit test framework.  
    To run these tests:
         python3 -m unittest unitTestContainerDAO.py
    """
    def setUp(self):
        """
       Setup a temporary database
       """
        self.dao = ContainerDAO()
        self.dao.changeDatabase("temp")

    def tearDown(self):
        """
        Delete the temporary database
        """
        c = Container("101010")  # c is container object
        self.dao.deleteContainer(c)
        del self.dao

    # TEST CREATE CONTAINER
    def testInsertContainerSmoke(self):
        c = Container("101010")
        return self.dao.insertContainer(c)

    def testInsertContainer(self):
        """
        Test that we can add a container that doesn't exist in the database
        """
        rc, msg = self.testInsertContainerSmoke()
        self.assertTrue(rc)
        """
        Check to see that the add went through
        """
        qrcode = "101010"
        rc, selectContainer = self.dao.selectContainer(qrcode)
        self.assertTrue(rc)
        self.assertEqual(qrcode, selectContainer.qrcode)

    def testInsertContainerTwice(self):
        """
        Test that we can't add a container twice
        First add should work correctly
        """
        rc, msg = self.testInsertContainerSmoke()
        self.assertTrue(rc)
        """
        Second add should fail
        """
        rc, msg = self.testInsertContainerSmoke()
        self.assertFalse(rc)
        self.assertEqual(msg, "Duplicate Entry")

    # TEST READ CONTAINER
    def testSelectContainer(self):
        """
        Test that we can select a container that exists in the database already
        """
        rc, msg = self.testInsertContainerSmoke()
        self.assertTrue(rc)

        qrcode = "101010"
        rc, selectContainer = self.dao.selectContainer(qrcode)
        self.assertTrue(rc)
        self.assertEqual(qrcode, selectContainer.qrcode)

    def testTotalContainersCheckedOut(self):
        rc, msg = self.dao.totalContainersCheckedOut()
        self.assertTrue(rc)

    def testTotalContainersDamagedLost(self):
        rc, msg = self.dao.totalContainersDamagedLost()
        self.assertTrue(rc)

    def testTotalContainersInStock(self):
        rc, msg = self.dao.totalContainersInStock()
        self.assertTrue(rc)

    def testTotalContainersInBins(self):
        rc, msg = self.dao.totalContainersInBins()
        self.assertTrue(rc)

    def testSelectRecentStatus(self):
        rc, msg = self.dao.selectRecentStatus()
        self.assertTrue(rc)

    def testSelectContainerDoesntExist(self):
        """
        Test that we can't select a container that doesnt exist in the database already
        """
        rc, msg = self.testInsertContainerSmoke()
        self.assertTrue(rc)

        qrcode = "101011"
        rc, selectContainer = self.dao.selectContainer(qrcode)
        self.assertFalse(rc)

        qrcode = None
        rc, selectContainer = self.dao.selectContainer(qrcode)
        self.assertFalse(rc)

    # TEST UPDATE CONTAINER
    def testUpdateContainer(self):
        """
        Test that we can update a container in the database
        """
        """
        rc, msg = self.testInsertContainerSmoke()
        self.assertTrue(rc)

        c = Container("Updated101010") # should we test to update active and description?
        rc, updateContainer = self.dao.updateContainer(c)
        self.assertTrue(rc)

    # but this unit test doesn't pass :(
    """

    # TEST DELETE CONTAINER
    def testDeleteContainer(self):
        """
        Test that we can delete a container from the database
        """
        rc, msg = self.testInsertContainerSmoke()
        self.assertTrue(rc)

        c = Container("101010")
        rc, deleteContainer = self.dao.deleteContainer(c)
        self.assertTrue(rc)
        """
        Verify that the container has actually been deleted.
        """
        rc, selectContainer = self.dao.selectContainer("101010")
        self.assertFalse(rc)

    def TestDeleteContainerDoesNotExist(self):
        """
        Test that we can't delete a container that doesn't exist.
        """
        rc, msg = self.testInsertContainerSmoke()
        self.assertTrue(rc)

        c = Container("101010")
        rc, deleteContainer = self.dao.deleteContainer(c)
        self.assertTrue(rc)
        """
        QR Code already deleted.
        """
        rc, deleteContainer = self.dao.deleteContainer(c)
        self.assertFalse(rc)

        c = Container("101010")
        """
        QR code never in database.
        """
        rc, deleteContainer = self.dao.deleteContainer(c)
        self.assertFalse(rc)

        c = None
        """
        Container is NULL
        """
        rc, deleteContainer = self.dao.deleteContainer(c)
        self.assertFalse(rc)

    def testInsertContainerQRCodeTooLong(self):
        """
        Test that we cannot add a qrcode to container that is over 45 characters long
        """
        c = Container(
            "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
        )

        rc, insertContainer = self.dao.insertContainer(c)
        self.assertFalse(rc)
示例#6
0
 def setUp(self):
     """
    Setup a temporary database
    """
     self.dao = ContainerDAO()
     self.dao.changeDatabase("temp")
示例#7
0
 def __init__(self, helperHandler):
     self.helperHandler = helperHandler
     self.locationdao = LocationDao()
     self.containerdao = ContainerDAO()
示例#8
0
class LocationHandler:
    def __init__(self, helperHandler):
        self.helperHandler = helperHandler
        self.locationdao = LocationDao()
        self.containerdao = ContainerDAO()

    def locationcheckandAuth(self, request, locationDao):
        locationDic = None
        keys = ["qrcode", 'email', 'auth_token']
        try:
            locationDic = self.helperHandler.handleRequestAndAuth(
                request, keys)
            res = self.locationdao.selectByLocationQRcode(
                locationDic['qrcode'])
            self.helperHandler.falseQueryCheck(res)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        if "/selectLocation" in str(request):
            return self.selectLocation(request, locationDao, res)
        elif "/clearLocation" in str(request):
            return self.clearlocation(request, locationDao, res)
        elif "/deleteLocation" in str(request):
            return self.deleteLocation(request, locationDao, res)

    def selectLocation(self, request, locationDao, res):
        res = res[0], res[1].locationToDict()
        return self.helperHandler.handleResponse(res)

    def clearlocation(self, request, locationDao, res):
        res[1].lastPickup = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        res2 = self.locationdao.updateLocation(res[1])

        return self.helperHandler.handleResponse(res2)

    def addLocation(self, request, locationDao):
        locationDic = None
        keys = ['location_qrcode', 'email', 'auth_token', 'description']
        try:
            locationDic = self.helperHandler.handleRequestAndAuth(
                request, keys)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        locationDic['lastPickup'] = datetime.now().strftime(
            '%Y-%m-%d %H:%M:%S')
        location = Location()
        location.dictToLocation(locationDic)
        res = self.locationdao.insertLocation(location)
        return self.helperHandler.handleResponse(res)

    def deleteLocation(self, request, locationDao, res):
        res2 = locationDao.deleteLocation(res[1])
        return self.helperHandler.handleResponse(res2)

    def allLocations(self, request, locationDao):
        locationDic = None
        keys = ['email', 'auth_token']
        locations = []
        try:
            locationDic = self.helperHandler.handleRequestAndAuth(request,
                                                                  keys,
                                                                  t="args",
                                                                  hasAuth=True)
            res = self.locationdao.selectAll()
            self.helperHandler.falseQueryCheck(res)
            BinCounts = self.containerdao.totalContainersInBins()
            self.helperHandler.falseQueryCheck(BinCounts)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})
        Bin = BinCounts[1]
        for r in res[1]:
            tempLoc = r.locationToDict()
            counter = 0
            for item in Bin:
                if item['location_qrcode'] == tempLoc['location_qrcode']:
                    counter = counter + 1
            tempLoc.update({'count': counter})
            locations.append(tempLoc)
        res = res[0], locations
        return self.helperHandler.handleResponse(res)

    def updateLocation(self, request, locationDao):
        locationDic = None
        keys = ['qrcode', 'email', 'auth_token', 'description']
        try:
            locationDic = self.helperHandler.handleRequestAndAuth(
                request, keys)
            res = self.locationdao.selectByLocationQRcode(
                locationDic['qrcode'])
            self.helperHandler.falseQueryCheck(res)
        except Exception as e:
            return json.dumps({"success": False, "message": str(e)})

        res[1].description = locationDic['description']
        res2 = self.locationdao.updateLocation(res[1])
        return self.helperHandler.handleResponse(res2)
示例#9
0
from emailServer import EmailManager
from authHandler import AuthHandler
from helperHandler import HelperHandler
from userHandler import UserHandler
from containerHandler import ContainerHandler
from locationHandler import LocationHandler

#app methods
app = Flask(__name__)
CORS(app)
logging.basicConfig(filename='demo.log', level=logging.DEBUG)
app.debug = True

#daos and objects
userDao=UserDAO()
containerDao=ContainerDAO()
authDao = AuthDao()
locationDao=LocationDao()
appinfoDao=appInfoDAO()
relationshipDao = RelationshipDAO()
emailServer = EmailManager()
notificationHelper = NotificationHelper()

#handlers
helperHandler = HelperHandler(emailServer)
authHandler = AuthHandler(helperHandler)
userHandler = UserHandler(helperHandler)
containerHandler = ContainerHandler(helperHandler, notificationHelper)
locationHandler = LocationHandler(helperHandler)

#SSL/HTTPS config