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
def updateAClient(commandInPieces): # updates a client that is already in the repository # input - details about the client to be updated (id) and the values that must be edited repoCaller = Repo() listOfClients = repoCaller.getListOfClients() idToUpdate = commandInPieces[1] newId = commandInPieces[2] newName = commandInPieces[3] updatedClient = Client(newId, newName) position = 0 try: validateAClient(updatedClient) for iterator in listOfClients: if Client.getClientId(iterator) == idToUpdate: if commandInPieces[0] != "undo": undoFunction = Undo("updateC", [ "undo", newId, Client.getClientId(iterator), Client.getClientName(iterator), "undo", Client.getClientId(iterator), newId, newName ]) repoCaller.addToListOfOperations(undoFunction) repoCaller.setRedoFalse() repoCaller.removeClientFromPosition(position) repoCaller.insertClientIntoPosition(position, updatedClient) repoCaller.loadIntoFiles() repoCaller.loadIntoPickle() break position += 1 except ClientAlreadyExistsError: print(ClientAlreadyExistsError()) return False
def validateAClient(newClient): # checks if a client is already registred # input - instance of a client clientCaller = Client repoCaller = Repo() listOfClients = repoCaller.getListOfClients() for iterator in listOfClients: if clientCaller.getClientId(iterator) == clientCaller.getClientId( newClient): raise ClientAlreadyExistsError
def listAllClients(commandInPieces): # lists clients in repository # input - not important / not used repoCaller = Repo() listOfClients = repoCaller.getListOfClients() s = "" s += "Clients: \n" for iterator in listOfClients: s += Client.clientToString(iterator) + "\n" return s
def searchAClient(commandInPieces): repoCaller = Repo() keyToSearch = commandInPieces[1] listOfClients = repoCaller.getListOfClients() s = "" s += "Clients: \n" for client in listOfClients: if keyToSearch.lower() == Client.getClientId(client).lower(): s += Client.clientToString(client) + "\n" elif keyToSearch.lower() == Client.getClientName(client).lower(): s += Client.clientToString(client) + "\n" return s
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]