def run(cls, database=DEFAULT_DB_ALIAS, **kwargs): import frepple # Uncomment the following lines to bypass the connection to odoo and use # a XML flat file alternative. This can be useful for debugging. #with open("my_path/my_data_file.xml", 'rb') as f: # frepple.readXMLdata(f.read().decode('utf-8'), False, False) # frepple.printsize() # return odoo_user = Parameter.getValue("odoo.user", database) if settings.ODOO_PASSWORDS.get(database) == '': odoo_password = Parameter.getValue("odoo.password", database) else: odoo_password = settings.ODOO_PASSWORDS.get(database) odoo_db = Parameter.getValue("odoo.db", database) odoo_url = Parameter.getValue("odoo.url", database) odoo_company = Parameter.getValue("odoo.company", database) ok = True if not odoo_user: print("Missing or invalid parameter odoo.user") ok = False if not odoo_password: print("Missing or invalid parameter odoo.password") ok = False if not odoo_db: print("Missing or invalid parameter odoo.db") ok = False if not odoo_url: print("Missing or invalid parameter odoo.url") ok = False if not odoo_company: print("Missing or invalid parameter odoo.company") ok = False odoo_language = Parameter.getValue("odoo.language", database, 'en_US') if not ok: raise Exception("Odoo connector not configured correctly") # Connect to the odoo URL to GET data url = "%sfrepple/xml?%s" % (odoo_url, urlencode({ 'database': odoo_db, 'language': odoo_language, 'company': odoo_company, 'mode': cls.mode })) try: request = Request(url) encoded = base64.encodestring(('%s:%s' % (odoo_user, odoo_password)).encode('utf-8'))[:-1] request.add_header("Authorization", "Basic %s" % encoded.decode('ascii')) except HTTPError as e: print("Error connecting to odoo at %s: %s" % (url, e)) raise e # Download and parse XML data with urlopen(request) as f: frepple.readXMLdata(f.read().decode('utf-8'), False, False) frepple.printsize()
def odoo_read(db=DEFAULT_DB_ALIAS): ''' This function connects to a URL, authenticates itself using HTTP basic authentication, and then reads data from the URL. The data from the source must adhere to frePPLe's official XML schema, as defined in the schema files bin/frepple.xsd and bin/frepple_core.xsd. ''' odoo_user = Parameter.getValue("odoo.user", db) if settings.OODO_PASSWORDS.get(db) == '': odoo_password = Parameter.getValue("odoo.password", db) else: odoo_password = settings.OODO_PASSWORDS.get(db) odoo_db = Parameter.getValue("odoo.db", db) odoo_url = Parameter.getValue("odoo.url", db) odoo_company = Parameter.getValue("odoo.company", db) ok = True if not odoo_user: print("Missing or invalid parameter odoo.user") ok = False if not odoo_password: print("Missing or invalid parameter odoo.password") ok = False if not odoo_db: print("Missing or invalid parameter odoo.db") ok = False if not odoo_url: print("Missing or invalid parameter odoo.url") ok = False if not odoo_company: print("Missing or invalid parameter odoo.company") ok = False odoo_language = Parameter.getValue("odoo.language", db, 'en_US') if not ok: raise Exception("Odoo connector not configured correctly") # Connect to the odoo URL to GET data f = None try: request = Request("%sfrepple/xml/?%s" % (odoo_url, urlencode({ 'database': odoo_db, 'language': odoo_language, 'company': odoo_company }))) encoded = base64.encodestring(('%s:%s' % (odoo_user, odoo_password)).encode('utf-8'))[:-1] request.add_header("Authorization", "Basic %s" % encoded.decode('ascii')) f = urlopen(request) except HTTPError as e: print("Error connecting to odoo", e) raise e # Download and parse XML data try: frepple.readXMLdata(f.read().decode('utf-8'), False, False) finally: if f: f.close()
frepple.readXMLdata('''<?xml version="1.0" encoding="UTF-8" ?> <plan xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <resources> <resource name="Resource"> <maximum_calendar name="Capacity"> <buckets> <bucket start="2009-01-01T00:00:00"> <value>1</value> </bucket> </buckets> </maximum_calendar> <loads> <load> <operation name="make item - step 1" /> </load> <load> <operation name="make item - step 2" /> </load> </loads> </resource> </resources> <flows> <flow xsi:type="flow_start"> <operation name="delivery end item" /> <buffer name="end item" /> <quantity>-1</quantity> </flow> <flow xsi:type="flow_end"> <operation name="make or buy item" /> <buffer name="end item" /> <quantity>1</quantity> </flow> </flows> </plan> ''')
def odoo_read(db=DEFAULT_DB_ALIAS, mode=1): ''' This function connects to a URL, authenticates itself using HTTP basic authentication, and then reads data from the URL. The data from the source must adhere to frePPLe's official XML schema, as defined in the schema files bin/frepple.xsd and bin/frepple_core.xsd. The mode is pass as an argument: - Mode 1: This mode returns all data that is loaded with every planning run. - Mode 2: This mode returns data that is loaded that changes infrequently and can be transferred during automated scheduled runs at a quiet moment. Which data elements belong to each category is determined in the odoo addon module and can vary between implementations. ''' odoo_user = Parameter.getValue("odoo.user", db) if settings.ODOO_PASSWORDS.get(db) == '': odoo_password = Parameter.getValue("odoo.password", db) else: odoo_password = settings.ODOO_PASSWORDS.get(db) odoo_db = Parameter.getValue("odoo.db", db) odoo_url = Parameter.getValue("odoo.url", db) odoo_company = Parameter.getValue("odoo.company", db) ok = True if not odoo_user: print("Missing or invalid parameter odoo.user") ok = False if not odoo_password: print("Missing or invalid parameter odoo.password") ok = False if not odoo_db: print("Missing or invalid parameter odoo.db") ok = False if not odoo_url: print("Missing or invalid parameter odoo.url") ok = False if not odoo_company: print("Missing or invalid parameter odoo.company") ok = False odoo_language = Parameter.getValue("odoo.language", db, 'en_US') if not ok: raise Exception("Odoo connector not configured correctly") # Connect to the odoo URL to GET data f = None url = "%sfrepple/xml?%s" % (odoo_url, urlencode({ 'database': odoo_db, 'language': odoo_language, 'company': odoo_company, 'mode': mode })) try: request = Request(url) encoded = base64.encodestring(('%s:%s' % (odoo_user, odoo_password)).encode('utf-8'))[:-1] request.add_header("Authorization", "Basic %s" % encoded.decode('ascii')) f = urlopen(request) except HTTPError as e: print("Error connecting to odoo at %s: %s" % (url, e)) raise e # Download and parse XML data try: frepple.readXMLdata(f.read().decode('utf-8'), False, False) finally: if f: f.close()
def run(cls, database=DEFAULT_DB_ALIAS, **kwargs): import frepple # Uncomment the following lines to bypass the connection to odoo and use # a XML flat file alternative. This can be useful for debugging. # with open("my_path/my_data_file.xml", 'rb') as f: # frepple.readXMLdata(f.read().decode('utf-8'), False, False) # frepple.printsize() # return odoo_user = Parameter.getValue("odoo.user", database) odoo_password = settings.ODOO_PASSWORDS.get(database, None) if not settings.ODOO_PASSWORDS.get(database): odoo_password = Parameter.getValue("odoo.password", database) odoo_db = Parameter.getValue("odoo.db", database) odoo_url = Parameter.getValue("odoo.url", database).strip() if not odoo_url.endswith("/"): odoo_url = odoo_url + "/" odoo_company = Parameter.getValue("odoo.company", database) ok = True # Set debugFile=PathToXmlFile if you want frePPLe to read that file # rather than the data at url # else leave it to False debugFile = False # "c:/temp/frepple_data.xml" if not odoo_user and not debugFile: logger.error("Missing or invalid parameter odoo.user") ok = False if not odoo_password and not debugFile: logger.error("Missing or invalid parameter odoo.password") ok = False if not odoo_db and not debugFile: logger.error("Missing or invalid parameter odoo.db") ok = False if not odoo_url and not debugFile: logger.error("Missing or invalid parameter odoo.url") ok = False if not odoo_company and not debugFile: logger.error("Missing or invalid parameter odoo.company") ok = False odoo_language = Parameter.getValue("odoo.language", database, "en_US") if not ok and not debugFile: raise Exception("Odoo connector not configured correctly") # Connect to the odoo URL to GET data try: loglevel = int(Parameter.getValue("odoo.loglevel", database, "0")) except Exception: loglevel = 0 if not debugFile: url = "%sfrepple/xml?%s" % ( odoo_url, urlencode({ "database": odoo_db, "language": odoo_language, "company": odoo_company, "mode": cls.mode, }), ) try: request = Request(url) encoded = base64.encodestring( ("%s:%s" % (odoo_user, odoo_password)).encode("utf-8"))[:-1] request.add_header("Authorization", "Basic %s" % encoded.decode("ascii")) except HTTPError as e: logger.error("Error connecting to odoo at %s: %s" % (url, e)) raise e # Download and parse XML data with urlopen(request) as f: frepple.readXMLdata(f.read().decode("utf-8"), False, False, loglevel) else: # Download and parse XML data with open(debugFile) as f: frepple.readXMLdata(f.read(), False, False, loglevel) # Hierarchy correction: Count how many items/locations/customers have no owner # If we find 2+ then we use All items/All customers/All locations as root # otherwise we assume that the hierarchy is correct rootItem = None for r in frepple.items(): if r.owner is None: if not rootItem: rootItem = r else: rootItem = None break rootLocation = None for r in frepple.locations(): if r.owner is None: if not rootLocation: rootLocation = r else: rootLocation = None break rootCustomer = None for r in frepple.customers(): if r.owner is None: if not rootCustomer: rootCustomer = r else: rootCustomer = None break if not rootItem: rootItem = frepple.item_mts(name="All items", source="odoo_%s" % cls.mode) for r in frepple.items(): if r.owner is None and r != rootItem: r.owner = rootItem if not rootLocation: rootLocation = frepple.location(name="All locations", source="odoo_%s" % cls.mode) for r in frepple.locations(): if r.owner is None and r != rootLocation: r.owner = rootLocation if not rootCustomer: rootCustomer = frepple.customer(name="All customers", source="odoo_%s" % cls.mode) for r in frepple.customers(): if r.owner is None and r != rootCustomer: r.owner = rootCustomer
def run(cls, database=DEFAULT_DB_ALIAS, **kwargs): import frepple # Uncomment the following lines to bypass the connection to odoo and use # a XML flat file alternative. This can be useful for debugging. #with open("my_path/my_data_file.xml", 'rb') as f: # frepple.readXMLdata(f.read().decode('utf-8'), False, False) # frepple.printsize() # return odoo_user = Parameter.getValue("odoo.user", database) if settings.ODOO_PASSWORDS.get(database) == '': odoo_password = Parameter.getValue("odoo.password", database) else: odoo_password = settings.ODOO_PASSWORDS.get(database) odoo_db = Parameter.getValue("odoo.db", database) odoo_url = Parameter.getValue("odoo.url", database) odoo_company = Parameter.getValue("odoo.company", database) ok = True if not odoo_user: logger.error("Missing or invalid parameter odoo.user") ok = False if not odoo_password: logger.error("Missing or invalid parameter odoo.password") ok = False if not odoo_db: logger.error("Missing or invalid parameter odoo.db") ok = False if not odoo_url: logger.error("Missing or invalid parameter odoo.url") ok = False if not odoo_company: logger.error("Missing or invalid parameter odoo.company") ok = False odoo_language = Parameter.getValue("odoo.language", database, 'en_US') if not ok: raise Exception("Odoo connector not configured correctly") # Connect to the odoo URL to GET data url = "%sfrepple/xml?%s" % (odoo_url, urlencode({ 'database': odoo_db, 'language': odoo_language, 'company': odoo_company, 'mode': cls.mode })) try: request = Request(url) encoded = base64.encodestring( ('%s:%s' % (odoo_user, odoo_password)).encode('utf-8'))[:-1] request.add_header("Authorization", "Basic %s" % encoded.decode('ascii')) except HTTPError as e: logger.error("Error connecting to odoo at %s: %s" % (url, e)) raise e # Download and parse XML data with urlopen(request) as f: frepple.readXMLdata(f.read().decode('utf-8'), False, False) frepple.printsize()
def run(cls, database=DEFAULT_DB_ALIAS, **kwargs): import frepple # Uncomment the following lines to bypass the connection to odoo and use # a XML flat file alternative. This can be useful for debugging. # with open("my_path/my_data_file.xml", 'rb') as f: # frepple.readXMLdata(f.read().decode('utf-8'), False, False) # frepple.printsize() # return odoo_user = Parameter.getValue("odoo.user", database) odoo_password = settings.ODOO_PASSWORDS.get(database, None) if not settings.ODOO_PASSWORDS.get(database): odoo_password = Parameter.getValue("odoo.password", database) odoo_db = Parameter.getValue("odoo.db", database) odoo_url = Parameter.getValue("odoo.url", database) odoo_company = Parameter.getValue("odoo.company", database) ok = True if not odoo_user: logger.error("Missing or invalid parameter odoo.user") ok = False if not odoo_password: logger.error("Missing or invalid parameter odoo.password") ok = False if not odoo_db: logger.error("Missing or invalid parameter odoo.db") ok = False if not odoo_url: logger.error("Missing or invalid parameter odoo.url") ok = False if not odoo_company: logger.error("Missing or invalid parameter odoo.company") ok = False odoo_language = Parameter.getValue("odoo.language", database, "en_US") if not ok: raise Exception("Odoo connector not configured correctly") # Assign to single roots root_item = None for r in frepple.items(): if r.owner is None: root_item = r break root_customer = None for r in frepple.customers(): if r.owner is None: root_customer = r break root_location = None for r in frepple.locations(): if r.owner is None: root_location = r break # Connect to the odoo URL to GET data url = "%sfrepple/xml?%s" % ( odoo_url, urlencode( { "database": odoo_db, "language": odoo_language, "company": odoo_company, "mode": cls.mode, } ), ) try: request = Request(url) encoded = base64.encodestring( ("%s:%s" % (odoo_user, odoo_password)).encode("utf-8") )[:-1] request.add_header("Authorization", "Basic %s" % encoded.decode("ascii")) except HTTPError as e: logger.error("Error connecting to odoo at %s: %s" % (url, e)) raise e # Download and parse XML data with urlopen(request) as f: frepple.readXMLdata(f.read().decode("utf-8"), False, False) # Assure single root hierarchies for r in frepple.items(): if r.owner is None and r != root_item: r.owner = root_item for r in frepple.customers(): if r.owner is None and r != root_customer: r.owner = root_customer for r in frepple.locations(): if r.owner is None and r != root_location: r.owner = root_location