From 0807fe4357c65b027f309217dfb7fa31f0df1e03 Mon Sep 17 00:00:00 2001 From: bobrador Date: Thu, 16 Jan 2025 09:46:35 +0100 Subject: [PATCH] [FIX] bookstore_mgmt_google_books_api: Fixed book importation for product.product Refactored to use a mixin for Google Books API integration. This mixin can now be inherited by both product.template and product.product, enabling proper functionality for both models. --- bookstore_mgmt/README.rst | 2 +- bookstore_mgmt_google_books_api/README.rst | 7 +- .../models/__init__.py | 1 + .../models/google_books_mixin.py | 120 ++++++++++++++++++ .../models/product_product.py | 77 +---------- .../models/product_template.py | 120 +----------------- .../readme/CONTRIBUTORS.md | 3 +- 7 files changed, 134 insertions(+), 196 deletions(-) create mode 100644 bookstore_mgmt_google_books_api/models/google_books_mixin.py diff --git a/bookstore_mgmt/README.rst b/bookstore_mgmt/README.rst index e1c59bb..323b6d7 100644 --- a/bookstore_mgmt/README.rst +++ b/bookstore_mgmt/README.rst @@ -88,4 +88,4 @@ Current `maintainers `__: This module is part of the `OCA/vertical-edition `_ project on GitHub. -You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. \ No newline at end of file diff --git a/bookstore_mgmt_google_books_api/README.rst b/bookstore_mgmt_google_books_api/README.rst index 0a66cf1..f618612 100644 --- a/bookstore_mgmt_google_books_api/README.rst +++ b/bookstore_mgmt_google_books_api/README.rst @@ -69,10 +69,11 @@ Authors Contributors ------------ -- [APSL-Nagarro](https://apsl.tech): +- [APSL-Nagarro](https://apsl.tech): - - Miquel Alzanillas - - Antoni Marroig + - Miquel Alzanillas + - Antoni Marroig + - Bernat Obrador Maintainers ----------- diff --git a/bookstore_mgmt_google_books_api/models/__init__.py b/bookstore_mgmt_google_books_api/models/__init__.py index 5d72f9b..e7dbcf6 100644 --- a/bookstore_mgmt_google_books_api/models/__init__.py +++ b/bookstore_mgmt_google_books_api/models/__init__.py @@ -1,4 +1,5 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +from . import google_books_mixin from . import product_product from . import product_template diff --git a/bookstore_mgmt_google_books_api/models/google_books_mixin.py b/bookstore_mgmt_google_books_api/models/google_books_mixin.py new file mode 100644 index 0000000..0681cb4 --- /dev/null +++ b/bookstore_mgmt_google_books_api/models/google_books_mixin.py @@ -0,0 +1,120 @@ +import base64 +from datetime import datetime + +import requests +import unidecode +from google_books_api_wrapper.api import GoogleBooksAPI + +from odoo import _, models +from odoo.exceptions import UserError + + +class GoogleBooksMixin(models.AbstractModel): + _name = "google.books.mixin" + + def get_convert_to_base64(self, url): + return base64.b64encode(requests.get(url, timeout=5).content) + + def get_editorial_id(self, editorial_name): + editorial_id = self.env["product.book.editorial"].search( + [("name", "ilike", unidecode.unidecode(editorial_name))] + ) + if not editorial_id: + editorial_id = self.env["product.book.editorial"].create( + {"name": editorial_name} + ) + if len(editorial_id) > 1: + editorial = editorial_id.filtered(lambda x: x.name == editorial_name) + return editorial if editorial else editorial_id[0] + return editorial_id + + def get_genre_id(self, genres): + for genre_name in genres: + genre_id = self.env["product.book.genre"].search( + [("name", "ilike", unidecode.unidecode(genre_name))] + ) + if not genre_id: + genre_id = self.env["product.book.genre"].create({"name": genre_name}) + if len(genre_id) > 1: + genre = genre_id.filtered( + lambda x, genre_name=genre_name: x.name == genre_name + ) + return genre if genre else genre_id[0] + return genre_id + + def get_author_id(self, author_name): + author_id = self.env["product.book.author"].search( + [("name", "ilike", unidecode.unidecode(author_name))] + ) + if not author_id: + author_id = self.env["product.book.author"].create({"name": author_name}) + if len(author_id) > 1: + author = author_id.filtered(lambda x: x.name == author_name) + return author if author else author_id[0] + return author_id + + def action_import_from_isbn(self): + for record in self.filtered(lambda x: x.is_book): + if record.barcode: + client = GoogleBooksAPI() + isbn = record.barcode.replace("-", "") + book = client.get_book_by_isbn13(isbn) + if not book: + book = client.get_book_by_isbn10(isbn) + if book: + # Set data to be updated + data = { + "name": book.title, + } + + if book.published_date: + # Convert to year format + try: + published_year = datetime.strptime( + book.published_date, "%Y-%m-%d" + ).year + except Exception: + published_year = int(book.published_date[:4]) + + data["year_edition"] = published_year + + if book.authors: + data["author_id"] = record.get_author_id(book.authors[0]) + + if book.publisher: + data["editorial_id"] = record.get_editorial_id(book.publisher) + + if book.subjects: + data["genre_id"] = record.get_genre_id(book.subjects) + + if book.description: + data["description"] = book.description + data["description_sale"] = book.description + + if book.large_thumbnail: + data["image_1920"] = record.get_convert_to_base64( + book.large_thumbnail + ) + + # Update book data in Odoo + record.write(data) + + # Show success notification + self.env.user.notify_success( + message=_("Book data updated from Google Books API") + ) + + else: + # Return not found notification + return { + "type": "ir.actions.client", + "tag": "display_notification", + "params": { + "title": _("Warning"), + "type": "warning", + "message": _("Not book found with this data"), + "sticky": True, + }, + } + else: + raise UserError(_("ISBN code is mandatory. Please, provide one.")) diff --git a/bookstore_mgmt_google_books_api/models/product_product.py b/bookstore_mgmt_google_books_api/models/product_product.py index b6cf5c3..e1ba608 100644 --- a/bookstore_mgmt_google_books_api/models/product_product.py +++ b/bookstore_mgmt_google_books_api/models/product_product.py @@ -1,79 +1,8 @@ # Copyright 2024 (APSL-Nagarro) - Miquel Alzanillas, Antoni Marroig # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). - -from datetime import datetime - -from google_books_api_wrapper.api import GoogleBooksAPI - -from odoo import _, models -from odoo.exceptions import UserError +from odoo import models class Product(models.Model): - _inherit = "product.product" - - def action_import_from_isbn(self): - for record in self.filtered(lambda x: x.is_book): - if record.barcode: - client = GoogleBooksAPI() - isbn = record.barcode.replace("-", "") - book = client.get_book_by_isbn13(isbn) - if not book: - book = client.get_book_by_isbn10(isbn) - if book: - # Set data to be updated - data = { - "name": book.title, - } - - if book.published_date: - # Convert to year format - try: - published_year = datetime.strptime( - book.published_date, "%Y-%m-%d" - ).year - except Exception: - published_year = int(book.published_date[:4]) - - data["year_edition"] = published_year - - if book.authors: - data["author_id"] = record.get_author_id(book.authors[0]) - - if book.publisher: - data["editorial_id"] = record.get_editorial_id(book.publisher) - - if book.subjects: - data["genre_id"] = record.get_genre_id(book.subjects) - - if book.description: - data["description"] = book.description - data["description_sale"] = book.description - - if book.large_thumbnail: - data["image_1920"] = record.get_convert_to_base64( - book.large_thumbnail - ) - - # Update book data in Odoo - record.write(data) - - # Show success notification - self.env.user.notify_success( - message=_("Book data updated from Google Books API") - ) - - else: - # Return not found notification - return { - "type": "ir.actions.client", - "tag": "display_notification", - "params": { - "title": _("Warning"), - "type": "warning", - "message": _("Not book found with this data"), - "sticky": True, - }, - } - else: - raise UserError(_("ISBN code is mandatory. Please, provide one.")) + _name = "product.product" + _inherit = ["product.product", "google.books.mixin"] diff --git a/bookstore_mgmt_google_books_api/models/product_template.py b/bookstore_mgmt_google_books_api/models/product_template.py index 223f134..d6afc16 100644 --- a/bookstore_mgmt_google_books_api/models/product_template.py +++ b/bookstore_mgmt_google_books_api/models/product_template.py @@ -1,123 +1,9 @@ # Copyright 2024 (APSL-Nagarro) - Miquel Alzanillas, Antoni Marroig # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -import base64 -from datetime import datetime - -import requests -import unidecode -from google_books_api_wrapper.api import GoogleBooksAPI - -from odoo import _, models -from odoo.exceptions import UserError +from odoo import models class ProductTemplate(models.Model): - _inherit = "product.template" - - def get_convert_to_base64(self, url): - return base64.b64encode(requests.get(url, timeout=5).content) - - def get_editorial_id(self, editorial_name): - editorial_id = self.env["product.book.editorial"].search( - [("name", "ilike", unidecode.unidecode(editorial_name))] - ) - if not editorial_id: - editorial_id = self.env["product.book.editorial"].create( - {"name": editorial_name} - ) - if len(editorial_id) > 1: - editorial = editorial_id.filtered(lambda x: x.name == editorial_name) - return editorial if editorial else editorial_id[0] - return editorial_id - - def get_genre_id(self, genres): - for genre_name in genres: - genre_id = self.env["product.book.genre"].search( - [("name", "ilike", unidecode.unidecode(genre_name))] - ) - if not genre_id: - genre_id = self.env["product.book.genre"].create({"name": genre_name}) - if len(genre_id) > 1: - genre = genre_id.filtered( - lambda x, genre_name=genre_name: x.name == genre_name - ) - return genre if genre else genre_id[0] - return genre_id - - def get_author_id(self, author_name): - author_id = self.env["product.book.author"].search( - [("name", "ilike", unidecode.unidecode(author_name))] - ) - if not author_id: - author_id = self.env["product.book.author"].create({"name": author_name}) - if len(author_id) > 1: - author = author_id.filtered(lambda x: x.name == author_name) - return author if author else author_id[0] - return author_id - - def action_import_from_isbn(self): - for record in self.filtered(lambda x: x.is_book): - if record.barcode: - client = GoogleBooksAPI() - isbn = record.barcode.replace("-", "") - book = client.get_book_by_isbn13(isbn) - if not book: - book = client.get_book_by_isbn10(isbn) - if book: - # Set data to be updated - data = { - "name": book.title, - } - - if book.published_date: - # Convert to year format - try: - published_year = datetime.strptime( - book.published_date, "%Y-%m-%d" - ).year - except Exception: - published_year = int(book.published_date[:4]) - - data["year_edition"] = published_year - - if book.authors: - data["author_id"] = record.get_author_id(book.authors[0]) - - if book.publisher: - data["editorial_id"] = record.get_editorial_id(book.publisher) - - if book.subjects: - data["genre_id"] = record.get_genre_id(book.subjects) - - if book.description: - data["description"] = book.description - data["description_sale"] = book.description - - if book.large_thumbnail: - data["image_1920"] = record.get_convert_to_base64( - book.large_thumbnail - ) - - # Update book data in Odoo - record.write(data) - - # Show success notification - self.env.user.notify_success( - message=_("Book data updated from Google Books API") - ) - - else: - # Return not found notification - return { - "type": "ir.actions.client", - "tag": "display_notification", - "params": { - "title": _("Warning"), - "type": "warning", - "message": _("Not book found with this data"), - "sticky": True, - }, - } - else: - raise UserError(_("ISBN code is mandatory. Please, provide one.")) + _name = "product.template" + _inherit = ["product.template", "google.books.mixin"] diff --git a/bookstore_mgmt_google_books_api/readme/CONTRIBUTORS.md b/bookstore_mgmt_google_books_api/readme/CONTRIBUTORS.md index 7b362a4..6ffb518 100644 --- a/bookstore_mgmt_google_books_api/readme/CONTRIBUTORS.md +++ b/bookstore_mgmt_google_books_api/readme/CONTRIBUTORS.md @@ -1,3 +1,4 @@ - \[APSL-Nagarro\](): - Miquel Alzanillas \<\> - - Antoni Marroig \<\> \ No newline at end of file + - Antoni Marroig \<\> + - Bernat Obrador \<\>