Skip to content

Commit

Permalink
Merge PR #26 into 17.0
Browse files Browse the repository at this point in the history
Signed-off-by peluko00
  • Loading branch information
OCA-git-bot committed Jan 16, 2025
2 parents 82065db + 0807fe4 commit f951e5c
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 196 deletions.
2 changes: 1 addition & 1 deletion bookstore_mgmt/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,4 @@ Current `maintainers <https://odoo-community.org/page/maintainer-role>`__:

This module is part of the `OCA/vertical-edition <https://github.com/OCA/vertical-edition/tree/17.0/bookstore_mgmt>`_ 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.
7 changes: 4 additions & 3 deletions bookstore_mgmt_google_books_api/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,11 @@ Authors
Contributors
------------

- [APSL-Nagarro](https://apsl.tech):
- [APSL-Nagarro](https://apsl.tech):

- Miquel Alzanillas <[email protected]>
- Antoni Marroig <[email protected]>
- Miquel Alzanillas <[email protected]>
- Antoni Marroig <[email protected]>
- Bernat Obrador <[email protected]>

Maintainers
-----------
Expand Down
1 change: 1 addition & 0 deletions bookstore_mgmt_google_books_api/models/__init__.py
Original file line number Diff line number Diff line change
@@ -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
120 changes: 120 additions & 0 deletions bookstore_mgmt_google_books_api/models/google_books_mixin.py
Original file line number Diff line number Diff line change
@@ -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."))
77 changes: 3 additions & 74 deletions bookstore_mgmt_google_books_api/models/product_product.py
Original file line number Diff line number Diff line change
@@ -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"]
120 changes: 3 additions & 117 deletions bookstore_mgmt_google_books_api/models/product_template.py
Original file line number Diff line number Diff line change
@@ -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"]
3 changes: 2 additions & 1 deletion bookstore_mgmt_google_books_api/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
- \[APSL-Nagarro\](<https://apsl.tech>):
- Miquel Alzanillas \<<[email protected]>\>
- Antoni Marroig \<<[email protected]>\>
- Antoni Marroig \<<[email protected]>\>
- Bernat Obrador \<<[email protected]>\>

0 comments on commit f951e5c

Please sign in to comment.