Example #1
0
def validateRental(ghostBook, ghostClient):
    inBookList = False
    repoCaller = Repo()
    listOfBooks = repoCaller.getListOfBooks()
    listOfRentedBooks = repoCaller.getListOfRentals()
    listOfClients = repoCaller.getListOfClients()
    for iterator in listOfBooks:
        if Book.getBookId(iterator) == Book.getBookId(ghostBook):
            inBookList = True
            break
    if inBookList == False:
        raise BookAlreadyRentedOrDoesntExistError
    # bookIsRented=True
    for books in listOfRentedBooks:
        if Book.getBookId(ghostBook) == Rental.getBookId(
                books) and Rental.getReturnDate(books) == "notRented":
            for iterator in listOfRentedBooks:
                if Book.getBookId(iterator) == Book.getBookId(ghostBook):
                    raise BookAlreadyRentedOrDoesntExistError
    clientFound = False
    for iterator in listOfClients:
        if Client.getClientId(iterator) == Client.getClientId(ghostClient):
            clientFound = True
    if clientFound == False:
        raise ClientAlreadyExistsError
Example #2
0
def updateABook(commandInPieces):
    # updates a book from the repository
    # input - details about the book that must be updated(id) and the values that must be edited
    repoCaller = Repo()
    listOfBooks = repoCaller.getListOfBooks()
    idToUpdate = commandInPieces[1]
    newBookId = commandInPieces[2]
    newTitle = commandInPieces[3]
    newAuthor = commandInPieces[4]
    updatedBook = Book(newBookId, newTitle, newAuthor)
    position = 0
    try:
        validateABook(updatedBook)
        for iterator in listOfBooks:
            if Book.getBookId(iterator) == idToUpdate:
                if commandInPieces[0] != "undo":
                    undoFunction = Undo("updateB", [
                        "undo", newBookId,
                        Book.getBookId(iterator),
                        Book.getBookName(iterator),
                        Book.getBookAuthor(iterator), "undo", idToUpdate,
                        newBookId, newTitle, newAuthor
                    ])
                    repoCaller.addToListOfOperations(undoFunction)
                    repoCaller.setRedoFalse()
                repoCaller.removeBookFromPosition(position)
                repoCaller.insertBookIntoPosition(position, updatedBook)
                repoCaller.loadIntoFiles()
                repoCaller.loadIntoPickle()
                break
            position += 1
    except BookAlreadyExistsError:
        print(BookAlreadyExistsError())
    return False
Example #3
0
def removeABook(commandInPieces):
    # removes a book from the repository
    # input - details about the book to be removed (id)
    repoCaller = Repo()
    listOfBooks = repoCaller.getListOfBooks()
    idToRemove = commandInPieces[1]
    position = 0
    removed = False
    for iterator in listOfBooks:

        if Book.getBookId(iterator) == idToRemove:
            if commandInPieces[0] != "undo":
                undoFunction = Undo("addB", [
                    "undo",
                    Book.getBookId(iterator),
                    Book.getBookName(iterator),
                    Book.getBookAuthor(iterator)
                ])
                repoCaller.addToListOfOperations(undoFunction)
                repoCaller.setRedoFalse()
            repoCaller.removeBookFromPosition(position)
            repoCaller.loadIntoFiles()
            repoCaller.loadIntoPickle()
            removed = True
        position += 1
    if removed == False:
        print(IdOfBookDoesntExist())
    return False
Example #4
0
def validateABook(newBook):
    # checks if a book is already registred
    # input - instance of a book
    bookCaller = Book
    repoCaller = Repo()
    listOfBooks = repoCaller.getListOfBooks()
    for iterator in listOfBooks:
        if bookCaller.getBookId(iterator) == bookCaller.getBookId(newBook):
            raise BookAlreadyExistsError()
Example #5
0
def listAllBooks(commandInPieces):
    # lists books in repository
    # input - not important/ not used
    repoCaller = Repo()
    listOfBooks = repoCaller.getListOfBooks()
    s = ""
    s += "Books: \n"
    for iterator in listOfBooks:
        s += Book.bookToString(iterator) + "\n"
    return s
Example #6
0
def searchABook(commandInPieces):
    repoCaller = Repo()
    keyToSearch = commandInPieces[1]
    listOfBooks = repoCaller.getListOfBooks()
    s = ""
    s = "Books: \n"
    for book in listOfBooks:
        if keyToSearch.lower() in Book.getBookId(book).lower():
            s += Book.bookToString(book) + "\n"
        elif keyToSearch.lower() in Book.getBookAuthor(book).lower():
            s += Book.bookToString(book) + "\n"
        elif keyToSearch.lower() in Book.getBookName(book).lower():
            s += Book.bookToString(book) + "\n"
    return s
Example #7
0
def showStatistics(null):
    repoCaller = Repo()
    listOfRentedBooks = repoCaller.getListOfRentals()
    mostRentedBooks = repoCaller.getListOfMostRentedBooks()
    mostRentedAuthors = repoCaller.getListOfMostRentedAuthors()
    mostActiveClients = repoCaller.getListOfMostActiveClients()
    listOfBooks = repoCaller.getListOfBooks()
    listOfClients = repoCaller.getListOfClients()
    booksStatistics = "Books statistic:\n"
    authorStatistics = "Author statistics:\n"
    clientsStatistics = "Clients statistics:\n"
    for rentedBooks in listOfRentedBooks:
        try:
            mostRentedAuthors[Rental.getBookId(rentedBooks)] += 1
        except KeyError:
            mostRentedAuthors[Rental.getBookId(rentedBooks)] = 1

    for rentedBooks in listOfRentedBooks:
        try:
            mostRentedBooks[Rental.getBookId(rentedBooks)] += 1
        except KeyError:
            mostRentedBooks[Rental.getBookId(rentedBooks)] = 1

    mostRentedAuthorsList = sorted(mostRentedAuthors.items(),
                                   key=lambda x: x[1],
                                   reverse=True)
    mostRentedBooksList = sorted(mostRentedBooks.items(),
                                 key=lambda x: x[1],
                                 reverse=True)

    for rentedBooks in mostRentedBooksList:
        booksStatistics += "Id of book: " + str(
            rentedBooks[0]) + " Rented: " + str(
                rentedBooks[1]) + " times" + "\n"

    for rentedBooks in mostRentedAuthorsList:
        for books in listOfBooks:
            if (Book.getBookId(books) == rentedBooks[0]):
                authorStatistics += "Author: " + str(
                    Book.getBookAuthor(books)) + " Rented: " + str(
                        rentedBooks[1]) + " times" + "\n"
    for client in listOfClients:
        total = 0
        for rent in listOfRentedBooks:
            if Client.getClientId(client) == Rental.getClientId(
                    rent) and rent.getReturnDate() != "notReturned":
                difference = Rental.getReturnDate(rent) - Rental.getRentDate(
                    rent)
                total += difference.total_seconds() / 86400
        try:
            mostActiveClients[Client.getClientName(client)] += total
        except KeyError:
            repoCaller.addToMostActiveClients(client, total)
    mostActiveClientsList = sorted(mostActiveClients.items(),
                                   key=lambda x: x[1],
                                   reverse=True)

    for client in mostActiveClientsList:
        clientsStatistics += "Name: " + str(
            client[0]) + " Total rental days: " + str(client[1]) + "\n"
    return [booksStatistics, authorStatistics, clientsStatistics]