-
-
Notifications
You must be signed in to change notification settings - Fork 569
[18.0][IMP] website_sale_hide_empty_category: Use _read_group and cache to performance #1226
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
base: 18.0
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,12 +16,27 @@ class ProductPublicCategory(models.Model): | |
| @api.depends("product_tmpl_ids", "child_id.has_product_recursive") | ||
| @api.depends_context("website_id") | ||
| def _compute_has_product_recursive(self): | ||
| for category in self: | ||
| website = self.env["website"].get_current_website() | ||
| website_domain = website.sale_product_domain() | ||
| has_products = bool( | ||
| category.product_tmpl_ids.filtered_domain(website_domain) | ||
| ) | ||
| category.has_product_recursive = has_products or any( | ||
| child.has_product_recursive for child in category.child_id | ||
| website = self.env["website"].get_current_website() | ||
| website_domain = website.sale_product_domain() | ||
| data = self.env["product.template"]._read_group( | ||
| domain=website_domain, | ||
| groupby=["public_categ_ids"], | ||
| ) | ||
| used_category_ids = set() | ||
| for group in data: | ||
| category = group[0] | ||
| if not category or (category.website_id and category.website_id != website): | ||
|
Contributor
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. I would add these filtering conditions to the search domain directly. categories = self.search(
[
("website_id", "in", [False, website.id]),
("product_tmpl_ids", "any", website_domain),
]
) |
||
| continue | ||
| used_category_ids.update( | ||
|
Contributor
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. I'm not sure if this works (nested categories = self.search(
[
("website_id", "in", [False, website.id]),
"|",
("product_tmpl_ids", "any", website_domain),
("child_ids", "any", ("product_tmpl_ids", "any", website_domain)),
]
) |
||
| int(category_id) | ||
| for category_id in category.parent_path.split("/") | ||
| if category_id | ||
| ) | ||
| # TODO: Filter categories before split in c to avoid cache because compute is | ||
| # called with only one category | ||
| categories = self.search(website.website_domain()) | self | ||
| self.env.cache.update_raw( | ||
|
Contributor
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. I'm not sure about this respects the ORM, tbh. For instance if you have other computed fields depending on them, I don't think their recomputations will be triggered like this. Maybe the cache can be used but not like this. Perhaps we could check if we already have a cached But I'd say first check the performance with the recommendations I provided above, maybe it's not even needed |
||
| categories, | ||
| self._fields["has_product_recursive"], | ||
| [category.id in used_category_ids for category in categories], | ||
| ) | ||
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'm not sure the read_group is the right choice here, because we don't really care about the grouped products.. so that's just overhead.
How about this instead?