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 listRentedBooks(commandInPieces):
    repoCaller = Repo()
    listOfRentedBooks = repoCaller.getListOfRentals()
    s = "Rented: \n"
    for iterator in listOfRentedBooks:
        s += Rental.printRental(iterator)
    return s
Example #3
0
def returnABook(commandInPieces):
    repoCaller = Repo()
    idBook = commandInPieces[1]
    idClient = commandInPieces[2]
    returnDateString = commandInPieces[3].split('/')
    returnDate = datetime.datetime(int(returnDateString[0]),
                                   int(returnDateString[1]),
                                   int(returnDateString[2]))
    listOfRentedBooks = repoCaller.getListOfRentals()
    for iterator in listOfRentedBooks:
        if Rental.getBookId(iterator) == idBook and Rental.getClientId(
                iterator) == idClient:
            Rental.setReturnDate(iterator, returnDate)
            repoCaller.loadIntoFiles()
            repoCaller.loadIntoPickle()
    return False
Example #4
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]