예제 #1
0
 def __init__(self, database, user):
     """
     :param database: Database Setting File location
     :type database: string
     :param user: User Object following the LMSLibraryDatabase.user_schema
     :type user: dict
     """
     self.db = LMSLibraryDatabase(database)
     self.display_text = "Return Book(s)"
     self.user = user
예제 #2
0
 def __init__(self, database, user):
     """
     :param database: Database Setting File location
     :type database: string
     """
     self.db = LMSLibraryDatabase(database)
     self.display_text = "Search Book by text"
     self.user = user
     self.database = database
예제 #3
0
 def test_update_or_add_user_1(self):
     # Test user add
     user = {
         "first_name": "John",
         "last_name": "Smith",
         "email": "*****@*****.**",
         "username": "******"
     }
     test_db = "lms_library_config_test.json"
     db = LMSLibraryDatabase(test_db)
     user_list = list()
     user_list.append(user["username"])
     user_list.append(user["first_name"])
     user_list.append(user["last_name"])
     user_list.append(user["email"])
     MasterPi.update_or_add_user(test_db, user)
     db_user = db.get_user(user["username"])
     self.assertTupleEqual(tuple(user_list), db_user[0])
예제 #4
0
    def __init__(self, database, cc):
        """
        Creates a handler object

        :param databse: Database setting file location
        :type database: str
        :param cc: Client Connection Object of the Reception Pi
        :type cc: obj
        """
        self.db = LMSLibraryDatabase(database)
        self.cc = cc
        self.display_text = "Search Book by voice"
예제 #5
0
 def __init__(self, database, user, cc):
     """
     :param database: Database Setting File location
     :type database: string
     :param user: User Object following the LMSLibraryDatabase.user_schema
     :type user: dict
     :param cc: Client Connection Object of the Reception Pi
     :type cc: obj
     """
     self.database = database
     self.db = LMSLibraryDatabase(database)
     self.display_text = "Return Book(s) using Barcode"
     self.user = user
     self.cc = cc
예제 #6
0
    def update_or_add_user(db_location, user):
        """
        Updates or addes the user to the Master Database

        :param user: User Dict to enter into Database
        :type users: dict that passes validate_user_dict
        :return: Record from the database
        :rtype: dict that conforms with user_schema
        """
        db = LMSLibraryDatabase(db_location)
        db_user = db.get_user(user["username"])
        if not db_user:
            db.add_user(user)
        else:
            db.update_user(user)
예제 #7
0
class ConsoleReturnBook(MenuHandler):
    """
    A class to handle the customer returning a book

    max_borrow_days : int
        Number of days to borrow a book
    db : LMSLibraryDatabase
        Database object of the master database
    display_text : str
        Display test for the console menu
    user : dict
        User Object following the LMSLibraryDatabase.user_schema
    """
    max_borrow_days = 7

    def __init__(self, database, user):
        """
        :param database: Database Setting File location
        :type database: string
        :param user: User Object following the LMSLibraryDatabase.user_schema
        :type user: dict
        """
        self.db = LMSLibraryDatabase(database)
        self.display_text = "Return Book(s)"
        self.user = user

    def invoke(self):
        """
        Function that is called to invoke the return book function.
        Gets the user input and validates if it is in a vaild book id format
        """
        print("\nEnter BookID(s) to Return.")
        print("You may enter ID's as comma seperated e.g. '1,2': ", end="")
        # get option from user, and strip whitespace
        str_input = input().strip()

        # check for blank input
        if not str_input:
            print("Invalid Input!")
            return
        # split string into list
        str_list = str_input.split(",")
        for str_id in str_list:
            self.start(str_id)

    def start(self, book_string):
        """
        This fuction is responsible for returning a book.
        It takes the book id given by the user and checks if it is borrowed by
        the user and then returns the book, deleting the calander invite

        :param book_string: Book ID of the book to return
        :type book_string: str
        :return: No return
        """
        # validate input
        if (not book_string.isdigit()):
            # input not a number
            print("{} is not a valid BookID".format(book_string))
            return
        # input is a number
        book_id = int(book_string)
        # check if book exists
        book_item = self.db.query_book_by_id(book_id)[0]
        if not book_item:
            print("Book with ID {} does not exist!".format(book_id))
            return
        book = dict()
        # convert to book dict
        for key, value in zip(LMSLibraryDatabase.book_schema, book_item):
            book[key] = value
        print("Returning {} by {}...".format(book["Title"], book["Author"]))
        # check if book is borrowed
        borrowed_record = self.is_borrowed(book, self.user["username"])
        if not borrowed_record:
            print("Book with ID {} is not borrowed by you!".format(book_id))
            return
        self.return_book(borrowed_record)

    def is_borrowed(self, book_borrowed, username):
        """
        Function to check if a book is borrowed or not

        :param book: Book details of book to check
        :type book: dict
        :param username: Username of who would like to return book
        :type username: str
        :return: If book is borrowed
        :rtype: bool
        """
        # makes call to db to get borrowed record
        borrowed = self.db.query_borrowed_book_by_user(book_borrowed["BookID"],
                                                       "borrowed", username)
        if not borrowed:
            return False
        else:
            book = dict()
            # create dict from record
            for key, value in zip(LMSLibraryDatabase.book_borrow_schema,
                                  borrowed[0]):
                book[key] = value
            return book

    def return_book(self, book_borrowed):
        """
        Function to return book

        :param book: Book details of book to check
        :type book: dict
        :return: No return
        """
        # set date return
        today = datetime.now()

        # generate event through google calander api
        GoogleCalanderAPI.delete_due_event(book_borrowed["EventID"])
        # insert db record for borrowed book
        self.db.update_borrowed_book(book_borrowed["BookBorrowedID"], today)
        print("Successfully Returned book: " +
              "{}, Reminder Deleted!".format(book_borrowed["BookID"]))
        # print if book is being returned after due date
        if today.date() > book_borrowed["DueDate"]:
            print("Book was due on {} and is returned Late!".format(
                book_borrowed["DueDate"]))
예제 #8
0
class ConsoleBorrowBook(MenuHandler):
    """
    A class to handle the customer borrowing a book

    max_borrow_days : int
        Number of days to borrow a book
    db : LMSLibraryDatabase
        Database object of the master database
    display_text : str
        Display test for the console menu
    user : dict
        User Object following the LMSLibraryDatabase.user_schema
    """
    max_borrow_days = 7

    def __init__(self, database, user):
        """
        :param database: Database Setting File location
        :type database: string
        :param user: User Object following the LMSLibraryDatabase.user_schema
        :type user: dict
        """
        self.db = LMSLibraryDatabase(database)
        self.display_text = "Borrow Book(s)"
        self.user = user

    def invoke(self):
        """
        Function to get user input for borrow book
        """
        print("\nEnter BookID(s) to borrow.")
        print("You may enter ID's as comma seperated e.g. '1,2': ", end="")
        # get option from user, and strip whitespace
        str_input = input().strip()

        # check for blank input
        if not str_input:
            print("Invalid Input!")
            return
        # split string into list
        str_list = str_input.split(",")
        for str_id in str_list:
            self.start(str_id)

    def start(self, book_string):
        """
        Fuction to borrow a book from the library

        :param book_string: Book ID of the book to borrow
        :type book_string: str
        :return: No return
        """
        # validate input
        if (not book_string.isdigit()):
            # input not a number
            print("{} is not a valid BookID".format(book_string))
            return
        # input is a number
        book_id = int(book_string)
        # check if book exists
        book_item = self.db.query_book_by_id(book_id)
        if not book_item:
            print("Book with ID {} does not exist!".format(book_id))
            return
        book = dict()
        # convert to book dict
        for key, value in zip(LMSLibraryDatabase.book_schema, book_item[0]):
            book[key] = value
        print("Borrowing {} by {}...".format(book["Title"], book["Author"]))
        # check if book is borrowed
        if self.is_borrowed(book):
            print("Cannot borrow book, {} is currently borrowed!".format(
                book_id
            ))
            return
        self.borrow_book(book)

    def is_borrowed(self, book):
        """
        Function to check if a book is borrowed or not

        :param book: Book details of book to check
        :type book: dict
        :return: If book is borrowed
        :rtype: bool
        """
        # makes call to db to get borrowed record
        borrowed = self.db.query_borrowed_book(book["BookID"], "borrowed")
        if not borrowed:
            return False
        else:
            return True

    def borrow_book(self, book):
        """
        Function to borrow book

        :param book: Book details of book to check
        :type book: dict
        :return: No return
        """
        # set date borrowed and due date
        today = datetime.now()
        due_date = today + timedelta(days=self.max_borrow_days)
        # generate event through google calander api
        event_id = GoogleCalanderAPI.create_due_event(
            due_date, book,
            self.user
        )
        # insert db record for borrowed book
        self.db.insert_borrowed_book(
            self.user["username"],
            book["BookID"],
            today,
            due_date,
            event_id
        )
        print(
            "Successfully borrowed book: " +
            "{}, Reminder to return sent to {}!".format(
                book["BookID"],
                self.user["email"]
            )
        )