Last active
January 15, 2020 22:53
-
-
Save LuqueDaniel/a300375cb88a54e078aebd2f2b945ec1 to your computer and use it in GitHub Desktop.
Script to copy products images from a source database to a destination database in Odoo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# License AGPLv3 (http://www.gnu.org/licenses/agpl-3.0-standalone.html) | |
from threading import Thread | |
from queue import Queue | |
import odoorpc | |
ODOO_SRC = { | |
'host': '127.0.0.1', | |
'port': 8069, | |
'db': 'odoodb', | |
'login': 'user', | |
'pass': 'password' | |
} | |
ODOO_DST = { | |
'host': '127.0.0.1', | |
'port': 8069, | |
'db': 'odoodb', | |
'login': 'user', | |
'pass': 'password' | |
} | |
# Modify according to needs | |
EXTERNAL_ID_FORMAT = '__export__.product_template_{}' | |
def connect(connect_data: dict) -> odoorpc.ODOO: | |
odoo = odoorpc.ODOO(connect_data['host'], port=connect_data['port']) | |
odoo.login(connect_data['db'], login=connect_data['login'], | |
password=connect_data['pass']) | |
return odoo | |
def get_images(odoo: odoorpc.ODOO): | |
model = odoo.env['product.template'] | |
products = model.search([('image', '!=', False)]) | |
for product in products: | |
Q.put((EXTERNAL_ID_FORMAT.format(product), model.browse(product).image)) | |
Q.put(None) | |
def set_images(odoo: odoorpc.ODOO): | |
for item in iter(Q.get, None): | |
try: | |
product = odoo.env.ref(item[0]) | |
except odoorpc.error.RPCError as err: | |
print(err.info['data']['arguments'][0]) | |
else: | |
if not product.image: | |
print(f"Product: {product.name} has no image.") | |
print("\t==> Setting image") | |
product.image = item[1] | |
else: | |
print(f"Product: {product.name} has an image.") | |
print("\t==> Skipped.") | |
Q.task_done() | |
if __name__ == '__main__': | |
Q = Queue(maxsize=50) | |
O_SRC = connect(ODOO_SRC) | |
O_DST = connect(ODOO_DST) | |
TH1 = Thread(name='Get Images', target=get_images, args=(O_SRC,)) | |
TH2 = Thread(name='Set Images', target=set_images, args=(O_DST,)) | |
TH1.start() | |
TH2.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment