Skip to content

Commit

Permalink
[IMP] product attribute prices
Browse files Browse the repository at this point in the history
- switch to synchronous
- problem : in magento, products created before a change in attribute prices don't change their price. In odoo they do -> can result in prices mismatch
  • Loading branch information
PierrickBrun committed Oct 26, 2017
1 parent 3224dc8 commit a567d00
Show file tree
Hide file tree
Showing 9 changed files with 161 additions and 38 deletions.
1 change: 1 addition & 0 deletions connector_magento_configurable/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
from . import product_attribute
from . import product_attribute_value
from . import product_attribute_line
from . import product_attribute_price
10 changes: 7 additions & 3 deletions connector_magento_configurable/models/configurable.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import logging

from odoo import models
from odoo import tools

from odoo.addons.component.core import Component

Expand All @@ -23,11 +24,14 @@ class ConfigurableBatchImporter(Component):

def run(self, filters=None):
""" Run the synchronization """
# from_date = filters.pop('from_date', None)
from_date = filters.pop('from_date', None)
# to_date = filters.pop('to_date', None)
internal_ids = self.env['magento.product.product'].search(
[('product_type', '=', 'configurable')]
)
['&',
('product_type', '=', 'configurable'),
('write_date', '>', from_date.strftime(
tools.misc.DEFAULT_SERVER_DATETIME_FORMAT)),
])
_logger.info('search for configurable products %s returned %s',
filters, internal_ids)
for internal_id in internal_ids:
Expand Down
14 changes: 1 addition & 13 deletions connector_magento_configurable/models/inherit.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,6 @@
from odoo.addons.component.core import Component


class ProductTemplate(models.Model):
_inherit = 'product.template'

@api.multi
@job
def delayable_unlink(self):
"""
Allows to delay an unlink job.
This is used to delete the orphaned product templates
"""
self.unlink()


class MagentoBackend(models.Model):
_inherit = 'magento.backend'

Expand Down Expand Up @@ -67,4 +54,5 @@ class MagentoConfigurableModelBinder(Component):
'magento.product.attribute',
'magento.product.attribute.value',
'magento.product.attribute.line',
'magento.product.attribute.price',
]
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ProductAttributeBatchImporter(Component):
""" Import the Magento Product Attributes.
"""
_name = 'magento.product.attribute.batch.importer'
_inherit = 'magento.delayed.batch.importer'
_inherit = 'magento.direct.batch.importer'
_apply_on = ['magento.product.attribute']

def run(self, filters=None):
Expand All @@ -19,7 +19,7 @@ def run(self, filters=None):
updated_attributes = self.backend_adapter.list_attributes(
record.default_code)
for attribute in updated_attributes:
self._import_record(attribute, job_options={'priority': 98})
self._import_record(attribute)


class ProductAttributeImporter(Component):
Expand All @@ -40,7 +40,8 @@ def _after_import(self, binding):
self.backend_record,
{
'values': self.magento_record['values'],
'attribute_id': self.magento_record['attribute_id'],
'magento_attribute': binding,
'product_id': self.magento_record['product_id'],
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ProductAttributeLineBatchImporter(Component):
""" Import the Magento Product Attribute Lines.
"""
_name = 'magento.product.attribute.line.batch.importer'
_inherit = 'magento.delayed.batch.importer'
_inherit = 'magento.direct.batch.importer'
_apply_on = ['magento.product.attribute.line']

def _write_product(self, magento_product, tmpl_id, value_ids):
Expand Down Expand Up @@ -42,7 +42,10 @@ def _import_magento_product_attribute_line(self,
value,
record
)
self._import_record(line, job_options={'priority': 100})
self._import_record(line)
self.env['product.template'].search([
('product_variant_ids', '=', False)
]).unlink()

def run(self, filters=None):
""" Run the synchronization """
Expand Down Expand Up @@ -106,9 +109,6 @@ def run(self, magento_record, force=False):
magento_record['external_id'],
force,
)
self.env['product.template'].search([
('product_variant_ids', '=', False)
]).with_delay(priority=101, eta=60).delayable_unlink()


class ProductAttributeLineImportMapper(Component):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# -*- coding: utf-8 -*-
from . import common
from . import importer
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import logging
from odoo import models, fields

_logger = logging.getLogger(__name__)


class MagentoProductAttributePrice(models.Model):
_name = 'magento.product.attribute.price'
_inherit = 'magento.binding'
_inherits = {'product.attribute.price': 'odoo_id'}
_description = 'Magento Product Attribute'

odoo_id = fields.Many2one(
comodel_name='product.attribute.price',
string='Product Attribute',
required=True,
ondelete='cascade')


class ProductAttributePrice(models.Model):
_inherit = 'product.attribute.price'

magento_bind_ids = fields.One2many(
comodel_name='magento.product.attribute.price',
inverse_name='odoo_id',
string="Magento Bindings",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# -*- coding: utf-8 -*-
# Copyright 2017 Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html)

from odoo.addons.component.core import Component
from odoo.addons.connector.components.mapper import mapping
from odoo.addons.connector.exception import MappingError


class ProductAttributePriceBatchImporter(Component):
""" Import the Magento Product Attributes.
"""
_name = 'magento.product.attribute.price.batch.importer'
_inherit = 'magento.direct.batch.importer'
_apply_on = ['magento.product.attribute.price']

def run(self, filters=None):
""" Run the synchronization """
price = filters['price']
price['product_id'] = filters['product_id']
price['magento_value'] = filters['magento_value']
price['external_id'] = price['value_index']
price['external_id'] += '_' + price['product_id']
self._import_record(price)


class ProductAttributePriceImporter(Component):
_name = 'magento.product.attribute.price.importer'
_inherit = 'magento.importer'
_apply_on = ['magento.product.attribute.price']

def _get_magento_data(self, storeview_id=None):
"""
In this case, the magento_record contains all the data to insert
"""
record = self.magento_record
binder = self.binder_for('magento.product.product')
product_binding = binder.to_internal(record['product_id'])

if not product_binding:
raise MappingError("The product with "
"magento id %s is not imported." %
record['product_id'])

record['template'] = product_binding.product_tmpl_id
return record

def run(self, magento_record, force=False):
self.magento_record = magento_record
super(ProductAttributePriceImporter, self).run(
magento_record['external_id'],
force,
)


class ProductAttributePriceImportMapper(Component):
_name = 'magento.product.attribute.price.import.mapper'
_inherit = 'magento.import.mapper'
_apply_on = 'magento.product.attribute.price'

direct = [
('pricing_value', 'price_extra'),
('external_id', 'external_id'),
]

@mapping
def price_extra(self, record):
price = record['pricing_value']
if record['is_percent'] == '1':
price = (float(price)/100) * record['template'].list_price
return {'price_extra': price}

@mapping
def backend_id(self, record):
return {'backend_id': self.backend_record.id}

@mapping
def product_tmpl_id(self, record):
return {
'product_tmpl_id': record['template'].id,
}

@mapping
def value_id(self, record):
if not record.get('magento_value'):
return

value = record.get('magento_value').odoo_id
return {
'value_id': value.id,
'magento_value_id': record.get('magento_value').id,
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ class ProductAttributeValueBatchImporter(Component):
""" Import the Magento Product Attributes.
"""
_name = 'magento.product.attribute.value.batch.importer'
_inherit = 'magento.delayed.batch.importer'
_inherit = 'magento.direct.batch.importer'
_apply_on = ['magento.product.attribute.value']

def run(self, filters=None):
""" Run the synchronization """
for value in filters['values']:
value['attribute_id'] = filters['attribute_id']
self._import_record(value, job_options={'priority': 99})
value['magento_attribute'] = filters['magento_attribute']
value['product_id'] = filters['product_id']
self._import_record(value)


class ProductAttributeValueImporter(Component):
Expand All @@ -32,6 +33,16 @@ def _get_magento_data(self, storeview_id=None):
"""
return self.magento_record

def _after_import(self, binding):
self.env['magento.product.attribute.price'].import_batch(
self.backend_record,
{
'price': self.magento_record,
'magento_value': binding,
'product_id': self.magento_record['product_id']
}
)

def run(self, magento_record, force=False):
self.magento_record = magento_record
super(ProductAttributeValueImporter, self).run(
Expand All @@ -57,18 +68,10 @@ def backend_id(self, record):

@mapping
def attribute_id(self, record):
if not record.get('attribute_id'):
if not record.get('magento_attribute'):
return
binder = self.binder_for('magento.product.attribute')
attribute_binding = binder.to_internal(record['attribute_id'])

if not attribute_binding:
raise MappingError("The product attribute with "
"magento id %s is not imported." %
record['attribute_id'])

parent = attribute_binding.odoo_id
return {
'attribute_id': parent.id,
'magento_attribute_id': attribute_binding.id,
'attribute_id': record.get('magento_attribute').odoo_id.id,
'magento_attribute_id': record.get('magento_attribute').id,
}

0 comments on commit a567d00

Please sign in to comment.