From 70d586ee13d98fbb48727dbbc9dee054216bf159 Mon Sep 17 00:00:00 2001 From: Antoni Marroig Campomar Date: Wed, 13 Nov 2024 16:48:11 +0100 Subject: [PATCH] [FIX] bookstore_mgmt_google_books_api: Fix product model functions --- .../__manifest__.py | 2 +- bookstore_mgmt_google_books_api/i18n/ca.po | 22 ++++-- bookstore_mgmt_google_books_api/i18n/es.po | 22 ++++-- .../models/__init__.py | 3 +- .../models/product_product.py | 79 +++++++++++++++++++ .../{product.py => product_template.py} | 0 6 files changed, 112 insertions(+), 16 deletions(-) create mode 100644 bookstore_mgmt_google_books_api/models/product_product.py rename bookstore_mgmt_google_books_api/models/{product.py => product_template.py} (100%) diff --git a/bookstore_mgmt_google_books_api/__manifest__.py b/bookstore_mgmt_google_books_api/__manifest__.py index 491a68c..b98fd1d 100644 --- a/bookstore_mgmt_google_books_api/__manifest__.py +++ b/bookstore_mgmt_google_books_api/__manifest__.py @@ -9,7 +9,7 @@ "license": "AGPL-3", "maintainers": ["peluko00", "miquelalzanillas"], "version": "17.0.1.0.0", - "depends": ["bookstore_mgmt", "web_notify", "stock"], + "depends": ["bookstore_mgmt", "web_notify"], "external_dependencies": { "python": ["google-books-api-wrapper", "Unidecode"], }, diff --git a/bookstore_mgmt_google_books_api/i18n/ca.po b/bookstore_mgmt_google_books_api/i18n/ca.po index d2cf835..29415d4 100644 --- a/bookstore_mgmt_google_books_api/i18n/ca.po +++ b/bookstore_mgmt_google_books_api/i18n/ca.po @@ -6,11 +6,10 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-11-06 14:45+0000\n" -"PO-Revision-Date: 2024-11-06 14:45+0000\n" +"POT-Creation-Date: 2024-11-13 15:45+0000\n" +"PO-Revision-Date: 2024-11-13 15:45+0000\n" "Last-Translator: \n" "Language-Team: \n" -"Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: \n" @@ -18,14 +17,16 @@ msgstr "" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: code:addons/bookstore_mgmt_google_books_api/models/product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 #, python-format msgid "Book data updated from Google Books API" msgstr "Dades actualitzades des de l'API de Google Books" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: code:addons/bookstore_mgmt_google_books_api/models/product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 #, python-format msgid "ISBN code is mandatory. Please, provide one." msgstr "El codi ISBN és obligatori. Si us plau, proporcioneu-ne un." @@ -38,7 +39,8 @@ msgstr "Importa la informació del llibre" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: code:addons/bookstore_mgmt_google_books_api/models/product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 #, python-format msgid "Not book found with this data" msgstr "No s'ha trobat cap llibre amb aquestes dades" @@ -48,9 +50,15 @@ msgstr "No s'ha trobat cap llibre amb aquestes dades" msgid "Product" msgstr "Producte" +#. module: bookstore_mgmt_google_books_api +#: model:ir.model,name:bookstore_mgmt_google_books_api.model_product_product +msgid "Product Variant" +msgstr "Variant de producte" + #. module: bookstore_mgmt_google_books_api #. odoo-python -#: code:addons/bookstore_mgmt_google_books_api/models/product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 #: code:addons/bookstore_mgmt_google_books_api/tests/test_google_books_api.py:0 #, python-format msgid "Warning" diff --git a/bookstore_mgmt_google_books_api/i18n/es.po b/bookstore_mgmt_google_books_api/i18n/es.po index db35300..2f57233 100644 --- a/bookstore_mgmt_google_books_api/i18n/es.po +++ b/bookstore_mgmt_google_books_api/i18n/es.po @@ -6,11 +6,10 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2024-11-06 14:45+0000\n" -"PO-Revision-Date: 2024-11-06 14:45+0000\n" +"POT-Creation-Date: 2024-11-13 15:45+0000\n" +"PO-Revision-Date: 2024-11-13 15:45+0000\n" "Last-Translator: \n" "Language-Team: \n" -"Language: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: \n" @@ -18,14 +17,16 @@ msgstr "" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: code:addons/bookstore_mgmt_google_books_api/models/product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 #, python-format msgid "Book data updated from Google Books API" msgstr "Datos actualizados desde la API de Google Libros" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: code:addons/bookstore_mgmt_google_books_api/models/product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 #, python-format msgid "ISBN code is mandatory. Please, provide one." msgstr "El código ISBN es obligatorio. Por favor, proporcione uno." @@ -38,7 +39,8 @@ msgstr "Importar datos del libro" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: code:addons/bookstore_mgmt_google_books_api/models/product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 #, python-format msgid "Not book found with this data" msgstr "No se ha encontrado ningún libro con estos datos" @@ -48,9 +50,15 @@ msgstr "No se ha encontrado ningún libro con estos datos" msgid "Product" msgstr "Producto" +#. module: bookstore_mgmt_google_books_api +#: model:ir.model,name:bookstore_mgmt_google_books_api.model_product_product +msgid "Product Variant" +msgstr "Variante de producto" + #. module: bookstore_mgmt_google_books_api #. odoo-python -#: code:addons/bookstore_mgmt_google_books_api/models/product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_product.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 #: code:addons/bookstore_mgmt_google_books_api/tests/test_google_books_api.py:0 #, python-format msgid "Warning" diff --git a/bookstore_mgmt_google_books_api/models/__init__.py b/bookstore_mgmt_google_books_api/models/__init__.py index ff8911a..5d72f9b 100644 --- a/bookstore_mgmt_google_books_api/models/__init__.py +++ b/bookstore_mgmt_google_books_api/models/__init__.py @@ -1,3 +1,4 @@ # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). -from . import product +from . import product_product +from . import product_template diff --git a/bookstore_mgmt_google_books_api/models/product_product.py b/bookstore_mgmt_google_books_api/models/product_product.py new file mode 100644 index 0000000..ec60659 --- /dev/null +++ b/bookstore_mgmt_google_books_api/models/product_product.py @@ -0,0 +1,79 @@ +# 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 + + +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 = book.published_date + + 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.py b/bookstore_mgmt_google_books_api/models/product_template.py similarity index 100% rename from bookstore_mgmt_google_books_api/models/product.py rename to bookstore_mgmt_google_books_api/models/product_template.py