Esempio n. 1
0
    def showCarLocations(self):

        with DatabaseUtils() as db:
            self.carLocations = db.getCarLocations()

        url = "http://maps.google.com/maps/api/staticmap?size=1280x1280&scale=2&markers=size:small|color:blue|"

        for car in self.carLocations:
            url = url + car[1] + "|"

        url = url + "&key=KEY"

        print("\nCar Locations: " + url)

        img = requests.get(url).content
        currDate = str(date.today())
        try:
            filename = "maps/" + currDate + ".png"
            with open(filename, 'wb') as handler:
                handler.write(img)
        except:
            filename = "MasterPi/maps/" + currDate + ".png"
            with open(filename, 'wb') as handler:
                handler.write(img)

        print("\nNew Map Picture Added to MasterPi/maps directory")

        self.printMenu()
Esempio n. 2
0
    def start(self):
        """
            initialises database if not already created.
            runs ui for user
        """

        with DatabaseUtils() as db:
            db.createDatabase()
            #initialise database
        print("\nWelcome")
        running = True
        self.printMenu()
        while running:
            try:  #get user input
                choice = int(input("> "))
            except ValueError:
                print("Please enter number within menu options")
            else:
                if choice > 3 or choice <= 0:
                    print("Please enter number within menu options")
                else:
                    if choice == 1:
                        self.register()
                    elif choice == 2:
                        self.login()
                    elif choice == 3:
                        running = False
                        print("\nThank you")
Esempio n. 3
0
    def login(self):
        """
            prompts user for their username and password
            checks off credentials with database.
            If correct, enters master menu.
        """
        currentUser = input("Enter Username: "******"CUSTOMER":
                        userMenu = mastermenu.CustomerMenu()
                    elif user[2] == "ADMIN" or user[2] == "MANAGER" or user[
                            2] == "ENGINEER":
                        userMenu = mastermenu.AdminMenu()

                    userMenu.start(currentUser)
                    found = True
            if not found:
                print("Incorrect username or password")

        print("\nReturning to login..\n")
        self.printMenu()
Esempio n. 4
0
 def cancelBooking(self):
     """
         allows user to cancel booking given their booken id when prompted
     """
     eventID = input("Enter Booking ID. : ")
     try:  #delete booking given ID from both calendar and DB
         CalendarUtils().deleteEvent(eventID)
         with DatabaseUtils() as db:
             db.deleteBooking(eventID)
     except:
         print("\nError: Booking Doesnt Exist")
     self.printMenu()
Esempio n. 5
0
    def reportFault(self):
        """
            report fault with given rego.
            optionally sends fault to engineer via pushbullet
        """
        print("Report Fault:")
        with DatabaseUtils() as db:
            rego = input("Enter rego of faulty vehicle: ")

            cars = db.searchCars("Rego", rego)
            if cars:
                fault = input("Please provide description of fault for %s: " %
                              rego)
                if db.reportFault(rego, fault):
                    locations = db.getCarLocations()
                    for location in locations:
                        if location[0] == rego:
                            fault = fault + "\nVehicle located at {}".format(
                                location[1])

                    print(
                        "Fault reported on and location recorded for %s, assign engineer? y/n"
                        % rego)
                    while True:
                        send = input("> ")
                        if send == "y":
                            pushbullet = PushbulletAccess(
                            )  #send via pushbullet to account
                            if pushbullet.send_notification(rego, fault):
                                print("Notification sent")
                                break
                            else:
                                print(
                                    "Fault reported\nhowever error in notifying engineer."
                                )
                                break
                        elif send == "n":
                            print("Fault reported. No notifcation sent.")
                            break
                        else:
                            print("please enter y/n")

                else:
                    print("Error in reporting fault.")
            else:
                print("No such rego exists")

        self.printMenu()
Esempio n. 6
0
    def bookingHistory(self, user):
        """
            prints booking history for given user
            in format:
            Booking id      Rego     StartDate    EndDate
        """
        print("Booking History for " + user + ": ")
        #connect to database and get booking history for user
        with DatabaseUtils() as db:
            self.bookings = db.getBookingHistory(user)

        data = ["Booking ID", "Rego", "Start Date", "Finish Date"]
        row_format = "{:<17}" * (len(data))
        print(row_format.format(*data))
        for bookings in self.bookings:
            print(
                row_format.format(str(bookings[0]), bookings[1],
                                  str(bookings[2]), str(bookings[3])))
        self.printMenu()
Esempio n. 7
0
    def start(self, user):
        """
            alternate start menu for admin account
        """
        #cleans terminal for new menu
        print(chr(27) + "[2J")
        running = True
        print("\nWelcome " + user + "\n")
        self.printMenu()
        self.user = user
        #start menu display and assure database is up to date
        with DatabaseUtils() as db:
            self.cars = db.getAllCars()
            self.userList = db.getUsers()
            if not self.cars:
                db.populateDatabase()
                self.cars = db.getAllCars()

        while (running):
            try:
                choice = int(input("> "))
            except ValueError:
                print("Please enter number within menu options")
            else:
                if choice > 7 or choice <= 0:
                    print("Please enter number within menu options")
                else:  #get user input
                    if choice == 1:
                        self.rentalHistory()
                    elif choice == 2:
                        self.search()
                    elif choice == 3:
                        self.updateCar()
                    elif choice == 4:
                        self.updateUsers()
                    elif choice == 5:
                        self.reportFault()
                    elif choice == 6:
                        self.showCarLocations()
                    elif choice == 7:
                        running = False
                        print("Thank you")
Esempio n. 8
0
    def bookCar(self):
        """
            allows logged in user to book car given rego number, prompts for dates and creates calendar event
        """

        rego = input("Enter Rego: ")
        regex = re.compile(
            "([12]\d{3}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01]))$")

        validDate = False
        while validDate == False:
            startDate = input("Input Booking Start Date (YYYY-MM-DD): ")
            endDate = input("Input Booking End Date (YYYY-MM-DD): ")
            if (regex.match(startDate)
                    and regex.match(endDate)):  #check input is in date format
                startYear, startMonth, startDay = startDate.split('-')
                startDate = datetime.date(int(startYear), int(startMonth),
                                          int(startDay))
                #get user date data to create booking
                endYear, endMonth, endDay = endDate.split('-')
                endDate = datetime.date(int(endYear), int(endMonth),
                                        int(endDay))
                if (endDate < startDate):
                    print("\nError: End Booking Date is Before Start Start\n")
                else:
                    validDate = True
            else:  #date validation check
                print("\nInvalid Dates, Please Try Again\n")

        with DatabaseUtils() as db:
            self.userEmail = db.getUserEmail(self.user)
            eventID = rego
            #confirmed booking can be created and calendar event is also created
            eventID = CalendarUtils().createEvent(self.user, self.userEmail,
                                                  rego, startDate, endDate)
            if (db.bookCar(self.user, rego.upper(), startDate, endDate,
                           eventID)):
                print("Your Booking ID is: " + eventID)
            else:
                print("Sorry, Booking already exists for booking date")
        self.printMenu()
Esempio n. 9
0
    def rentalHistory(self):
        """
            view rental history of a vehicle
        """
        print("Rental History:")
        rego = input("Enter Rego: ")

        with DatabaseUtils() as db:
            self.bookings = db.admin_getBookingHistory(rego)
        if self.bookings:
            data = [
                "Rego", "Booking ID", "UserID", "Start Date", "Finish Date"
            ]
            row_format = "{:<17}" * (len(data))
            print(row_format.format(*data))
            for bookings in self.bookings:
                print(
                    row_format.format(str(bookings[0]), bookings[1],
                                      str(bookings[2]), str(bookings[3]),
                                      str(bookings[4])))
        else:
            print("No bookings found \n")
        self.printMenu()
Esempio n. 10
0
    def showCars(self):
        """
            shows all available cars given todays date isnt booked for that car
            in format:
            Rego     Model    Body    Seats    Colour     Price Per Day      Location     Returned (y/n)
        """
        print("Cars Currently Available: \n")
        #connect to database and get all available cars given date
        with DatabaseUtils() as db:
            today = datetime.date.today()
            availableCars = db.getAvailableCars(today.strftime("%d-%m-%Y"))

            data = [
                "Rego", "Model", "Body", "Seats", "Colour", "Price Per Day($)",
                "Location"
            ]
            row_format = "{:<17}" * (len(data))
            print(row_format.format(*data))
            for car in availableCars:
                print(
                    row_format.format(car[0], car[1], str(car[2]), str(car[3]),
                                      str(car[4]), str(car[5]), str(car[6])))

        self.printMenu()
Esempio n. 11
0
    def start(self, user):
        #cleans terminal for new menu
        print(chr(27) + "[2J")
        running = True
        print("\nWelcome " + user + "\n")
        self.printMenu()
        self.user = user
        #start menu display and assure database is up to date
        with DatabaseUtils() as db:
            self.cars = db.getAllCars()
            if not self.cars:
                db.populateDatabase()
                self.cars = db.getAllCars()

        while (running):
            try:
                choice = int(input("> "))
            except ValueError:
                print("Please enter number within menu options")
            else:
                if choice > 6 or choice <= 0:
                    print("Please enter number within menu options")
                else:  #get user input
                    if choice == 1:
                        self.bookingHistory(user)
                    elif choice == 2:
                        self.showCars()
                    elif choice == 3:
                        self.searchCars()
                    elif choice == 4:
                        self.bookCar()
                    elif choice == 5:
                        self.cancelBooking()
                    elif choice == 6:
                        running = False
                        print("Thank you")
Esempio n. 12
0
    def start(self):
        #initalise socket
        master = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        server_address = ('localhost', 10000)
        print('starting up on {} port {}'.format(*server_address))
        master.bind(server_address)

        while True:
            #listen for incoming data from agent pi's
            master.listen(1)
            data = None
            while True:
                connection, client_address = master.accept()
                try:
                    while True:
                        data = connection.recv(100)
                        if data:
                            string = data.decode('utf-8')

                            if string[
                                    0:
                                    6] == 'RETURN':  #first return type: returning car
                                rego = string[
                                    7:13]  #update db that car is unavailable
                                print(
                                    '(Car returned: {}) Returning to UI..\n> '.
                                    format(rego),
                                    end="")
                                connection.sendall('T'.encode('utf-8'))
                                with DatabaseUtils() as db:
                                    db.returnCar(str(rego))
                            elif string[0:4] == 'REGO':
                                rego = string[5:11]
                                with DatabaseUtils() as db:
                                    cars = db.searchCars("rego", rego)
                                    if cars:
                                        connection.sendall('T'.encode('utf-8'))
                                    else:
                                        connection.sendall('F'.encode('utf-8'))
                            elif string[0:8] == 'LOCATION':
                                splitString = string.split("_")
                                carRego = splitString[1]
                                latitude = splitString[2]
                                longitude = splitString[3]

                                #Create string with both long and lat for db
                                location = latitude + "," + longitude

                                with DatabaseUtils() as db:
                                    if (
                                            db.addCarLocation(
                                                carRego, location)
                                    ):  #Return a 'T' if the db is updated succesfully
                                        print(
                                            "\n(car {} location has been updated) Returning to UI.. \n> "
                                            .format(carRego),
                                            end="")
                                        connection.sendall('T'.encode('utf-8'))
                                        found = True
                                    else:  #If it cannot update the db successfully
                                        connection.sendall('F'.encode('utf-8'))
                                        print(
                                            '\n(car {} has had attempted to update location) Returning to UI..\n> '
                                            .format(carRego),
                                            end="")
                            else:
                                rego = string[
                                    0:6]  #second return type, taking car
                                username = string[string.find('_user:'******'_pass:'******'_pass:'******'_date:') - 1]
                                date = string[string.find('_date:') +
                                              6:len(string)]
                                found = False  #check database that user login details are correct
                                with DatabaseUtils() as db:
                                    if (db.checkoutBooking(
                                            username, password, rego, date)):
                                        print(
                                            '\n(car {} has been unlocked) Returning to UI..\n> '
                                            .format(rego),
                                            end="")
                                        connection.sendall('T'.encode('utf-8'))
                                        found = True
                                    else:  #in event details are incorrect, update admin that car has had attempted access
                                        connection.sendall('F'.encode('utf-8'))
                                        print(
                                            '\n(car {} has had attempted access) Returning to UI..\n> '
                                            .format(rego),
                                            end="")
                        else:
                            break
                finally:
                    connection.close()
Esempio n. 13
0
    def updateCar(self):
        """
            update or delete any part of the car database
        """
        rego = input("Enter rego: ")
        with DatabaseUtils() as db:
            car = db.searchCars("rego", rego)
            if car:
                print("1. Edit %s?" % rego)
                print("2. Delete %s?" % rego)
                choosing = True
                while (choosing):
                    try:
                        choice = int(input("> "))
                    except ValueError:
                        print("incorrect value")
                    else:
                        if choice == 1:
                            print(
                                "Edit: \n1. Make\n2. Body\n3. Seats\n4. Colour\n5. Cost per day"
                            )
                            running = True
                            while (running):
                                try:
                                    choice = int(input("> "))
                                except ValueError:
                                    print(
                                        "Please enter number within menu options"
                                    )
                                else:
                                    if choice > 5 or choice <= 0:
                                        print(
                                            "Please enter number within menu options"
                                        )
                                    else:
                                        if choice == 1:
                                            attribute = "Make"
                                            running = False
                                        elif choice == 2:
                                            attribute = "Body"
                                            running = False
                                        elif choice == 3:
                                            attribute = "Seats"
                                            running = False
                                        elif choice == 4:
                                            attribute = "Colour"
                                            running = False
                                        elif choice == 5:
                                            attribute = "CostPerDay"
                                            running = False
                                            #get car attribute
                                if attribute is not None:
                                    print("Enter new %s data: " % attribute)
                                    editing = True
                                    valid = False
                                    while (editing):
                                        newData = input()
                                        if (attribute == "Seats"):
                                            try:
                                                newData = int(newData)
                                            except ValueError:
                                                print("incorrect value type")
                                            else:
                                                valid = True
                                                break
                                        if (attribute == "CostPerDay"):
                                            try:
                                                newData = newData
                                            except ValueError:
                                                print("incorrect value type")
                                            else:
                                                valid = True
                                                break
                                        else:
                                            valid = True
                                            break
                                    if valid:
                                        with DatabaseUtils() as db:
                                            if db.updateCar(
                                                    rego, attribute, newData):
                                                print("%s updated.\n" % rego)
                                            else:
                                                print(
                                                    "Error with update data, please try again."
                                                )
                                            choosing = False
                                    break

                        elif choice == 2:
                            print("Are you sure you wish to delete car %s?" %
                                  rego)
                            print("enter y/n")
                            while True:
                                choice = input("> ")
                                if choice == "y":
                                    with DatabaseUtils() as db:
                                        db.deleteCar(rego)
                                    print("%s deleted\n" % rego)
                                    choosing = False
                                    break
                                elif choice == "n":
                                    choosing = False
                                    break

                            else:
                                print("incorrect value")

            else:
                print("car does not exist, create new? y/n")
                while True:
                    choice = input("> ")
                    if (choice == 'y'):
                        print("create car for rego %s:" % rego)
                        manufacturer = input("Manufacturer: ")
                        body = input("body: ")
                        seats = int(input("seats: "))
                        colour = input("colour: ")
                        cost = input("cost per day ($): ")
                        with DatabaseUtils() as db:
                            if db.addCar(rego, manufacturer, body, seats,
                                         colour, cost):
                                print("Car added")
                            else:
                                print("error with car data")
                        break
                    elif (choice == 'n'):
                        break

        self.printMenu()
Esempio n. 14
0
    def searchUser(self):
        """
            admin ability to search user
        """
        print("Search by: ")
        print("1. User ID")
        print("2. Email Address")
        print("3. Name")
        print("4. User type")

        attribute = None
        running = True

        while (running):
            try:
                choice = int(input("> "))
            except ValueError:
                print("Please enter number within menu options")
            else:
                if choice > 4 or choice <= 0:
                    print("Please enter number within menu options")
                else:
                    if choice == 1:
                        attribute = "UserID"
                        running = False
                    elif choice == 2:
                        attribute = "Email"
                        running = False
                    elif choice == 3:
                        attribute = "Name"
                        running = False
                    elif choice == 4:
                        attribute = "UserType"
                        running = False

        if attribute is not None:

            if attribute == "UserType":
                print("1. customer\n2. admin\n3. manager\n4. engineer\n")
                choosing = True
                while (choosing):
                    try:
                        choice = int(input("> "))
                    except ValueError:
                        print("Please enter number within user options")
                    else:
                        if choice > 3 or choice <= 0:
                            print("Please enter number within user options")
                        else:
                            if choice == 1:
                                term = "CUSTOMER"
                                break
                            elif choice == 2:
                                term = "ADMIN"
                                break
                            elif choice == 3:
                                term = "MANAGER"
                                break
                            elif choice == 4:
                                term = "ENGINEER"
                                break
            else:
                term = input("\nEnter " + attribute + ": ")

            with DatabaseUtils() as db:
                self.searchUsers = db.searchUsers(attribute, term)

            data = ["UserID", "Name", "Email", "User Type"]
            row_format = "{:<17}" * (len(data))
            print(row_format.format(*data))
            for user in self.searchUsers:
                print(
                    row_format.format(str(user[0]), str(user[1]), str(user[2]),
                                      str(user[4])))
        else:
            print("No record of user")

        self.printMenu()
Esempio n. 15
0
    def updateUsers(self):
        """
            update or delete part of the user accounts
        """
        print("Users:")
        username = input("Enter username: "******"Name", username)
            if user:
                print("1. Edit %s?" % username)
                print("2. Delete %s?" % username)
                choosing = True
                while (choosing):
                    try:
                        choice = int(input("> "))
                    except ValueError:
                        print("incorrect value")
                    else:
                        if choice == 1:
                            print(
                                "Edit: \n1. Name\n2. Email\n3. Password\n4. User Type\n"
                            )
                            running = True
                            while (running):
                                try:
                                    choice = int(input("> "))
                                    if choice > 4 or choice <= 0:
                                        print(
                                            "Please enter number within menu options"
                                        )
                                    else:
                                        if choice == 1:
                                            attribute = "Name"
                                            running = False
                                        elif choice == 2:
                                            attribute = "Email"
                                            running = False
                                        elif choice == 3:
                                            attribute = "Password"
                                            running = False
                                        elif choice == 4:
                                            attribute = "UserType"
                                            running = False
                                except ValueError:
                                    print(
                                        "Please enter number within menu options"
                                    )

                                    #get car attribute
                                if attribute is not None:
                                    print("Enter new %s data: " % attribute)
                                    editing = True
                                    valid = False
                                    while (editing):
                                        if (attribute == "UserType"):
                                            print(
                                                "Change to:\n1. Engineer\n2. Admin\n3. Manager\n4. Customer"
                                            )
                                            userType = int(input("> "))
                                            if (userType == 1):
                                                newData = "ENGINEER"
                                                valid = True
                                                break
                                            elif (userType == 2):
                                                newData = "ADMIN"
                                                valid = True
                                                break
                                            elif (userType == 3):
                                                newData = "MANAGER"
                                                valid = True
                                                break
                                            elif (userType == 4):
                                                newData = "CUSTOMER"
                                                valid = True
                                                break
                                        elif (attribute == "Password"):
                                            newData = input("New Password: "******"> ")
                                            valid = True
                                            break
                                    if valid:
                                        with DatabaseUtils() as db:
                                            if db.updateUser(
                                                    username, attribute,
                                                    newData):
                                                print("%s updated.\n" %
                                                      username)
                                            else:
                                                print(
                                                    "Error with update data, please try again."
                                                )
                                            choosing = False
                                    break

                        elif choice == 2:
                            print("Are you sure you wish to delete User %s?" %
                                  username)
                            print("enter y/n")
                            while True:
                                choice = input("> ")
                                if choice == "y":
                                    with DatabaseUtils() as db:
                                        db.deleteUser(username)
                                    print("%s deleted\n" % username)
                                    choosing = False
                                    break
                                elif choice == "n":
                                    choosing = False
                                    break

                            else:
                                print("incorrect value")

            else:
                print("User does not exist, create new? y/n")
                while True:
                    choice = input("> ")
                    if (choice == 'y'):
                        print("create user with name %s:" % username)
                        email = input("email: ")
                        password = input("password: "******"Create as:\n1. Engineer\n2. Admin\n3. Manager\n4. Customer"
                            )
                            userType = int(input("> "))
                            if (userType == 1):
                                userType = "ENGINEER"
                                valid = True
                                break
                            elif (userType == 2):
                                userType = "ADMIN"
                                valid = True
                                break
                            elif (userType == 3):
                                userType = "MANAGER"
                                valid = True
                                break
                            elif (userType == 4):
                                userType = "CUSTOMER"
                                valid = True
                                break

                        with DatabaseUtils() as db:
                            if db.insertUser(username, email, password,
                                             userType):
                                print("User added")
                            else:
                                print("error with user data")
                        break
                    elif (choice == 'n'):
                        break
        self.printMenu()
Esempio n. 16
0
    def register(self):
        """
            allows user to create a new account given username, email and password.
            Inserts data into database after validation
        """
        print(
            "(below choice only for testing. removed once admin accounts are accessible)"
        )
        print("1. Customer")
        print("2. Admin")
        print("3. Manager")
        print("4. Engineer\n")
        userType = ""
        while True:
            try:  #get user input
                choice = int(input("> "))
            except ValueError:
                print("Please enter number within user options")
            else:
                if choice > 4 or choice <= 0:
                    print("Please enter number within user options")
                else:
                    if choice == 1:
                        userType = "CUSTOMER"
                        break
                    elif choice == 2:
                        userType = "ADMIN"
                        break
                    elif choice == 3:
                        userType = "MANAGER"
                        break
                    elif choice == 4:
                        userType = "ENGINEER"
                        break

        newUser = input("Enter new username: "******"Enter your email address: ")
            regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'
            if re.search(regex, email):
                break
            else:
                print("Invalid email. Retry:")
        #Asks user to confirm the password entry to validate input
        valid = False
        while (valid == False):
            newPassword = getpass(prompt='Enter a password: '******'Confirm Password: ')
            #verify new password is correct
            if (newPassword == confirmPassword and len(newPassword) > 0):
                valid = True
            else:
                print("Invalid password, please try again")

        with DatabaseUtils() as db:
            userList = db.getUsers()
            for user in userList:
                if user[0] == newUser:
                    valid = False
                    print("Sorry, username already exists")
                    #check user doesnt already exist then insert if not
            if valid:
                db.insertUser(newUser, email, sha256_crypt.hash(newPassword),
                              userType)
                print("\nuser created\n")

        print("\nReturning to login..\n")
        self.printMenu()
Esempio n. 17
0
    def searchCars(self):
        """
            prompts user to search for specific car given attribute type:
            Rego,Make,BodyType,Seats, Colour,Cost
            returns with format:
            Rego     Model    Body    Seats    Colour     Price Per Day      Location     Returned (y/n)
        """
        print("Search By: ")
        print("1. Rego")
        print("2. Make")
        print("3. Body type")
        print("4. Seats")
        print("5. Colour")
        print("6. Cost Per Day")
        print("7. Back")
        #various search types from atrributes
        attribute = None
        running = True
        while (running):
            try:
                choice = int(input("> "))
            except ValueError:
                print("Please enter number within menu options")
            else:
                if choice > 6 or choice <= 0:
                    print("Please enter number within menu options")
                else:
                    if choice == 1:
                        attribute = "Rego"
                        running = False
                    elif choice == 2:
                        attribute = "Make"
                        running = False
                    elif choice == 3:
                        attribute = "Body"
                        running = False
                    elif choice == 4:
                        attribute = "Seats"
                        running = False
                    elif choice == 5:
                        attribute = "Colour"
                        running = False
                    elif choice == 6:
                        attribute = "CostPerDay"
                        running = False
                    elif choice == 7:
                        running = False
                        #get car attribute
        if attribute is not None:
            term = input("\nEnter " + attribute + ": ")
            with DatabaseUtils() as db:
                self.searchCars = db.searchCars(attribute, term)

            data = [
                "Rego", "Make", "Body", "Seats", "Colour", "Cost Per Day($)",
                "Location", "Returned"
            ]
            row_format = "{:<17}" * (len(data))
            print(row_format.format(*data))
            for car in self.searchCars:  #return formatted string as car table
                print(
                    row_format.format(car[0], car[1], car[2], str(car[3]),
                                      car[4], str(car[5]), str(car[6]),
                                      str(car[7])))
        else:
            print("No record of car")

        self.printMenu()