def exportLocations(cursor): print("Exporting locations...") starttime = time() cursor.execute("SELECT name FROM location") primary_keys = set([ i[0] for i in cursor.fetchall() ]) cursor.executemany( "insert into location \ (name,description,available_id,category,subcategory,lastmodified) \ values(%s,%s,%s,%s,%s,%s)", [( i.name, i.description, i.available and i.available.name or None, i.category, i.subcategory, timestamp ) for i in frepple.locations() if i.name not in primary_keys ]) cursor.executemany( "update location \ set description=%s, available_id=%s, category=%s, subcategory=%s, lastmodified=%s \ where name=%s", [( i.description, i.available and i.available.name or None, i.category, i.subcategory, timestamp, i.name ) for i in frepple.locations() if i.name in primary_keys ]) cursor.executemany( "update location set owner_id=%s where name=%s", [( i.owner.name, i.name ) for i in frepple.locations() if i.owner ]) transaction.commit(using=database) print('Exported locations in %.2f seconds' % (time() - starttime))
def exportLocations(self, cursor): with transaction.atomic(using=self.database, savepoint=False): print("Exporting locations...") starttime = time() cursor.execute("SELECT name FROM location") primary_keys = set([ i[0] for i in cursor.fetchall() ]) cursor.executemany( "insert into location \ (name,description,available_id,category,subcategory,source,lastmodified) \ values(%s,%s,%s,%s,%s,%s,%s)", [ ( i.name, i.description, i.available and i.available.name or None, i.category, i.subcategory, i.source, self.timestamp ) for i in frepple.locations() if i.name not in primary_keys and (not self.source or self.source == i.source) ]) cursor.executemany( "update location \ set description=%s, available_id=%s, category=%s, subcategory=%s, source=%s, lastmodified=%s \ where name=%s", [ ( i.description, i.available and i.available.name or None, i.category, i.subcategory, i.source, self.timestamp, i.name ) for i in frepple.locations() if i.name in primary_keys and (not self.source or self.source == i.source) ]) cursor.executemany( "update location set owner_id=%s where name=%s", [ (i.owner.name, i.name) for i in frepple.locations() if i.owner and (not self.source or self.source == i.source) ]) print('Exported locations in %.2f seconds' % (time() - starttime))
def printModel(filename): ''' A function that prints out all models to a file. ''' # Open the output file output = open(filename,"wt") # Global settings print("Echoing global settings", file=output) print("Plan name:", frepple.settings.name, file=output) print("Plan description:", frepple.settings.description.encode('utf-8'), file=output) print("Plan current:", frepple.settings.current, file=output) # Solvers print("\nEchoing solvers:", file=output) for b in frepple.solvers(): print(" Solver:", b.name, b.loglevel, getattr(b,'constraints',None), file=output) # Calendars print("\nEchoing calendars:", file=output) for b in frepple.calendars(): print(" Calendar:", b.name, getattr(b,'default',None), file=output) for j in b.buckets: print(" Bucket:", getattr(j,'value',None), j.start, j.end, j.priority, file=output) # Customers print("\nEchoing customers:", file=output) for b in frepple.customers(): print(" Customer:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) # Locations print("\nEchoing locations:", file=output) for b in frepple.locations(): print(" Location:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) # Items print("\nEchoing items:", file=output) for b in frepple.items(): print(" Item:", b.name, b.description, b.category, b.subcategory, b.owner, b.operation, file=output) # Resources print("\nEchoing resources:", file=output) for b in frepple.resources(): print(" Resource:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) for l in b.loads: print(" Load:", l.operation.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.loadplans: print(" Loadplan:", l.operationplan.id, l.operationplan.operation.name, l.quantity, l.startdate, l.enddate, file=output) # Buffers print("\nEchoing buffers:", file=output) for b in frepple.buffers(): print(" Buffer:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) for l in b.flows: print(" Flow:", l.operation.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.flowplans: print(" Flowplan:", l.operationplan.id, l.operationplan.operation.name, l.quantity, l.date, file=output) # Operations print("\nEchoing operations:", file=output) for b in frepple.operations(): print(" Operation:", b.name, b.description, b.category, b.subcategory, file=output) for l in b.loads: print(" Load:", l.resource.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.flows: print(" Flow:", l.buffer.name, l.quantity, l.effective_start, l.effective_end, file=output) if isinstance(b, frepple.operation_alternate): for l in b.alternates: print(" Alternate:", l.name, file=output) if isinstance(b, frepple.operation_routing): for l in b.steps: print(" Step:", l.name, file=output) # Demands print("\nEchoing demands:", file=output) for b in frepple.demands(): print(" Demand:", b.name, b.due, b.item.name, b.quantity, file=output) for i in b.operationplans: print(" Operationplan:", i.id, i.operation.name, i.quantity, i.end, file=output) # Operationplans print("\nEchoing operationplans:", file=output) for b in frepple.operationplans(): print(" Operationplan:", b.operation.name, b.quantity, b.start, b.end, file=output) for s in b.operationplans: print(" ", s.operation.name, s.quantity, s.start, s.end, file=output) # Problems print("\nPrinting problems", file=output) for i in frepple.problems(): print(" Problem:", i.entity, i.name, i.description, i.start, i.end, i.weight, file=output)
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, None) odoo_url = Parameter.getValue("odoo.url", database, "").strip() if not odoo_url.endswith("/"): odoo_url = odoo_url + "/" odoo_company = Parameter.getValue("odoo.company", database, None) 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, encoding="utf-8") 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) 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
def printModel(filename): ''' A function that prints out all models to a file. ''' # Open the output file with open(filename, "wt", encoding='utf-8') as output: # Global settings print("Echoing global settings", file=output) print("Plan name:", frepple.settings.name, file=output) print("Plan description:", frepple.settings.description, file=output) print("Plan current:", frepple.settings.current, file=output) # Solvers print("\nEchoing solvers:", file=output) for b in frepple.solvers(): print(" Solver:", b.name, b.loglevel, getattr(b, 'constraints', None), file=output) # Calendars print("\nEchoing calendars:", file=output) for b in frepple.calendars(): print(" Calendar:", b.name, getattr(b, 'default', None), file=output) for j in b.buckets: print(" Bucket:", getattr(j, 'value', None), j.start, j.end, j.priority, file=output) # Customers print("\nEchoing customers:", file=output) for b in frepple.customers(): print(" Customer:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) # Locations print("\nEchoing locations:", file=output) for b in frepple.locations(): print(" Location:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) # Items print("\nEchoing items:", file=output) for b in frepple.items(): print(" Item:", b.name, b.description, b.category, b.subcategory, b.owner, b.operation, file=output) # Resources print("\nEchoing resources:", file=output) for b in frepple.resources(): print(" Resource:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) for l in b.loads: print(" Load:", l.operation.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.loadplans: print(" Loadplan:", l.operationplan.id, l.operationplan.operation.name, l.quantity, l.startdate, l.enddate, file=output) # Buffers print("\nEchoing buffers:", file=output) for b in frepple.buffers(): print(" Buffer:", b.name, b.description, b.category, b.subcategory, b.owner, file=output) for l in b.flows: print(" Flow:", l.operation.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.flowplans: print(" Flowplan:", l.operationplan.id, l.operationplan.operation.name, l.quantity, l.date, file=output) # Operations print("\nEchoing operations:", file=output) for b in frepple.operations(): print(" Operation:", b.name, b.description, b.category, b.subcategory, file=output) for l in b.loads: print(" Load:", l.resource.name, l.quantity, l.effective_start, l.effective_end, file=output) for l in b.flows: print(" Flow:", l.buffer.name, l.quantity, l.effective_start, l.effective_end, file=output) if isinstance(b, frepple.operation_alternate): for l in b.alternates: print(" Alternate:", l[0].name, l[1], l[2], l[3], file=output) if isinstance(b, frepple.operation_routing): for l in b.steps: print(" Step:", l.name, file=output) # Demands print("\nEchoing demands:", file=output) for b in frepple.demands(): print(" Demand:", b.name, b.due, b.item.name, b.quantity, file=output) for i in b.operationplans: print(" Operationplan:", i.id, i.operation.name, i.quantity, i.end, file=output) # Operationplans print("\nEchoing operationplans:", file=output) for b in frepple.operationplans(): print(" Operationplan:", b.operation.name, b.quantity, b.start, b.end, file=output) for s in b.operationplans: print(" ", s.operation.name, s.quantity, s.start, s.end, file=output) # Problems print("\nPrinting problems", file=output) for i in frepple.problems(): print(" Problem:", i.entity, i.name, i.description, i.start, i.end, i.weight, file=output)