import os import odooly import argparse import pygsheets import pandas as pd def create_values(attribute, df_attributes, odoo_value, values): values_attribute = list(set(df_attributes.to_list())) values_attribute.remove('') if '' in values_attribute else values_attribute for value in values_attribute: value_exist = list(filter(lambda l: l.get('name') == str(value) and l.get('attribute_id')[1] == attribute.get('name'), values)) if value_exist: continue new_value = odoo_value.create({ 'name': str(value), 'attribute_id': attribute.get('id') }) print(f'El valor: {str(new_value.name)}, ha sido creado en el atributo: {new_value.attribute_id}') def main(odoo, filename, args): gc = pygsheets.authorize(service_file=os.path.expanduser('config/0a4abc985039aa.json')) # llave de credenciales para la api ss = gc.open(filename) # Acceder al archivo ws = ss.worksheet_by_title('Atributos') # Ingresa a la hoja especifica df = ws.get_as_df(start=args.primer_atributo, end=args.ultimo_atributo) # Definir el rango de columnas que contienen los atributos !!!! Importante ¡¡¡¡¡¡¡ attributes = list(df.columns) # Obtiene solo los atributos (Titulos) # Consulta los atributos y valores creados en Odoo Attribute = odoo.env['product.attribute'] Value = odoo.env['product.attribute.value'] odoo_attribute = Attribute.search_read([], ['id', 'name']) odoo_value = Value.search_read([], ['id', 'name', 'attribute_id']) #Recorre cada atributo y a su vez los valores, en caso de que no existan los crea for attribute in attributes: exist_attribute = list(filter(lambda l: l.get('name') == attribute, odoo_attribute)) if not exist_attribute: attribute_new = Attribute.create({'name': attribute}) exist_attribute.append({'id': attribute_new.id, 'name': attribute_new.name}) print(f'Nuevo atributo: {attribute}, creado en odoo') if len(exist_attribute) > 1: print(f'El atributo {exist_attribute} tiene mas de un registro duplicado') continue values_odoo = list(filter(lambda l: l.get('attribute_id')[1] == attribute, odoo_value)) create_values(exist_attribute[0], df[attribute], Value, values_odoo) if __name__ == "__main__": parser = argparse.ArgumentParser(description='Crear Atributos y Valores de Drive a Odoo.') parser.add_argument('--primer_atributo', required=True, help='Primera Celda donde se lee los atributos, Ejem: G1') parser.add_argument('--ultimo_atributo', required=True, help='Ultima Celda donde se lee los atributos, Ejem: R') args = parser.parse_args() filename = 'Maestro de Productos' # Valida las credenciales del usuario y determina el servicio a conectarse odooly.Client._config_file = os.path.expanduser('config/odooly.ini') odoo = odooly.Client.from_config('odootest25') main(odoo, filename, args)