Skip to content

Commit

Permalink
[MIG][16.0] Migrate connector_prestashop
Browse files Browse the repository at this point in the history
  • Loading branch information
florian-dacosta committed Jul 18, 2023
1 parent 0acc4e2 commit 0f04ffd
Show file tree
Hide file tree
Showing 19 changed files with 91 additions and 112 deletions.
8 changes: 4 additions & 4 deletions connector_prestashop/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

{
"name": "PrestaShop-Odoo connector",
"version": "15.0.1.0.0",
"version": "16.0.1.0.0",
"license": "AGPL-3",
"depends": [
"account",
Expand All @@ -16,7 +16,6 @@
"product_multi_image", # oca/product-attribute
"connector_ecommerce", # oca/connector-ecommerce
"purchase",
"onchange_helper",
],
"external_dependencies": {
"python": [
Expand All @@ -36,7 +35,9 @@
"Odoo Community Association (OCA)",
"website": "https://github.com/OCA/connector-prestashop",
"category": "Connector",
"demo": [],
"demo": [
"demo/backend.xml",
],
"data": [
"security/ir.model.access.csv",
"security/prestashop_security.xml",
Expand All @@ -55,7 +56,6 @@
"views/account_view.xml",
"views/stock_view.xml",
"views/queue_job_views.xml",
"demo/backend.xml",
],
"installable": True,
"application": True,
Expand Down
4 changes: 2 additions & 2 deletions connector_prestashop/components/exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def run(self, binding, *args, **kwargs):
self.binder.bind(self.prestashop_id, self.binding)
# commit so we keep the external ID if several cascading exports
# are called and one of them fails
if not getattr(threading.currentThread(), "testing", False):
if not getattr(threading.current_thread(), "testing", False):
self.env.cr.commit() # pylint: disable=invalid-commit
self._after_export()
return result
Expand Down Expand Up @@ -162,7 +162,7 @@ def _get_or_create_binding(
# Eager commit to avoid having 2 jobs
# exporting at the same time.
# do never commit during tests
if not getattr(threading.currentThread(), "testing", False):
if not getattr(threading.current_thread(), "testing", False):
model_c._cr.commit() # pylint: disable=invalid-commit
else:
# If prestashop_bind_ids does not exist we are typically in a
Expand Down
47 changes: 22 additions & 25 deletions connector_prestashop/components/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,31 +156,28 @@ def do_in_new_connector_env(self, model_name=None):
This can be used to make a preemptive check in a new transaction,
for instance to see if another transaction already made the work.
"""
with odoo.api.Environment.manage():
registry = odoo.modules.registry.Registry(self.env.cr.dbname)
with closing(registry.cursor()) as cr:
try:
new_env = odoo.api.Environment(cr, self.env.uid, self.env.context)
# connector_env = self.connector_env.create_environment(
# self.backend_record.with_env(new_env),
# model_name or self.model._name,
# connector_env=self.connector_env
# )
with self.backend_record.with_env(new_env).work_on(
self.model._name
) as work2:
yield work2
except BaseException:
cr.rollback()
raise
else:
# Despite what pylint says, this a perfectly valid
# commit (in a new cursor). Disable the warning.
self.env[
"base"
].flush() # TODO FIXME check if and why flush is mandatory here
if not getattr(threading.currentThread(), "testing", False):
cr.commit() # pylint: disable=invalid-commit
registry = odoo.modules.registry.Registry(self.env.cr.dbname)
with closing(registry.cursor()) as cr:
try:
new_env = odoo.api.Environment(cr, self.env.uid, self.env.context)
# connector_env = self.connector_env.create_environment(
# self.backend_record.with_env(new_env),
# model_name or self.model._name,
# connector_env=self.connector_env
# )
with self.backend_record.with_env(new_env).work_on(
self.model._name
) as work2:
yield work2
except BaseException:
cr.rollback()
raise
else:
# Despite what pylint says, this a perfectly valid
# commit (in a new cursor). Disable the warning.
self.env.flush_all() # TODO FIXME check if and why flush is mandatory here
if not getattr(threading.current_thread(), "testing", False):
cr.commit() # pylint: disable=invalid-commit

def _check_in_new_connector_env(self):
# with self.do_in_new_connector_env() as new_connector_env:
Expand Down
5 changes: 4 additions & 1 deletion connector_prestashop/models/account_tax_group/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,8 @@ class TaxGroupAdapter(Component):
def search(self, filters=None):
if filters is None:
filters = {}
filters["filter[deleted]"] = 0
# deleted does not exist in 1.5 version. Not sure when it arrived though
# adapt the version if needed.
if self.work.collection.version > "1.5":
filters["filter[deleted]"] = 0
return super().search(filters)
12 changes: 6 additions & 6 deletions connector_prestashop/models/product_product/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def _compute_product_price_extra(self):
def _set_variants_default_on(self, default_on_list=None):
if self.env.context.get("skip_check_default_variant", False):
return True
templates = self.mapped("product_tmpl_id")
templates = self.product_tmpl_id
for template in templates:
variants = template.with_context(
skip_check_default_variant=True
Expand All @@ -79,11 +79,11 @@ def _set_variants_default_on(self, default_on_list=None):
else:
variants[1:].write({"default_on": False})

@api.model
def create(self, vals):
res = super().create(vals)
res._set_variants_default_on()
return res
@api.model_create_multi
def create(self, vals_list):
products = super().create(vals_list)
products._set_variants_default_on()
return products

def write(self, vals):
if not vals.get("active", True):
Expand Down
3 changes: 1 addition & 2 deletions connector_prestashop/models/product_product/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,13 @@ def default_code(self, record):
@mapping
def barcode(self, record):
barcode = record.get("barcode") or record.get("ean13")
check_ean = self.env["barcode.nomenclature"].check_ean
if barcode in ["", "0"]:
backend_adapter = self.component(
usage="backend.adapter", model_name="prestashop.product.template"
)
template = backend_adapter.read(record["id_product"])
barcode = template.get("barcode") or template.get("ean13")
if barcode and barcode != "0" and check_ean(barcode):
if barcode and barcode != "0":
return {"barcode": barcode}
return {}

Expand Down
6 changes: 3 additions & 3 deletions connector_prestashop/models/product_template/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,11 +365,11 @@ def taxes_id(self, record):
return {"taxes_id": [(6, 0, taxes.ids)]}

@mapping
def type(self, record):
def detailed_type(self, record):
# The same if the product is a virtual one in prestashop.
if record["type"]["value"] and record["type"]["value"] == "virtual":
return {"type": "service"}
return {"type": "product"}
return {"detailed_type": "service"}
return {"detailed_type": "product"}

# TODO FIXME
# @mapping
Expand Down
30 changes: 16 additions & 14 deletions connector_prestashop/models/sale_order/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,14 @@ class PrestashopSaleOrderLine(models.Model):
index=True,
)

@api.model
def create(self, vals):
ps_sale_order = self.env["prestashop.sale.order"].search(
[("id", "=", vals["prestashop_order_id"])], limit=1
)
vals["order_id"] = ps_sale_order.odoo_id.id
return super().create(vals)
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
ps_sale_order = self.env["prestashop.sale.order"].search(
[("id", "=", vals["prestashop_order_id"])], limit=1
)
vals["order_id"] = ps_sale_order.odoo_id.id
return super().create(vals_list)


class PrestashopSaleOrderLineDiscount(models.Model):
Expand All @@ -176,13 +177,14 @@ class PrestashopSaleOrderLineDiscount(models.Model):
index=True,
)

@api.model
def create(self, vals):
ps_sale_order = self.env["prestashop.sale.order"].search(
[("id", "=", vals["prestashop_order_id"])], limit=1
)
vals["order_id"] = ps_sale_order.odoo_id.id
return super().create(vals)
@api.model_create_multi
def create(self, vals_list):
for vals in vals_list:
ps_sale_order = self.env["prestashop.sale.order"].search(
[("id", "=", vals["prestashop_order_id"])], limit=1
)
vals["order_id"] = ps_sale_order.odoo_id.id
return super().create(vals_list)


class OrderPaymentModel(models.TransientModel):
Expand Down
25 changes: 0 additions & 25 deletions connector_prestashop/models/sale_order/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,17 +315,6 @@ def finalize(self, map_record, values):
for k, v in values.items()
if k in self.env["sale.order"]._fields.keys()
}
sale_vals = self.env["sale.order"].play_onchanges(
sale_vals,
[
"fiscal_position_id",
"partner_id",
"partner_shipping_id",
"partner_invoice_id",
"payment_mode_id",
"workflow_process_id",
],
)
values.update(sale_vals)
presta_line_list = []
for line_vals_command in values["prestashop_order_line_ids"]:
Expand All @@ -337,9 +326,6 @@ def finalize(self, map_record, values):
for k, v in presta_line_vals.items()
if k in self.env["sale.order.line"]._fields.keys()
}
line_vals = self.env["sale.order.line"].play_onchanges(
line_vals, ["product_id"]
)
presta_line_vals.update(line_vals)
presta_line_list.append(
(line_vals_command[0], line_vals_command[1], presta_line_vals)
Expand Down Expand Up @@ -428,21 +414,10 @@ def _add_shipping_line(self, binding):
if self.backend_record.taxes_included
else binding.total_shipping_tax_excluded
)
# when we have a carrier_id, even with a 0.0 price,
# Odoo will adda a shipping line in the SO when the picking
# is done, so we better add the line directly even when the
# price is 0.0
if binding.odoo_id.carrier_id:
binding.odoo_id._create_delivery_line(
binding.odoo_id.carrier_id, shipping_total
)
binding.odoo_id.recompute()

def _create(self, data):
binding = super()._create(data)
if binding.fiscal_position_id:
binding.odoo_id._compute_tax_id()
return binding

def _after_import(self, binding):
res = super()._after_import(binding)
Expand Down
13 changes: 7 additions & 6 deletions connector_prestashop/models/stock_move/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ def get_prestashop_stock_locations(self):
class StockQuant(models.Model):
_inherit = "stock.quant"

@api.model
def create(self, vals):
@api.model_create_multi
def create(self, vals_list):
location_obj = self.env["stock.location"]
ps_locations = location_obj.get_prestashop_stock_locations()
quant = super().create(vals)
if quant.location_id in ps_locations:
quant.product_id.update_prestashop_qty()
return quant
quants = super().create(vals_list)
for quant in quants:
if quant.location_id in ps_locations:
quant.product_id.update_prestashop_qty()
return quants

def write(self, vals):
location_obj = self.env["stock.location"]
Expand Down
16 changes: 5 additions & 11 deletions connector_prestashop/tests/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -260,15 +260,12 @@ def configure_taxes(self):
self.journal = self.env["account.journal"].create(
{"name": "Test journal", "code": "TEST", "type": "general"}
)
income_type = self.env.ref("account.data_account_type_revenue")
expense_type = self.env.ref("account.data_account_type_expenses")
receivable_type = self.env.ref("account.data_account_type_receivable")
self.debit_account = self.env["account.account"].create(
{
"company_id": company.id,
"code": "DB",
"name": "Debit Account",
"user_type_id": income_type.id,
"account_type": "income",
"reconcile": False,
}
)
Expand All @@ -277,7 +274,7 @@ def configure_taxes(self):
"company_id": company.id,
"code": "CR",
"name": "Credit Account",
"user_type_id": expense_type.id,
"account_type": "income",
"reconcile": False,
}
)
Expand All @@ -286,22 +283,19 @@ def configure_taxes(self):
"company_id": company.id,
"code": "RA",
"name": "Receivable Account",
"user_type_id": receivable_type.id,
"account_type": "asset_receivable",
"reconcile": True,
}
)
self.env["ir.property"].search(
[("name", "=", "property_account_receivable_id"), ("res_id", "=", False)]
).value_reference = ("account.account,%s" % self.receivable_account.id)
liabilities_account = self.env.ref(
"account.data_account_type_current_liabilities"
)
self.tax_account = self.env["account.account"].create(
{
"company_id": company.id,
"code": "tax",
"name": "Tax Account",
"user_type_id": liabilities_account.id,
"account_type": "liability_current",
"reconcile": False,
}
)
Expand Down Expand Up @@ -337,7 +331,7 @@ def _create_product_binding(
product = self.env["product.product"].create(
{
"name": name,
"type": "product",
"detailed_type": "product",
}
)
template = product.product_tmpl_id
Expand Down
2 changes: 1 addition & 1 deletion connector_prestashop/tests/test_export_tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def setUp(self):
"picking_type_id": self.ref("stock.picking_type_out"),
"location_id": stock_loc,
"location_dest_id": customer_loc,
"move_lines": [
"move_ids": [
(
0,
0,
Expand Down
6 changes: 3 additions & 3 deletions connector_prestashop/tests/test_import_sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,17 @@ def test_import_sale_batch(self):
def test_import_sale_record(self):
"""Import a sale order"""
# setup for sale order with id 5, create the dependencies
mode_journal = self.env["account.journal"].search([], limit=1)
mode_journal = self.env["account.journal"].search(
[("type", "=", "bank"), ("company_id", "=", self.env.company.id)], limit=1
)
payment_method_xmlid = "account.account_payment_method_manual_in"
payment_method = self.env.ref(payment_method_xmlid)
mode_journal.inbound_payment_method_ids = payment_method
payment_mode = self.env["account.payment.mode"].create(
{
"name": "Bank wire",
"company_id": self.backend_record.company_id.id,
"bank_account_link": "fixed",
"fixed_journal_id": mode_journal.id,
"payment_type": "inbound",
"payment_method_id": payment_method.id,
}
)
Expand Down
2 changes: 1 addition & 1 deletion connector_prestashop/views/account_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
<field name="name" colspan="4" />
<field name="tax_ids" colspan="4" />
<group string="PrestaShop Binding">
<field name="prestashop_bind_ids" nolabel="1" />
<field name="prestashop_bind_ids" nolabel="1" colspan="2" />
</group>
</form>
</field>
Expand Down
2 changes: 1 addition & 1 deletion connector_prestashop/views/image_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<field name="arch" type="xml">
<xpath expr="//field[@name='comments']/.." position="after">
<group string="PrestaShop Binding">
<field name="prestashop_bind_ids" nolabel="1" />
<field name="prestashop_bind_ids" nolabel="1" colspan="2" />
</group>
</xpath>
</field>
Expand Down
Loading

0 comments on commit 0f04ffd

Please sign in to comment.