Skip to content

Commit

Permalink
[IMP] connector_woocommerce: included documents to export on product_…
Browse files Browse the repository at this point in the history
…template and product_product exporters
  • Loading branch information
KNVx committed Oct 23, 2023
1 parent 7cfe8fd commit ba1e4c7
Show file tree
Hide file tree
Showing 9 changed files with 176 additions and 46 deletions.
2 changes: 1 addition & 1 deletion connector_woocommerce/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
"depends": [
"connector_extension_woocommerce",
"website_sale",
"website_sale_product_document",
"connector_wordpress",
"sale_stock",
],
Expand Down
2 changes: 1 addition & 1 deletion connector_woocommerce/models/common/product_attachment.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from odoo import fields, models


# This model is used to store in order attachments from product images
# This model is used to store in order attachments from product images - documents
class ProductAttachment(models.TransientModel):
_name = "product.attachment"
_order = "sequence"
Expand Down
75 changes: 54 additions & 21 deletions connector_woocommerce/models/product_product/export_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,48 @@ def stock(self, record):

@mapping
def description(self, record):
return {
"description": record.with_context(
lang=self.backend_record.language_id.code
).variant_public_description
or None
}
description = record.with_context(
lang=self.backend_record.language_id.code
).variant_public_description
document_description = ""
if record.variant_document_ids:
if self.collection.wordpress_backend_id:
with self.collection.wordpress_backend_id.work_on(
"wordpress.ir.attachment"
) as work:
exporter = work.component(self._usage)
binder = exporter.binder_for("wordpress.ir.attachment")
for data in record.variant_document_ids:
external_id = binder.get_external_dict_ids(
data.attachment_id, check_external_id=False
)
if external_id:
variant_document = record.variant_document_ids.filtered(
lambda x: x.datas == data.attachment_id.datas
)
document_description += "<p><a href=%s>%s</a></p>" % (
external_id["source_url"],
variant_document.with_context(
lang=self.backend_record.language_id.code
).description,
)
else:
if (
self.backend_record.wordpress_backend_id
and not self.backend_record.wordpress_backend_id.test_database
):
assert external_id, (
"Unexpected error on %s:"
"The backend id cannot be obtained."
"At this stage, the backend record should "
"have been already linked via "
"._export_dependencies. " % record._name
)
if description:
description = description + "\n" + document_description
else:
description = document_description
return {"description": description or None}

@mapping
def parent_id(self, record):
Expand All @@ -104,27 +140,24 @@ def parent_id(self, record):
@mapping
def image(self, record):
# WooCommerce only allows one image per variant product
if record.product_attachment_ids and self.collection.wordpress_backend_id:
if record.product_image_attachment_ids and self.collection.wordpress_backend_id:
with self.collection.wordpress_backend_id.work_on(
"wordpress.ir.attachment"
) as work:
exporter = work.component(self._usage)
binder = exporter.binder_for("wordpress.ir.attachment")
if record.product_attachment_ids:
image = record.product_attachment_ids[0].attachment_id
values = binder.get_external_dict_ids(
image, check_external_id=False
)
if (
not values
and self.backend_record.wordpress_backend_id.test_database
):
return
return {
"image": {
"id": values["id"],
}
image = record.product_image_attachment_ids[0].attachment_id
values = binder.get_external_dict_ids(image, check_external_id=False)
if (
not values
and self.backend_record.wordpress_backend_id.test_database
):
return
return {
"image": {
"id": values["id"],
}
}

@mapping
def attributes(self, record):
Expand Down
15 changes: 13 additions & 2 deletions connector_woocommerce/models/product_product/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def _export_dependencies(self, relation):
attribute_line.attribute_id, "woocommerce.product.attribute"
)
if (
relation.product_attachment_ids
relation.product_image_attachment_ids
and len(relation.product_tmpl_id.product_variant_ids) > 1
):
if self.collection.wordpress_backend_id:
Expand All @@ -71,6 +71,17 @@ def _export_dependencies(self, relation):
) as work:
exporter = work.component(self._usage)
exporter._export_dependency(
relation.product_attachment_ids[0].attachment_id,
relation.product_image_attachment_ids[0].attachment_id,
"wordpress.ir.attachment",
)
if relation.product_document_attachment_ids:
if self.collection.wordpress_backend_id:
with self.collection.wordpress_backend_id.work_on(
"wordpress.ir.attachment"
) as work:
exporter = work.component(self._usage)
for attachment in relation.product_document_attachment_ids:
exporter._export_dependency(
attachment.attachment_id,
"wordpress.ir.attachment",
)
38 changes: 31 additions & 7 deletions connector_woocommerce/models/product_product/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ProductProduct(models.Model):
"alternative_product_ids",
"accessory_product_ids",
"variant_inventory_availability",
"variant_document_ids",
"product_tmpl_id",
"product_tmpl_id.has_attributes",
"product_tmpl_id.woocommerce_enabled",
Expand Down Expand Up @@ -83,12 +84,12 @@ def _compute_variant_is_published(self):
for rec in self:
rec.variant_is_published = rec.product_tmpl_id.is_published

product_attachment_ids = fields.Many2many(
product_image_attachment_ids = fields.Many2many(
comodel_name="product.attachment",
compute="_compute_product_attachment_ids",
compute="_compute_product_image_attachment_ids",
)

def _compute_product_attachment_ids(self):
def _compute_product_image_attachment_ids(self):
for rec in self:
attachment = self.env["ir.attachment"].search(
[
Expand All @@ -98,7 +99,7 @@ def _compute_product_attachment_ids(self):
]
)
if attachment:
rec.product_attachment_ids = [
rec.product_image_attachment_ids = [
(
0,
0,
Expand All @@ -122,7 +123,7 @@ def _compute_product_attachment_ids(self):
("res_field", "=", "image_1920"),
]
)
rec.product_attachment_ids = [
rec.product_image_attachment_ids = [
(
0,
0,
Expand All @@ -132,5 +133,28 @@ def _compute_product_attachment_ids(self):
},
)
]
if not rec.product_attachment_ids:
rec.product_attachment_ids = self.env["product.attachment"]
if not rec.product_image_attachment_ids:
rec.product_image_attachment_ids = self.env["product.attachment"]

product_document_attachment_ids = fields.Many2many(
comodel_name="product.attachment",
compute="_compute_product_document_attachment_ids",
)

def _compute_product_document_attachment_ids(self):
for rec in self:
sequence = 0
for attachment in rec.variant_document_ids.mapped("attachment_id"):
rec.product_document_attachment_ids = [
(
0,
0,
{
"attachment_id": attachment.id,
"sequence": sequence,
},
)
]
sequence += 1
if not rec.product_document_attachment_ids:
rec.product_document_attachment_ids = self.env["product.attachment"]
43 changes: 41 additions & 2 deletions connector_woocommerce/models/product_template/export_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,43 @@ def description(self, record):
).variant_public_description
return {"description": description if description else ""}

@mapping
def short_description(self, record):
if self.collection.wordpress_backend_id:
with self.collection.wordpress_backend_id.work_on(
"wordpress.ir.attachment"
) as work:
exporter = work.component(self._usage)
binder = exporter.binder_for("wordpress.ir.attachment")
short_description = ""
for data in record.template_document_ids:
external_id = binder.get_external_dict_ids(
data.attachment_id, check_external_id=False
)
if external_id:
template_document = record.template_document_ids.filtered(
lambda x: x.datas == data.attachment_id.datas
)
short_description += "<p><a href=%s>%s</a></p>\n" % (
external_id["source_url"],
template_document.description,
)
else:
if (
self.backend_record.wordpress_backend_id
and not self.backend_record.wordpress_backend_id.test_database
):
assert external_id, (
"Unexpected error on %s:"
"The backend id cannot be obtained."
"At this stage, the backend record should "
"have been already linked via "
"._export_dependencies. " % record._name
)
return {
"short_description": short_description if short_description else ""
}

@mapping
def product_type(self, record):
return {"type": "variable" if record.has_attributes else "simple"}
Expand Down Expand Up @@ -198,8 +235,10 @@ def images(self, record):
exporter = work.component(self._usage)
binder = exporter.binder_for("wordpress.ir.attachment")
img_list = []
if record.product_attachment_ids:
for image in record.product_attachment_ids.mapped("attachment_id"):
if record.product_image_attachment_ids:
for image in record.product_image_attachment_ids.mapped(
"attachment_id"
):
external_id = binder.get_external_dict_ids(
image, check_external_id=False
)
Expand Down
9 changes: 7 additions & 2 deletions connector_woocommerce/models/product_template/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,13 @@ def _export_dependencies(self, relation):
"wordpress.ir.attachment"
) as work:
exporter = work.component(self._usage)
for attachment in relation.product_attachment_ids:
for image_attachment in relation.product_image_attachment_ids:
exporter._export_dependency(
attachment.attachment_id,
image_attachment.attachment_id,
"wordpress.ir.attachment",
)
for document_attachment in relation.product_document_attachment_ids:
exporter._export_dependency(
document_attachment.attachment_id,
"wordpress.ir.attachment",
)
35 changes: 28 additions & 7 deletions connector_woocommerce/models/product_template/product_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def _compute_has_attributes(self):
"public_description",
"inventory_availability",
"has_attributes",
"template_document_ids",
"woocommerce_enabled",
)
def _compute_woocommerce_write_date(self):
Expand Down Expand Up @@ -112,12 +113,12 @@ def _inverse_inventory_availability(self):
rec.inventory_availability
)

product_attachment_ids = fields.Many2many(
product_image_attachment_ids = fields.Many2many(
comodel_name="product.attachment",
compute="_compute_product_attachment_ids",
compute="_compute_product_image_attachment_ids",
)

def _compute_product_attachment_ids(self):
def _compute_product_image_attachment_ids(self):
for rec in self:
attachment = self.env["ir.attachment"].search(
[
Expand All @@ -127,7 +128,7 @@ def _compute_product_attachment_ids(self):
]
)
if attachment:
rec.product_attachment_ids = [
rec.product_image_attachment_ids = [
(
0,
0,
Expand All @@ -151,7 +152,7 @@ def _compute_product_attachment_ids(self):
("res_field", "=", "image_1920"),
]
)
rec.product_attachment_ids = [
rec.product_image_attachment_ids = [
(
0,
0,
Expand All @@ -161,8 +162,28 @@ def _compute_product_attachment_ids(self):
},
)
]
if not rec.product_attachment_ids:
rec.product_attachment_ids = self.env["product.attachment"]
if not rec.product_image_attachment_ids:
rec.product_image_attachment_ids = self.env["product.attachment"]

product_document_attachment_ids = fields.Many2many(
comodel_name="product.attachment",
compute="_compute_product_document_attachment_ids",
)

def _compute_product_document_attachment_ids(self):
for rec in self:
sequence = 0
for attachment in rec.template_document_ids.mapped("attachment_id"):
rec.product_document_attachment_ids = [
(
0,
0,
{"attachment_id": attachment.id, "sequence": sequence},
)
]
sequence += 1
if not rec.product_document_attachment_ids:
rec.product_document_attachment_ids = self.env["product.attachment"]

def write(self, vals):
res = super().write(vals)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

# TODO: check lotes de facturacion -- no se como deberia verse,
# en las que he hecho sale un error de que no hay factura

# This field is created with digits=False to force
# the creation with type numeric, like discount.
woocommerce_discount = fields.Float(
Expand Down

0 comments on commit ba1e4c7

Please sign in to comment.