=== modified file 'product_images.py' --- product_images.py 2010-09-27 13:08:21 +0000 +++ product_images.py 2010-11-29 11:35:58 +0000 @@ -101,5 +101,60 @@ self.write(cr, uid, each.id, {'mage_file':result}) result = update_image(result, each) return True + + def unlink(self, cr, uid, ids, context=None): + """ Delete image on Magento before deleting it on OpenERP """ + if not context: + context = {} + + logger = netsvc.Logger() + shop_obj = self.pool.get('sale.shop') + product_obj = self.pool.get('product.product') + + # get all magento shops + shops_ids = shop_obj.search(cr, uid, [('magento_shop', '=', True)]) + + for image in self.browse(cr, uid, ids, context): + # mage_file is the name of the file on magento + if not image.mage_file: + continue + + # we'll delete the image in each magento shop + for shop in shop_obj.browse(cr, uid, shops_ids, context): + # search if the product exists on that shop + # and get the id of the product in magento + magento_product_id = product_obj.oeid_to_extid(cr, + uid, + image.product_id.id, + shop.referential_id.id, + context) + if not magento_product_id: + continue + + # create connection with magento + conn = shop_obj.external_connection(cr, uid, shop.referential_id) + if not conn: + raise osv.except_osv(_('Delete image'), + _("Could not establish connection with Magento.")) + + # get image list on magento for the product + mag_images = conn.call('catalog_product_attribute_media.list', + [ + magento_product_id, + ]) + # check if the image still exists on Magento before drop it + for mag_image in mag_images: + if mag_image['file'] == image.mage_file: + # delete image on Magento + conn.call('catalog_product_attribute_media.remove', + [ + magento_product_id, + image.mage_file, + ]) + break + logger.notifyChannel('ext synchro', + netsvc.LOG_INFO, + "Deleted %s's image: %s" % (image.product_id.name, image.name)) + return super(product_images, self).unlink(cr, uid, ids, context) product_images()