-
-
Notifications
You must be signed in to change notification settings - Fork 310
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[14.0][IMP] product_import: Import products asynchronously #1076
Open
florentx
wants to merge
2
commits into
OCA:14.0
Choose a base branch
from
florentx:product_import_job
base: 14.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<odoo noupdate="1"> | ||
|
||
<record id="channel_product_import" model="queue.job.channel"> | ||
<field name="name">product_import</field> | ||
<field name="parent_id" ref="queue_job.channel_root" /> | ||
</record> | ||
|
||
<record id="job_create_update_products" model="queue.job.function"> | ||
<field name="model_id" ref="model_product_import" /> | ||
<field name="method">_create_update_products</field> | ||
<field name="channel_id" ref="channel_product_import" /> | ||
</record> | ||
|
||
</odoo> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,12 +4,15 @@ | |
import logging | ||
from base64 import b64decode, b64encode | ||
from datetime import date, timedelta | ||
from itertools import zip_longest | ||
|
||
from lxml import etree | ||
|
||
from odoo import _, api, fields, models | ||
from odoo.exceptions import UserError | ||
|
||
CHUNK_SIZE = 40 | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
|
@@ -197,14 +200,46 @@ | |
return product_vals | ||
|
||
@api.model | ||
def create_product(self, parsed_product, chatter_msg, seller=None): | ||
product_vals = self._prepare_product(parsed_product, chatter_msg, seller=seller) | ||
if not product_vals: | ||
return False | ||
def _create_update_products(self, products, seller_id): | ||
"""Create / Update a product. | ||
|
||
This method is called from a queue job. | ||
""" | ||
|
||
seller = self.env["res.partner"].browse(seller_id) | ||
|
||
log_msgs = [] | ||
for parsed_product in products: | ||
product_vals = self._prepare_product( | ||
parsed_product, log_msgs, seller=seller | ||
) | ||
if product_vals: | ||
msg = self._create_update_product(product_vals) | ||
log_msgs.append(msg) | ||
|
||
return "\n".join(log_msgs) | ||
|
||
@api.model | ||
def _create_update_product(self, product_vals): | ||
"""Create / Update a product. | ||
|
||
This method is called from a queue job. | ||
""" | ||
chatter_msg = [] | ||
|
||
product = product_vals.pop("recordset", None) | ||
if product: | ||
supplierinfo = product_vals.pop("seller_ids", ()) | ||
supplierinfo_obj = self.env["product.supplierinfo"] | ||
for (command, line_id, values) in supplierinfo: | ||
if command == 1: | ||
supplierinfo_obj.browse(line_id).write(values) | ||
elif command == 0: | ||
supplierinfo_obj.create({**values, "product_id": product.id}) | ||
else: | ||
raise RuntimeError(f"Command {command} not supported") | ||
product.write(product_vals) | ||
logger.info("Product %d updated", product.id) | ||
logger.debug("Product %s updated", product.default_code) | ||
else: | ||
product_active = product_vals.pop("active") | ||
product = self.env["product.product"].create(product_vals) | ||
|
@@ -213,33 +248,33 @@ | |
# all characteristics into product.template | ||
product.flush() | ||
product.action_archive() | ||
logger.info("Product %d created", product.id) | ||
return product | ||
logger.debug("Product %s created", product.default_code) | ||
|
||
@api.model | ||
def _create_products(self, catalogue, seller, filename=None): | ||
products = self.env["product.product"].browse() | ||
for product in catalogue.get("products"): | ||
record = self.create_product( | ||
product, | ||
catalogue["chatter_msg"], | ||
seller=seller, | ||
) | ||
if record: | ||
products |= record | ||
self._bdimport.post_create_or_update(catalogue, seller, doc_filename=filename) | ||
logger.info("Products updated for vendor %d", seller.id) | ||
return products | ||
log_msg = f"Product created/updated {product.id}\n" + "\n".join(chatter_msg) | ||
return log_msg | ||
|
||
def import_button(self): | ||
def import_button(self, chunk_size=CHUNK_SIZE): | ||
self.ensure_one() | ||
file_content = b64decode(self.product_file) | ||
catalogue = self.parse_product_catalogue(file_content, self.product_filename) | ||
if not catalogue.get("products"): | ||
raise UserError(_("This catalogue doesn't have any product!")) | ||
company_id = self._get_company_id(catalogue) | ||
seller = self._get_seller(catalogue) | ||
self.with_context(product_company_id=company_id)._create_products( | ||
catalogue, seller, filename=self.product_filename | ||
wiz = self.with_context(product_company_id=company_id) | ||
# Create products asynchronously | ||
iterators = [iter(catalogue["products"])] * chunk_size | ||
for products in zip_longest(*iterators): | ||
if products[-1] is None: | ||
products = [product for product in products if product] | ||
# One job for x products (chunk of 40) | ||
wiz.with_delay()._create_update_products(products, seller.id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here I would simply call |
||
# Save imported file as attachment | ||
self._bdimport.post_create_or_update( | ||
catalogue, seller, doc_filename=self.product_filename | ||
) | ||
logger.info( | ||
"Update for vendor %s: %d products", seller.name, len(catalogue["products"]) | ||
) | ||
|
||
return {"type": "ir.actions.act_window_close"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've thought again about this... We should remove this dependency IMO.
In the simplest case we won't need it, in the more complex case we'll probably use the framework. Then, we can leverage the framework to the split.
Additionally, there's now a new feature in queue_job which is called
split
and allows to.... split items in batches.See OCA/queue#658