ai*
This commit is contained in:
4
ai_website_sale/models/__init__.py
Normal file
4
ai_website_sale/models/__init__.py
Normal file
@@ -0,0 +1,4 @@
|
||||
from . import ai_session
|
||||
from . import product_image
|
||||
from . import product_product
|
||||
from . import product_template
|
||||
14
ai_website_sale/models/ai_session.py
Normal file
14
ai_website_sale/models/ai_session.py
Normal file
@@ -0,0 +1,14 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
|
||||
from odoo import models
|
||||
|
||||
|
||||
class AiSession(models.Model):
|
||||
_inherit = 'ai.session'
|
||||
|
||||
def _get_request_context_snapshot(self, context=None):
|
||||
context = self.env.context if context is None else context
|
||||
return {
|
||||
**super()._get_request_context_snapshot(context),
|
||||
**{key: context[key] for key in ('website_id', 'pricelist_id', 'fiscal_position_id') if key in context},
|
||||
}
|
||||
17
ai_website_sale/models/product_image.py
Normal file
17
ai_website_sale/models/product_image.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from odoo import models
|
||||
|
||||
|
||||
class ProductImage(models.Model):
|
||||
_inherit = 'product.image'
|
||||
|
||||
def _ai_read(self, fnames=None, files_parts=None, files_checksums=None):
|
||||
if fnames:
|
||||
return super()._ai_read(fnames, files_checksums)
|
||||
|
||||
if files_checksums is None:
|
||||
files_checksums = set()
|
||||
|
||||
__, files_parts, files_checksums = super()._ai_read(fnames, files_parts, files_checksums)
|
||||
product_tmpl_id = self.product_tmpl_id if self.product_tmpl_id else self.product_variant_id.product_tmpl_id
|
||||
tmpl_vals_list, files_parts, __ = product_tmpl_id._ai_read(['display_name', 'type', 'description', 'description_sale'], files_parts, files_checksums)
|
||||
return tmpl_vals_list, files_parts
|
||||
36
ai_website_sale/models/product_product.py
Normal file
36
ai_website_sale/models/product_product.py
Normal file
@@ -0,0 +1,36 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
from odoo import models
|
||||
from odoo.http import request
|
||||
|
||||
|
||||
class ProductProduct(models.Model):
|
||||
_inherit = 'product.product'
|
||||
|
||||
def _resolve_product_combination_info(self):
|
||||
"""Prepare website-aware combination info keyed by product ID.
|
||||
:return: {product_id: combination_info} dict, or empty dict if no website context is available.
|
||||
"""
|
||||
# Outside an HTTP request we lack session-based pricing context,
|
||||
# so we fall back to the ai's pre-set context (including website pricelist and fiscal position).
|
||||
pricelist, fiscal_position = None, None
|
||||
if not request:
|
||||
pricelist, fiscal_position = self.env['product.template']._get_website_context()
|
||||
return {
|
||||
product.id: product._get_combination_info_variant(
|
||||
pricelist=pricelist,
|
||||
fiscal_position=fiscal_position,
|
||||
)
|
||||
for product in self
|
||||
}
|
||||
|
||||
def _ai_get_preview_metadata(self):
|
||||
"""Extend preview metadata with website combination prices."""
|
||||
metadata = super()._ai_get_preview_metadata()
|
||||
combo_info = self._resolve_product_combination_info()
|
||||
for entry in metadata:
|
||||
entry.update(combo_info.get(entry['id'], {}))
|
||||
return metadata
|
||||
|
||||
def ai_set_main_image(self, attachment_id):
|
||||
self.ensure_one()
|
||||
self.image_1920 = self.env['ir.attachment'].browse(attachment_id).raw
|
||||
57
ai_website_sale/models/product_template.py
Normal file
57
ai_website_sale/models/product_template.py
Normal file
@@ -0,0 +1,57 @@
|
||||
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
||||
from odoo import api, models
|
||||
from odoo.http import request
|
||||
|
||||
|
||||
class ProductTemplate(models.Model):
|
||||
_inherit = 'product.template'
|
||||
|
||||
@api.model
|
||||
def _get_website_context(self):
|
||||
"""Resolve pricelist and fiscal position from the current context.
|
||||
:return: (pricelist, fiscal_position) recordsets (possibly empty)
|
||||
:rtype: tuple
|
||||
"""
|
||||
return (
|
||||
self.env['product.pricelist'].sudo().browse(self.env.context.get('pricelist_id')),
|
||||
self.env['account.fiscal.position'].sudo().browse(
|
||||
self.env.context.get('fiscal_position_id'),
|
||||
),
|
||||
)
|
||||
|
||||
def _resolve_product_template_pricing(self):
|
||||
"""Return website-aware sales prices for the recordset.
|
||||
|
||||
:return: {tmpl_id: price_dict} or empty dict if no website
|
||||
:rtype: dict
|
||||
"""
|
||||
website = self.env.website
|
||||
if not website:
|
||||
return {}
|
||||
if request and hasattr(request, 'pricelist') and hasattr(request, 'fiscal_position'):
|
||||
pricelist = request.pricelist
|
||||
fiscal_position = request.fiscal_position
|
||||
else:
|
||||
pricelist, fiscal_position = self._get_website_context()
|
||||
return self._get_sales_prices(pricelist, fiscal_position, website)
|
||||
|
||||
def _ai_get_preview_metadata(self):
|
||||
"""Extend preview metadata with website sales prices."""
|
||||
metadata = super()._ai_get_preview_metadata()
|
||||
prices_by_tmpl = self._resolve_product_template_pricing()
|
||||
for entry in metadata:
|
||||
entry.update(prices_by_tmpl.get(entry['id'], {}))
|
||||
return metadata
|
||||
|
||||
def ai_set_main_image(self, attachment_id):
|
||||
self.ensure_one()
|
||||
self.image_1920 = self.env['ir.attachment'].browse(attachment_id).raw
|
||||
|
||||
def ai_add_extra_image(self, attachment_id):
|
||||
self.ensure_one()
|
||||
attachment = self.env['ir.attachment'].browse(attachment_id)
|
||||
self.env['product.image'].create({
|
||||
'product_tmpl_id': self.id,
|
||||
'name': attachment.name,
|
||||
'image_1920': attachment.raw,
|
||||
})
|
||||
Reference in New Issue
Block a user