From a7c3ed90e084d08fa39bd7dcaf07357d1456f3e4 Mon Sep 17 00:00:00 2001 From: Aniket Salvi Date: Thu, 23 Nov 2023 09:35:39 +0100 Subject: [PATCH 01/12] Updated Author class --- library/model/author.py | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/library/model/author.py b/library/model/author.py index ffc1a51..f20dd7f 100644 --- a/library/model/author.py +++ b/library/model/author.py @@ -1,23 +1,24 @@ class Author: - firstname: str - lastname: str + _firstname: str + _lastname: str def __init__(self, firstname: str, lastname: str): - self.firstname = firstname - self.lastname = lastname + self._firstname = firstname + self._lastname = lastname + self._uid=f"{self._firstname} {self._lastname}" def get_firstname(self) -> str: - return self.firstname + return self._firstname def get_lastname(self) -> str: - return self.lastname + return self._lastname def get_fullname(self) -> str: - return f"{self.firstname} {self.lastname}" + return self._uid def __eq__(self, other): """Overrides the default implementation""" if isinstance(other, Author): - return self.get_fullname() == other.get_fullname() + return self._uid == other._uid return NotImplemented From 9502249b79d167762d862c69e97463de8e4af58c Mon Sep 17 00:00:00 2001 From: Aniket Salvi Date: Thu, 23 Nov 2023 09:41:11 +0100 Subject: [PATCH 02/12] Updated publisher clas --- library/model/publisher.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/library/model/publisher.py b/library/model/publisher.py index dc9a4c5..3c75f73 100644 --- a/library/model/publisher.py +++ b/library/model/publisher.py @@ -1,11 +1,11 @@ class Publisher: - name: str + _name: str def __init__(self, name): - self.name = name + self._name = name def __eq__(self, other): """Overrides the default implementation""" if isinstance(other, Publisher): - return self.name == other.name + return self._name == other._name return NotImplemented From 99cda733d90ff6e56a424d1bf208ff9c90276697 Mon Sep 17 00:00:00 2001 From: denishoornaert Date: Thu, 23 Nov 2023 10:09:22 +0100 Subject: [PATCH 03/12] library: Book: refactor serializer and introduce BookGenre --- library/model/BookGenre.py | 7 +++++ library/model/book.py | 64 ++++++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 27 deletions(-) create mode 100644 library/model/BookGenre.py diff --git a/library/model/BookGenre.py b/library/model/BookGenre.py new file mode 100644 index 0000000..d2fe3ae --- /dev/null +++ b/library/model/BookGenre.py @@ -0,0 +1,7 @@ +from enum import Enum + +class BookType(): + PAPER = "Paper" + ELECTRONIC = "Electronic" + AUDIO = "Audio" + diff --git a/library/model/book.py b/library/model/book.py index abf5f8c..c7d3d36 100644 --- a/library/model/book.py +++ b/library/model/book.py @@ -3,24 +3,24 @@ from library.model.author import Author from library.model.genre import Genre from library.model.publisher import Publisher +from library.model.booktype import BookType import xml.etree.ElementTree as et from library.persistence.storage import LibraryRepository - class Book: title: str authors: list[Author] publisher: Publisher publication_date: datetime - genres: list[Genre] + genres: set[Genre] pages: int isbn: str existing_items: int borrowed_items: int - _book_type: str + _book_type: BookType duration: int = 0 def __init__(self, title, authors, publisher, pub_date, genres, pages, isbn, type, duration=0, existing_items=1, borrowed_items=0): @@ -54,11 +54,11 @@ def from_borrowed_book(cls, borrowed_book: "BorrowedBook") -> "Book": return book def can_borrow(self) -> bool: - if self._book_type == "Paper": + if self._book_type == BookType.PAPER: return self.existing_items - self.borrowed_items > 0 - elif self._book_type == "Electronic": + elif self._book_type == BookType.ELECTRONIC: return True - elif self._book_type == "Audio": + elif self._book_type == BookType.AUDIO: return True else: raise AttributeError("No such book type...") @@ -145,26 +145,36 @@ def __eq__(self, other): class BookSerializer: - def serialize(self, book: Book, format: str): - if format == "JSON": - book_info = { - "id": book.isbn, - "title": book.title, - "authors": [author.get_fullname() for author in book.authors], - "available_items": book.existing_items - book.borrowed_items, - "borrowed_items": book.borrowed_items, - } - return json.dumps(book_info) - elif format == "XML": - book_info = et.Element("book", attrib={"id": book.isbn}) - title = et.SubElement(book_info, "title") - title.text = book.title - authors = et.SubElement(book_info, "authors") - authors.text = ", ".join([author.get_fullname() for author in book.authors]) - avail = et.SubElement(book_info, "available") - avail.text = str(book.existing_items - book.borrowed_items) - authors = et.SubElement(book_info, "borrowed") - authors.text = str(book.borrowed_items) - return et.tostring(book_info, encoding="Unicode") + + @staticmethod + def _serialize_to_json(book: Book): + book_info = { + "id": book.isbn, + "title": book.title, + "authors": [author.get_fullname() for author in book.authors], + "available_items": book.existing_items - book.borrowed_items, + "borrowed_items": book.borrowed_items, + } + return json.dumps(book_info) + + @staticmethod + def _serialize_to_xml(book: Book): + book_info = et.Element("book", attrib={"id": book.isbn}) + title = et.SubElement(book_info, "title") + title.text = book.title + authors = et.SubElement(book_info, "authors") + authors.text = ", ".join([author.get_fullname() for author in book.authors]) + avail = et.SubElement(book_info, "available") + avail.text = str(book.existing_items - book.borrowed_items) + authors = et.SubElement(book_info, "borrowed") + authors.text = str(book.borrowed_items) + return et.tostring(book_info, encoding="Unicode") + + @staticmethod + def serialize(book: Book, format: str): + if (format == "JSON"): + return BookSerializer._serialize_to_json(book) + elif (format == "XML"): + return BookSerializer._serialize_to_xml(book) else: raise ValueError(format) From 201162f40736c75dc81d6f7c539aa34bc4213a04 Mon Sep 17 00:00:00 2001 From: denishoornaert Date: Thu, 23 Nov 2023 10:10:35 +0100 Subject: [PATCH 04/12] library: book: book type --- library/model/BookType.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 library/model/BookType.py diff --git a/library/model/BookType.py b/library/model/BookType.py new file mode 100644 index 0000000..d2fe3ae --- /dev/null +++ b/library/model/BookType.py @@ -0,0 +1,7 @@ +from enum import Enum + +class BookType(): + PAPER = "Paper" + ELECTRONIC = "Electronic" + AUDIO = "Audio" + From c55b10824f96d043c67e35b2cb4f0e4c8bfc88ac Mon Sep 17 00:00:00 2001 From: denishoornaert Date: Thu, 23 Nov 2023 10:11:48 +0100 Subject: [PATCH 05/12] removed --- library/model/BookGenre.py | 7 ------- 1 file changed, 7 deletions(-) delete mode 100644 library/model/BookGenre.py diff --git a/library/model/BookGenre.py b/library/model/BookGenre.py deleted file mode 100644 index d2fe3ae..0000000 --- a/library/model/BookGenre.py +++ /dev/null @@ -1,7 +0,0 @@ -from enum import Enum - -class BookType(): - PAPER = "Paper" - ELECTRONIC = "Electronic" - AUDIO = "Audio" - From dc83cd7bf90facf3c01ca7cf669f4113bdab0522 Mon Sep 17 00:00:00 2001 From: Aniket Salvi Date: Thu, 23 Nov 2023 10:26:32 +0100 Subject: [PATCH 06/12] Updated Booktype fee calculation and otehr things using static class BookTypeStatic defined somewherelse. --- library/model/BookType.py | 2 +- library/model/book.py | 43 ++++++++++++++++++++------------------- 2 files changed, 23 insertions(+), 22 deletions(-) diff --git a/library/model/BookType.py b/library/model/BookType.py index d2fe3ae..a5f6c12 100644 --- a/library/model/BookType.py +++ b/library/model/BookType.py @@ -1,6 +1,6 @@ from enum import Enum -class BookType(): +class BookType(Enum): PAPER = "Paper" ELECTRONIC = "Electronic" AUDIO = "Audio" diff --git a/library/model/book.py b/library/model/book.py index c7d3d36..442527d 100644 --- a/library/model/book.py +++ b/library/model/book.py @@ -54,33 +54,34 @@ def from_borrowed_book(cls, borrowed_book: "BorrowedBook") -> "Book": return book def can_borrow(self) -> bool: - if self._book_type == BookType.PAPER: - return self.existing_items - self.borrowed_items > 0 - elif self._book_type == BookType.ELECTRONIC: - return True - elif self._book_type == BookType.AUDIO: - return True - else: + # if self._book_type == BookType.PAPER: + # return self.existing_items - self.borrowed_items > 0 + # elif self._book_type == BookType.ELECTRONIC: + # return True + # elif self._book_type == BookType.AUDIO: + # return True + # else: + try: + return BookTypeStatic.can_borrow(self) + except: raise AttributeError("No such book type...") def get_approximate_duration(self) -> int: - if self._book_type == "Paper": - return self.pages * 3 * 60 - elif self._book_type == "Electronic": - return self.pages * 5 * 60 - elif self._book_type == "Audio": - return self.duration - else: + # if self._book_type == "Paper": + # return self.pages * 3 * 60 + # elif self._book_type == "Electronic": + # return self.pages * 5 * 60 + # elif self._book_type == "Audio": + # return self.duration + try: + return BookTypeStatic.get_approximate_duration(self) + except: raise AttributeError("No such book type...") def get_weekly_fee(self) -> int: - if self._book_type == "Paper": - return 5 - elif self._book_type == "Electronic": - return 2 - elif self._book_type == "Audio": - return 2 - else: + try: + return BookTypeStatic.get_weekly_fee(self) + except: raise AttributeError("No such book type...") def borrow_book(self) -> "BorrowedBook": From 673ad8e554cfa29f394df4468a454fb2b51fb821 Mon Sep 17 00:00:00 2001 From: Aniket Salvi Date: Thu, 23 Nov 2023 10:30:11 +0100 Subject: [PATCH 07/12] Updated use of booktype enum --- library/model/book.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/library/model/book.py b/library/model/book.py index 442527d..320f5bf 100644 --- a/library/model/book.py +++ b/library/model/book.py @@ -132,7 +132,7 @@ def renew_rental(self) -> "BorrowedBook": return self def return_book(self) -> Book: - if self._book_type == "Paper": + if self._book_type == BookType.PAPER: self.borrowed_items -= 1 book = Book.from_borrowed_book(self) LibraryRepository.update_book(book) From ed6554ca86022cb27da6f2b9263d785285f081f3 Mon Sep 17 00:00:00 2001 From: denishoornaert Date: Thu, 23 Nov 2023 10:31:12 +0100 Subject: [PATCH 08/12] removed --- library/model/BookType.py | 7 ------- library/model/book.py | 2 ++ library/model/booktype.py | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 7 deletions(-) delete mode 100644 library/model/BookType.py create mode 100644 library/model/booktype.py diff --git a/library/model/BookType.py b/library/model/BookType.py deleted file mode 100644 index a5f6c12..0000000 --- a/library/model/BookType.py +++ /dev/null @@ -1,7 +0,0 @@ -from enum import Enum - -class BookType(Enum): - PAPER = "Paper" - ELECTRONIC = "Electronic" - AUDIO = "Audio" - diff --git a/library/model/book.py b/library/model/book.py index 320f5bf..3a8b99d 100644 --- a/library/model/book.py +++ b/library/model/book.py @@ -16,6 +16,8 @@ class Book: genres: set[Genre] pages: int isbn: str + fee: float + duration: float existing_items: int borrowed_items: int diff --git a/library/model/booktype.py b/library/model/booktype.py new file mode 100644 index 0000000..4e4a455 --- /dev/null +++ b/library/model/booktype.py @@ -0,0 +1,44 @@ +from enum import Enum + +seconds_in_a_minute = 60 + +class BookType(Enum): + PAPER = "Paper" + ELECTRONIC = "Electronic" + AUDIO = "Audio" + +class BookTypeStatic(): + + @staticmethod + def can_borrow(book): + if (book._book_type == BookType.PAPER): + return book.existing_items - book.borrowed_item > 0 + elif (book._book_type == BookType.ELECTRONIC): + return True + elif (book._book_type == BookType.AUDIO): + return True + else: + raise AttributeError("No such a book...") + + @staticmethod + def get_approximate_duration(book): + if (book._book_type == BookType.PAPER): + return book._pages * 3 * seconds_in_a_minute + elif (book._book_type == BookType.ELECTRONIC): + return book._pages * 5 * seconds_in_a_minute + elif (book._book_type == BookType.AUDIO): + return book._duration + else: + raise AttributeError("No such a book...") + + @staticmethod + def get_weekly_fee(book): + if (book._book_type == BookType.PAPER): + return 5 + elif (book._book_type == BookType.ELECTRONIC): + return 2 + elif (book._book_type == BookType.AUDIO): + return 2 + else: + raise AttributeError("No such a book...") + From d336ba12916db960fde6ca6aab1f6de2f1578b67 Mon Sep 17 00:00:00 2001 From: Aniket Salvi Date: Thu, 23 Nov 2023 10:34:58 +0100 Subject: [PATCH 09/12] Updated imports --- library/model/book.py | 25 +++++-------------------- library/model/booktype.py | 2 +- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/library/model/book.py b/library/model/book.py index 3a8b99d..c0284b4 100644 --- a/library/model/book.py +++ b/library/model/book.py @@ -3,7 +3,8 @@ from library.model.author import Author from library.model.genre import Genre from library.model.publisher import Publisher -from library.model.booktype import BookType +from library.model.booktype import BookType, BookTypeProcessor + import xml.etree.ElementTree as et from library.persistence.storage import LibraryRepository @@ -16,9 +17,6 @@ class Book: genres: set[Genre] pages: int isbn: str - fee: float - duration: float - existing_items: int borrowed_items: int @@ -56,33 +54,20 @@ def from_borrowed_book(cls, borrowed_book: "BorrowedBook") -> "Book": return book def can_borrow(self) -> bool: - # if self._book_type == BookType.PAPER: - # return self.existing_items - self.borrowed_items > 0 - # elif self._book_type == BookType.ELECTRONIC: - # return True - # elif self._book_type == BookType.AUDIO: - # return True - # else: try: - return BookTypeStatic.can_borrow(self) + return BookTypeProcessor.can_borrow(self) except: raise AttributeError("No such book type...") def get_approximate_duration(self) -> int: - # if self._book_type == "Paper": - # return self.pages * 3 * 60 - # elif self._book_type == "Electronic": - # return self.pages * 5 * 60 - # elif self._book_type == "Audio": - # return self.duration try: - return BookTypeStatic.get_approximate_duration(self) + return BookTypeProcessor.get_approximate_duration(self) except: raise AttributeError("No such book type...") def get_weekly_fee(self) -> int: try: - return BookTypeStatic.get_weekly_fee(self) + return BookTypeProcessor.get_weekly_fee(self) except: raise AttributeError("No such book type...") diff --git a/library/model/booktype.py b/library/model/booktype.py index 4e4a455..4578bb9 100644 --- a/library/model/booktype.py +++ b/library/model/booktype.py @@ -7,7 +7,7 @@ class BookType(Enum): ELECTRONIC = "Electronic" AUDIO = "Audio" -class BookTypeStatic(): +class BookTypeProcessor(): @staticmethod def can_borrow(book): From dd1e91781bdc0bbcb122efaf297875ce7c735dad Mon Sep 17 00:00:00 2001 From: Aniket Salvi Date: Thu, 23 Nov 2023 10:42:11 +0100 Subject: [PATCH 10/12] minor updates --- library/model/book.py | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/library/model/book.py b/library/model/book.py index c0284b4..6f9717e 100644 --- a/library/model/book.py +++ b/library/model/book.py @@ -54,26 +54,19 @@ def from_borrowed_book(cls, borrowed_book: "BorrowedBook") -> "Book": return book def can_borrow(self) -> bool: - try: return BookTypeProcessor.can_borrow(self) - except: - raise AttributeError("No such book type...") + def get_approximate_duration(self) -> int: - try: return BookTypeProcessor.get_approximate_duration(self) - except: - raise AttributeError("No such book type...") + def get_weekly_fee(self) -> int: - try: - return BookTypeProcessor.get_weekly_fee(self) - except: - raise AttributeError("No such book type...") - + return BookTypeProcessor.get_weekly_fee(self) + def borrow_book(self) -> "BorrowedBook": if self.can_borrow(): - if self._book_type == "Paper": + if self._book_type == BookType.PAPER: self.borrowed_items += 1 LibraryRepository.update_book(self) borrowed_book = BorrowedBook.from_book(self) From 8df1a65c7b0836c748db9ed1421578bb5b027987 Mon Sep 17 00:00:00 2001 From: Aniket Salvi Date: Thu, 23 Nov 2023 10:47:41 +0100 Subject: [PATCH 11/12] Updated references to booktype Paper --- library/model/book.py | 2 +- library/model/booktype.py | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/library/model/book.py b/library/model/book.py index 6f9717e..7ce065e 100644 --- a/library/model/book.py +++ b/library/model/book.py @@ -66,7 +66,7 @@ def get_weekly_fee(self) -> int: def borrow_book(self) -> "BorrowedBook": if self.can_borrow(): - if self._book_type == BookType.PAPER: + if BookTypeProcessor.book_check_reqrd(self): self.borrowed_items += 1 LibraryRepository.update_book(self) borrowed_book = BorrowedBook.from_book(self) diff --git a/library/model/booktype.py b/library/model/booktype.py index 4578bb9..74395bf 100644 --- a/library/model/booktype.py +++ b/library/model/booktype.py @@ -1,5 +1,5 @@ from enum import Enum - +from model.book import Book seconds_in_a_minute = 60 class BookType(Enum): @@ -9,6 +9,13 @@ class BookType(Enum): class BookTypeProcessor(): + @staticmethod + def book_check_reqrd(book:Book): + if book._book_type == BookType.PAPER: + return True + else: + return False + @staticmethod def can_borrow(book): if (book._book_type == BookType.PAPER): From 63c3789e3e151e41f04f30a0c62fad5126c2e0bd Mon Sep 17 00:00:00 2001 From: denishoornaert Date: Thu, 23 Nov 2023 10:50:46 +0100 Subject: [PATCH 12/12] library: user: change summary output --- library/model/user.py | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/library/model/user.py b/library/model/user.py index 5300290..dad31b7 100644 --- a/library/model/user.py +++ b/library/model/user.py @@ -85,13 +85,19 @@ def __eq__(self, other): return NotImplemented def __str__(self): + # formatted tempalte + output_template = "{}, {}, ({}): {}{}/{}\n____BORROWED BOOKS____\n{}\n____READ BOOKS____\n{}\n\nOpen invoices: {}" borrowed_books = "\n".join(str(book) for book in self.borrowed_books) read_books = "\n".join(str(book) for book in self.read_books) - return f"""{self.firstname}, {self.lastname} ({self.email}): {self.country_calling_code}{self.area_code}/{self.landline_number} - _______BORROWED BOOKS________ - {borrowed_books} - _______READ BOOKS________ - {read_books} - - Open invoices: {[x.id for x in self.invoices]} - """ + summary = output_template.format( + self.firstname, + self.lastname, + self.email, + self.country_calling_code, + self.area_code, + self.landline_number, + borrowed_books, + read_books, + [invoice.id, for invoice in self.invoices] + ) + return summary