Skip to content

Commit

Permalink
[IMP] connector_woocommerce: Common product class to be inhereted in …
Browse files Browse the repository at this point in the history
…product_product and product_template.
  • Loading branch information
KNVx committed Oct 26, 2023
1 parent a540324 commit f3099ed
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 105 deletions.
4 changes: 2 additions & 2 deletions connector_woocommerce/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3

=======================
=====================
WooCommerce Connector
=======================
=====================

* WooCommerce connector

Expand Down
1 change: 1 addition & 0 deletions connector_woocommerce/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from . import product_public_category
from . import product_attribute
from . import product_attribute_value
from . import product
from . import product_template
from . import product_product
from . import res_partner
Expand Down
1 change: 1 addition & 0 deletions connector_woocommerce/models/product/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import export_mapper
48 changes: 48 additions & 0 deletions connector_woocommerce/models/product/export_mapper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# Copyright NuoBiT Solutions - Kilian Niubo <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)

from odoo.addons.component.core import AbstractComponent


class WooCommerceProductExportMapper(AbstractComponent):
_name = "woocommerce.product.export.mapper"
_inherit = "woocommerce.export.mapper"

def _prepare_url(self, binding, document):
return binding.wordpress_source_url

Check warning on line 12 in connector_woocommerce/models/product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product/export_mapper.py#L12

Added line #L12 was not covered by tests

def _prepare_document_description(self, record):
document_description = ""

Check warning on line 15 in connector_woocommerce/models/product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product/export_mapper.py#L15

Added line #L15 was not covered by tests
if self.backend_record.wordpress_backend_id:
with self.backend_record.wordpress_backend_id.work_on(

Check warning on line 17 in connector_woocommerce/models/product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product/export_mapper.py#L17

Added line #L17 was not covered by tests
"wordpress.ir.attachment"
) as work:
binder = work.component(usage="binder")

Check warning on line 20 in connector_woocommerce/models/product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product/export_mapper.py#L20

Added line #L20 was not covered by tests
for document in record.document_ids:
external_id = binder.get_external_dict_ids(

Check warning on line 22 in connector_woocommerce/models/product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product/export_mapper.py#L22

Added line #L22 was not covered by tests
document.attachment_id, check_external_id=False
)
if external_id:
binding = binder.wrap_record(document.attachment_id)
document_description += (

Check warning on line 27 in connector_woocommerce/models/product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product/export_mapper.py#L26-L27

Added lines #L26 - L27 were not covered by tests
"<p><a href=%s target='_blank'>%s</a></p>\n"
% (
self._prepare_url(binding, document),
document.with_context(
lang=self.backend_record.language_id.code
).name,
)
)
else:
if (
not self.backend_record.wordpress_backend_id.test_database
and self.backend_record.wordpress_backend_id
):
assert external_id, (

Check warning on line 41 in connector_woocommerce/models/product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product/export_mapper.py#L41

Added line #L41 was not covered by tests
"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 document_description

Check warning on line 48 in connector_woocommerce/models/product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product/export_mapper.py#L48

Added line #L48 was not covered by tests
39 changes: 3 additions & 36 deletions connector_woocommerce/models/product_product/export_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class WooCommerceProductProductExportMapper(Component):
_name = "woocommerce.product.product.export.mapper"
_inherit = "woocommerce.export.mapper"
_inherit = "woocommerce.product.export.mapper"

_apply_on = "woocommerce.product.product"

Expand Down Expand Up @@ -92,41 +92,8 @@ def description(self, record):
lang=self.backend_record.language_id.code
).variant_public_description
document_description = ""

Check warning on line 94 in connector_woocommerce/models/product_product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product_product/export_mapper.py#L94

Added line #L94 was not covered by tests
if record.variant_document_ids:
if self.backend_record.wordpress_backend_id:
with self.backend_record.wordpress_backend_id.work_on(
"wordpress.ir.attachment"
) as work:
binder = work.component(usage="binder")
for data in record.variant_document_ids:
external_id = binder.get_external_dict_ids(
data.attachment_id, check_external_id=False
)
source_url = binder.wrap_record(
data.attachment_id
).wordpress_source_url
if external_id:
variant_document = record.variant_document_ids.filtered(
lambda x: x.datas == data.attachment_id.datas
)
document_description += "<p><a href=%s target='_blank'>%s</a></p>" % (
source_url,
variant_document.with_context(
lang=self.backend_record.language_id.code
).name,
)
else:
if (
not self.backend_record.wordpress_backend_id.test_database
and self.backend_record.wordpress_backend_id
):
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 record.document_ids:
document_description = self._prepare_document_description(record)

Check warning on line 96 in connector_woocommerce/models/product_product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product_product/export_mapper.py#L96

Added line #L96 was not covered by tests
if description:
description = description + "\n" + document_description

Check warning on line 98 in connector_woocommerce/models/product_product/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product_product/export_mapper.py#L98

Added line #L98 was not covered by tests
else:
Expand Down
4 changes: 2 additions & 2 deletions connector_woocommerce/models/product_product/product.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ProductProduct(models.Model):
"alternative_product_ids",
"accessory_product_ids",
"variant_inventory_availability",
"variant_document_ids",
"document_ids",
"product_tmpl_id",
"product_tmpl_id.has_attributes",
"product_tmpl_id.woocommerce_enabled",
Expand Down Expand Up @@ -143,7 +143,7 @@ def _compute_product_image_attachment_ids(self):

def _compute_product_document_attachment_ids(self):
for rec in self:
for doc in rec.variant_document_ids:
for doc in rec.document_ids:
rec.product_document_attachment_ids = [

Check warning on line 147 in connector_woocommerce/models/product_product/product.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product_product/product.py#L147

Added line #L147 was not covered by tests
(
0,
Expand Down
40 changes: 3 additions & 37 deletions connector_woocommerce/models/product_template/export_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

class WooCommerceProductTemplateExportMapper(Component):
_name = "woocommerce.product.template.export.mapper"
_inherit = "woocommerce.export.mapper"
_inherit = "woocommerce.product.export.mapper"

_apply_on = "woocommerce.product.template"

Expand Down Expand Up @@ -113,42 +113,8 @@ def description(self, record):

@mapping
def short_description(self, record):
if self.backend_record.wordpress_backend_id:
with self.backend_record.wordpress_backend_id.work_on(
"wordpress.ir.attachment"
) as work:
binder = work.component(usage="binder")
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
)
source_url = binder.wrap_record(
data.attachment_id
).wordpress_source_url
short_description += "<p><a href=%s target='_blank'>%s</a></p>\n" % (
source_url,
template_document.name,
)
else:
if (
not self.backend_record.wordpress_backend_id.test_database
and self.backend_record.wordpress_backend_id
):
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 ""
}
short_description = self._prepare_document_description(record)
return {"short_description": short_description if short_description else ""}

Check warning on line 117 in connector_woocommerce/models/product_template/export_mapper.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product_template/export_mapper.py#L116-L117

Added lines #L116 - L117 were not covered by tests

@mapping
def product_type(self, record):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _compute_has_attributes(self):
"public_description",
"inventory_availability",
"has_attributes",
"template_document_ids",
"document_ids",
"woocommerce_enabled",
)
def _compute_woocommerce_write_date(self):
Expand Down Expand Up @@ -172,7 +172,7 @@ def _compute_product_image_attachment_ids(self):

def _compute_product_document_attachment_ids(self):
for rec in self:
for doc in rec.template_document_ids:
for doc in rec.document_ids:
rec.product_document_attachment_ids = [

Check warning on line 176 in connector_woocommerce/models/product_template/product_template.py

View check run for this annotation

Codecov / codecov/patch

connector_woocommerce/models/product_template/product_template.py#L176

Added line #L176 was not covered by tests
(
0,
Expand Down
92 changes: 66 additions & 26 deletions connector_woocommerce/views/product_product.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,23 +48,23 @@

</field>
</record>
<record id="product_product_tree_view" model="ir.ui.view">
<field name="name">product.product.connector.form</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_product_tree_view" />
<field
<record id="product_product_tree_view" model="ir.ui.view">
<field name="name">product.product.connector.form</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_product_tree_view" />
<field
name="groups_id"
eval="[(4, ref('connector.group_connector_manager'))]"
/>
<field name="arch" type="xml">
<field name="is_published" position="attributes">
<attribute name="invisible">1</attribute>
</field>
<field name="is_published" position="after">
<field name="variant_is_published" string="Is Published" />
</field>
<field name="arch" type="xml">
<field name="is_published" position="attributes">
<attribute name="invisible">1</attribute>
</field>
<field name="is_published" position="after">
<field name="variant_is_published" string="Is Published" />
</field>
</record>
</field>
</record>
<record id="woocommerce_product_product_view_form" model="ir.ui.view">
<field name="name">woocommerce.product.product.view.form</field>
<field name="model">woocommerce.product.product</field>
Expand Down Expand Up @@ -99,23 +99,63 @@
</tree>
</field>
</record>
<record id="product_search_form_view" model="ir.ui.view">
<field name="name">product.woocommerce.search.published</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_search_form_view" />
<field name="arch" type="xml">
<xpath expr="//filter[@name='published']" position="attributes">
<attribute name="invisible">1</attribute>
</xpath>
<xpath expr="//filter[@name='published']" position="after">
<filter
<record
id="product_product_view_form_easy_inherit_connector_woocommerce"
model="ir.ui.view"
>
<field
name="name"
>woocommerce.product.view.form.easy.inherit.connector_woocommerce
</field>
<field name="model">product.product</field>
<field
name="inherit_id"
ref="website_sale.product_product_view_form_easy_inherit_website_sale"
/>
<field name="arch" type="xml">
<sheet position="inside">
<notebook>
<page name="shop" string="eCommerce">
<group name="shop">
<field name="variant_is_published" />
<field name="variant_public_description" />
<field name="variant_inventory_availability" />
</group>
<group name="product_template_document" string="Documents">
<field name="document_ids" nolabel="1">
<tree editable="bottom">
<field name="sequence" widget="handle" />
<field name="name" />
<field name="datas" filename="datas_fname" />
<field name="datas_fname" />
</tree>
</field>
</group>
</page>
</notebook>
</sheet>
<group name="product_template_document" position="before">
<group name="product_variant_images" position="move" />
</group>
</field>
</record>
<record id="product_search_form_view" model="ir.ui.view">
<field name="name">product.woocommerce.search.published</field>
<field name="model">product.product</field>
<field name="inherit_id" ref="product.product_search_form_view" />
<field name="arch" type="xml">
<xpath expr="//filter[@name='published']" position="attributes">
<attribute name="invisible">1</attribute>
</xpath>
<xpath expr="//filter[@name='published']" position="after">
<filter
name="variant_is_published"
string="Published"
domain="[('variant_is_published', '=', True)]"
/>
</xpath>
</field>
</record>
</xpath>
</field>
</record>
<record id="woocommerce_product_product_action" model="ir.actions.act_window">
<field name="name">Products</field>
<field name="res_model">woocommerce.product.product</field>
Expand Down

0 comments on commit f3099ed

Please sign in to comment.