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/i18n/bookstore_mgmt_google_books_api.pot b/bookstore_mgmt_google_books_api/i18n/bookstore_mgmt_google_books_api.pot index 6eb9d9c..e23dc1f 100644 --- a/bookstore_mgmt_google_books_api/i18n/bookstore_mgmt_google_books_api.pot +++ b/bookstore_mgmt_google_books_api/i18n/bookstore_mgmt_google_books_api.pot @@ -6,6 +6,8 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2025-01-16 10:09+0000\n" +"PO-Revision-Date: 2025-01-16 10:09+0000\n" "Last-Translator: \n" "Language-Team: \n" "MIME-Version: 1.0\n" @@ -15,16 +17,14 @@ msgstr "" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #, python-format msgid "Book data updated from Google Books API" msgstr "" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #, python-format msgid "ISBN code is mandatory. Please, provide one." msgstr "" @@ -37,8 +37,7 @@ msgstr "" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #, python-format msgid "Not book found with this data" msgstr "" @@ -55,9 +54,13 @@ msgstr "" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #: code:addons/bookstore_mgmt_google_books_api/tests/test_google_books_api.py:0 #, python-format msgid "Warning" msgstr "" + +#. module: bookstore_mgmt_google_books_api +#: model:ir.model,name:bookstore_mgmt_google_books_api.model_google_books_mixin +msgid "google.books.mixin" +msgstr "" \ No newline at end of file diff --git a/bookstore_mgmt_google_books_api/i18n/ca.po b/bookstore_mgmt_google_books_api/i18n/ca.po index 3b22205..94a5886 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-13 15:45+0000\n" -"PO-Revision-Date: 2024-11-13 15:45+0000\n" +"POT-Creation-Date: 2025-01-16 10:02+0000\n" +"PO-Revision-Date: 2025-01-16 10:02+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,16 +17,14 @@ msgstr "" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.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_product.py:0 -#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/google_books_mixin.py:0 #, python-format msgid "ISBN code is mandatory. Please, provide one." msgstr "El codi ISBN és obligatori. Si us plau, proporcioneu-ne un." @@ -40,8 +37,7 @@ msgstr "Importa la informació del llibre" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #, python-format msgid "Not book found with this data" msgstr "No s'ha trobat cap llibre amb aquestes dades" @@ -58,9 +54,13 @@ msgstr "Variant de producte" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #: code:addons/bookstore_mgmt_google_books_api/tests/test_google_books_api.py:0 #, python-format msgid "Warning" msgstr "Avís" + +#. module: bookstore_mgmt_google_books_api +#: model:ir.model,name:bookstore_mgmt_google_books_api.model_google_books_mixin +msgid "google.books.mixin" +msgstr "" \ No newline at end of file diff --git a/bookstore_mgmt_google_books_api/i18n/es.po b/bookstore_mgmt_google_books_api/i18n/es.po index 3fd0300..596287d 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-13 15:45+0000\n" -"PO-Revision-Date: 2024-11-13 15:45+0000\n" +"POT-Creation-Date: 2025-01-16 10:10+0000\n" +"PO-Revision-Date: 2025-01-16 10:10+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,16 +17,14 @@ msgstr "" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.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_product.py:0 -#: code:addons/bookstore_mgmt_google_books_api/models/product_template.py:0 +#: code:addons/bookstore_mgmt_google_books_api/models/google_books_mixin.py:0 #, python-format msgid "ISBN code is mandatory. Please, provide one." msgstr "El código ISBN es obligatorio. Por favor, proporcione uno." @@ -40,8 +37,7 @@ msgstr "Importar datos del libro" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #, python-format msgid "Not book found with this data" msgstr "No se ha encontrado ningún libro con estos datos" @@ -58,9 +54,13 @@ msgstr "Variante de producto" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #: code:addons/bookstore_mgmt_google_books_api/tests/test_google_books_api.py:0 #, python-format msgid "Warning" msgstr "Advertencia" + +#. module: bookstore_mgmt_google_books_api +#: model:ir.model,name:bookstore_mgmt_google_books_api.model_google_books_mixin +msgid "google.books.mixin" +msgstr "" \ No newline at end of file diff --git a/bookstore_mgmt_google_books_api/i18n/it.po b/bookstore_mgmt_google_books_api/i18n/it.po index 7209368..2346801 100644 --- a/bookstore_mgmt_google_books_api/i18n/it.po +++ b/bookstore_mgmt_google_books_api/i18n/it.po @@ -6,28 +6,25 @@ msgid "" msgstr "" "Project-Id-Version: Odoo Server 17.0\n" "Report-Msgid-Bugs-To: \n" -"PO-Revision-Date: 2024-11-25 11:06+0000\n" -"Last-Translator: mymage \n" -"Language-Team: none\n" -"Language: it\n" +"POT-Creation-Date: 2025-01-16 10:10+0000\n" +"PO-Revision-Date: 2025-01-16 10:10+0000\n" +"Last-Translator: \n" +"Language-Team: \n" "MIME-Version: 1.0\n" "Content-Type: text/plain; charset=UTF-8\n" "Content-Transfer-Encoding: \n" -"Plural-Forms: nplurals=2; plural=n != 1;\n" -"X-Generator: Weblate 5.6.2\n" +"Plural-Forms: \n" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #, python-format msgid "Book data updated from Google Books API" msgstr "Dati libro aggiornati da API Google Books" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #, python-format msgid "ISBN code is mandatory. Please, provide one." msgstr "Il codice ISBN è obbligatorio. Inserirne uno." @@ -40,8 +37,7 @@ msgstr "Importa dati libro" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #, python-format msgid "Not book found with this data" msgstr "Nessun libro trovato con questi dati" @@ -58,9 +54,13 @@ msgstr "Variante prodotto" #. module: bookstore_mgmt_google_books_api #. odoo-python -#: 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/models/google_books_mixin.py:0 #: code:addons/bookstore_mgmt_google_books_api/tests/test_google_books_api.py:0 #, python-format msgid "Warning" msgstr "Attenzione" + +#. module: bookstore_mgmt_google_books_api +#: model:ir.model,name:bookstore_mgmt_google_books_api.model_google_books_mixin +msgid "google.books.mixin" +msgstr "" \ No newline at end of file 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 \<\>