Skip to content

Commit

Permalink
Merge pull request #110 from priyankarani/task-7788
Browse files Browse the repository at this point in the history
Reuse import data wizard to import products #7788
  • Loading branch information
tarunbhardwaj committed May 26, 2015
2 parents 7fbacfb + 72e44ee commit 6052ae8
Show file tree
Hide file tree
Showing 13 changed files with 97 additions and 263 deletions.
3 changes: 0 additions & 3 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
ImportMagentoCarriers, ConfigureMagento, ImportStoresStart, FailureStart,
SuccessStart, ExportMagentoOrderStatusStart, ExportMagentoOrderStatus,
UpdateMagentoCatalogStart, UpdateMagentoCatalog,
ImportMagentoCatalogStart, ImportMagentoCatalog,
ExportMagentoCatalogStart, ExportMagentoCatalog,
)
from channel import Channel, MagentoTier
Expand Down Expand Up @@ -62,7 +61,6 @@ def register():
MagentoInstanceCategory,
Product,
ProductPriceTier,
ImportMagentoCatalogStart,
ExportMagentoCatalogStart,
MagentoOrderState,
StockShipmentOut,
Expand All @@ -85,7 +83,6 @@ def register():
ExportMagentoInventory,
ExportMagentoTierPrices,
ExportMagentoShipmentStatus,
ImportMagentoCatalog,
UpdateMagentoCatalog,
ExportMagentoCatalog,
ExportMagentoOrderStatus,
Expand Down
10 changes: 5 additions & 5 deletions bom.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ def find_or_create_bom_for_magento_bundle(cls, order_data):
:return: Found or created BoM's active record
"""
Uom = Pool().get('product.uom')
Product = Pool().get('product.product')
ProductBom = Pool().get('product.product-production.bom')
Channel = Pool().get('sale.channel')

identified_boms = cls.identify_boms_from_magento_data(order_data)

if not identified_boms:
return

channel = Channel.get_current_magento_channel()

for item_id, data in identified_boms.iteritems():
bundle_product = \
Product.find_or_create_using_magento_sku(
data['bundle']['sku']
)
channel.import_product(data['bundle']['sku'])

# It contains a list of tuples, in which the first element is the
# product's active record and second is its quantity in the BoM
child_products = [(
Product.find_or_create_using_magento_sku(
channel.import_product(
each['sku']
), (
float(each['qty_ordered']) /
Expand Down
73 changes: 63 additions & 10 deletions channel.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,13 +270,23 @@ def get_current_magento_channel(cls):
channel_id = Transaction().context.get('current_channel')
if not channel_id:
cls.raise_user_error('missing_magento_channel')
return cls(channel_id)
channel = cls(channel_id)

def import_magento_products(self):
"Import products for this magento channel"
Product = Pool().get('product.template')
# Make sure channel belongs to magento
channel.validate_magento_channel()

self.validate_magento_channel()
return channel

def import_products(self):
"""
Import products for this magento channel
Downstream implementation for channel.import_products
"""
if self.source != 'magento':
return super(Channel, self).import_products()

self.import_category_tree()

with Transaction().set_context({'current_channel': self.id}):
with magento.Product(
Expand All @@ -288,14 +298,57 @@ def import_magento_products(self):

products = []
for magento_product in magento_products:
products.append(
Product.find_or_create_using_magento_data(
magento_product
)
)
products.append(self.import_product(magento_product['sku']))

return map(int, products)

def import_product(self, magento_sku):
"""
Import specific product for this magento channel
Downstream implementation for channel.import_product
"""
Product = Pool().get('product.product')

if self.source != 'magento':
return super(Channel, self).import_products()

product = Product.find_using_magento_sku(magento_sku)

if not product:
# if product is not found get the info from magento and
# delegate to create_using_magento_data
with magento.Product(
self.magento_url, self.magento_api_user,
self.magento_api_key
) as product_api:
product_data = product_api.info(magento_sku)

product = Product.create_using_magento_data(product_data)

return product

def import_category_tree(self):
"""
Imports the category tree and creates categories in a hierarchy same as
that on Magento
:param website: Active record of website
"""
Category = Pool().get('product.category')

self.validate_magento_channel()

with Transaction().set_context({'current_channel': self.id}):
with magento.Category(
self.magento_url, self.magento_api_user,
self.magento_api_key
) as category_api:
category_tree = category_api.tree(
self.magento_root_category_id
)
Category.create_tree_using_magento_data(category_tree)

def import_orders(self):
"""
Downstream implementation of channel.import_orders
Expand Down
5 changes: 2 additions & 3 deletions party.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ def find_or_create_using_magento_id(cls, magento_id):
"""
Channel = Pool().get('sale.channel')

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()

party = cls.find_using_magento_id(magento_id)
if not party:
Expand All @@ -75,7 +74,7 @@ def find_using_magento_id(cls, magento_id):
try:
magento_party, = MagentoParty.search([
('magento_id', '=', magento_id),
('channel', '=', Transaction().context.get('current_channel'))
('channel', '=', Transaction().context['current_channel'])
])
except ValueError:
return None
Expand Down
60 changes: 9 additions & 51 deletions product.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,7 @@ def find_or_create_using_magento_id(

category = cls.find_using_magento_id(magento_id)
if not category:
channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()

with magento.Category(
channel.magento_url, channel.magento_api_user,
Expand All @@ -110,7 +109,7 @@ def find_using_magento_data(cls, category_data):

records = MagentoCategory.search([
('magento_id', '=', int(category_data['category_id'])),
('channel', '=', Transaction().context.get('current_channel'))
('channel', '=', Transaction().context['current_channel'])
])
return records and records[0].category or None

Expand All @@ -127,7 +126,7 @@ def find_using_magento_id(cls, magento_id):

records = MagentoCategory.search([
('magento_id', '=', magento_id),
('channel', '=', Transaction().context.get('current_channel'))
('channel', '=', Transaction().context['current_channel'])
])

return records and records[0].category or None
Expand All @@ -146,7 +145,7 @@ def create_using_magento_data(cls, category_data, parent=None):
'parent': parent,
'magento_ids': [('create', [{
'magento_id': int(category_data['category_id']),
'channel': Transaction().context.get('current_channel'),
'channel': Transaction().context['current_channel'],
}])],
}])

Expand Down Expand Up @@ -226,38 +225,6 @@ def __setup__(cls):
"missing_product_code": 'Product "%s" has a missing code.',
})

@classmethod
def find_or_create_using_magento_sku(cls, magento_sku):
"""
Find or create a product using magento ID. This method looks
for an existing product using the magento ID provided. If found, it
returns the template found, else creates a new one and returns that
:param magento_sku: Product SKU from Magento
:returns: Active record of Product Created
"""
Channel = Pool().get('sale.channel')

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()

# TODO: handle case when same product (SKU matched)
# from different store, then add channel to product listing
product = cls.find_using_magento_sku(magento_sku)

if not product:
# if product is not found get the info from magento and
# delegate to create_using_magento_data
with magento.Product(
channel.magento_url, channel.magento_api_user,
channel.magento_api_key
) as product_api:
product_data = product_api.info(magento_sku)

product = cls.create_using_magento_data(product_data)

return product

@classmethod
def find_using_magento_sku(cls, magento_sku):
"""
Expand Down Expand Up @@ -312,8 +279,7 @@ def extract_product_values_from_data(cls, product_data):
"""
Channel = Pool().get('sale.channel')

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()
return {
'name': product_data.get('name') or
('SKU: ' + product_data.get('sku')),
Expand All @@ -326,10 +292,6 @@ def extract_product_values_from_data(cls, product_data):
'default_uom': channel.default_uom.id,
'salable': True,
'sale_uom': channel.default_uom.id,
'account_expense':
channel.default_account_expense.id,
'account_revenue':
channel.default_account_revenue.id,
}

@classmethod
Expand All @@ -347,8 +309,7 @@ def create_using_magento_data(cls, product_data):
Category = Pool().get('product.category')
Channel = Pool().get('sale.channel')

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()

# Get only the first category from the list of categories
# If no category is found, put product under unclassified category
Expand Down Expand Up @@ -391,8 +352,7 @@ def update_from_magento(self):
Channel = Pool().get('sale.channel')
SaleChannelListing = Pool().get('product.product.channel_listing')

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()

with magento.Product(
channel.magento_url, channel.magento_api_user,
Expand Down Expand Up @@ -463,8 +423,7 @@ def export_to_magento(self, category):
Channel = Pool().get('sale.channel')
SaleChannelListing = Pool().get('product.product.channel_listing')

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()

if not category.magento_ids:
self.raise_user_error(
Expand Down Expand Up @@ -557,8 +516,7 @@ def get_price(self, name):
if not Transaction().context.get('current_channel'):
return 0

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()
product = self.product_listing.product
return channel.price_list.compute(
None, product, product.list_price, self.quantity,
Expand Down
24 changes: 10 additions & 14 deletions sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,7 @@ def create_all_using_magento_data(cls, magento_data):

order_states_to_create = []

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()

for code, name in magento_data.iteritems():
if cls.search([
Expand Down Expand Up @@ -230,7 +229,7 @@ def find_using_magento_data(cls, order_data):
sales = cls.search([
('magento_id', '=', int(order_data['order_id'])),
('channel', '=',
Transaction().context.get('current_channel')),
Transaction().context['current_channel']),
])

return sales and sales[0] or None
Expand All @@ -248,8 +247,7 @@ def get_sale_using_magento_data(cls, order_data):
MagentoOrderState = Pool().get('magento.order_state')
Channel = Pool().get('sale.channel')

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()

currency = Currency.search_using_magento_code(
order_data['order_currency_code']
Expand Down Expand Up @@ -388,17 +386,18 @@ def get_sale_line_using_magento_data(self, item):
Get sale.line data from magento data.
"""
SaleLine = Pool().get('sale.line')
Product = Pool().get('product.product')
ChannelException = Pool().get('channel.exception')
Channel = Pool().get('sale.channel')
Uom = Pool().get('product.uom')

channel = Channel.get_current_magento_channel()

sale_line = None
unit, = Uom.search([('name', '=', 'Unit')])
if not item['parent_item_id']:
# If its a top level product, create it
try:
product = Product.find_or_create_using_magento_sku(item['sku'])
product = channel.import_product(item['sku'])
except xmlrpclib.Fault, exception:
if exception.faultCode == 101:
# Case when product doesnot exist on magento
Expand All @@ -423,7 +422,6 @@ def get_sale_line_using_magento_data(self, item):
'product': product,
})
if item.get('tax_percent') and Decimal(item.get('tax_percent')):
channel = Channel.get_current_magento_channel()
taxes = channel.get_taxes(
Decimal(item['tax_percent']) / 100
)
Expand All @@ -444,8 +442,7 @@ def find_or_create_using_magento_increment_id(cls, order_increment_id):
"""
Channel = Pool().get('sale.channel')

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()

sale = cls.find_using_magento_increment_id(order_increment_id)

Expand Down Expand Up @@ -474,7 +471,7 @@ def find_using_magento_id(cls, order_id):
sales = cls.search([
('magento_id', '=', order_id),
('channel', '=',
Transaction().context.get('current_channel'))
Transaction().context['current_channel'])
])
return sales and sales[0] or None

Expand All @@ -490,7 +487,7 @@ def find_using_magento_increment_id(cls, order_increment_id):
"""
Channel = Pool().get('sale.channel')

channel = Channel(Transaction().context.get('current_channel'))
channel = Channel.get_current_magento_channel()

sales = cls.search([
(
Expand Down Expand Up @@ -676,8 +673,7 @@ def export_tracking_info_to_magento(self):
Channel = Pool().get('sale.channel')
Shipment = Pool().get('stock.shipment.out')

channel = Channel(Transaction().context['current_channel'])
channel.validate_magento_channel()
channel = Channel.get_current_magento_channel()

carriers = MagentoCarrier.search([
('channel', '=', channel.id),
Expand Down

0 comments on commit 6052ae8

Please sign in to comment.