def _import_file(filepath): """ Processes the XLS data, ensures that the part numbers exist (incl. revision) and that all the required fields are set. If something is missing a warning is flashed but processing continues. Returns a tuple (success, headers, data) """ headers, data = read_xls(filepath) success = True if 'partno' not in headers: raise ValueError("'partno' column is missing") if 'serial' not in headers: raise ValueError("'serial' column is missing") if '_id' in headers: raise ValueError("reserved column name: '_id'") if 'comments' in headers: raise ValueError("reserved column name: 'comments'") if 'available' in headers: raise ValueError("reserved column name: 'available'") for idx, item in enumerate(data): try: pn = PartNumber(item.get('partno')) ensure_exists(pn.base_number) if pn.revision is None: raise ValueError('part number requires a revision') serial = item.get('serial') if not serial: raise ValueError('serial number is missing') # transform the serial to a string AFTER checking it exists. Otherwise the string would read 'None' serial = str(serial) if current_app.mongo.db.items.find_one(serial): raise ValueError("serial number '%s' exists already" % serial) reqs = get_requirements(pn) process_requirements(item, reqs) except Exception as e: flash('%s (row %d)' % (e, (idx+2)), 'error') success = False return success, headers, data
def _import_file(filepath): headers, data = read_xls(filepath) success = True if 'quantity' not in headers: raise ValueError("'quantity' column is missing") if 'partno' not in headers: raise ValueError("'partno' column is missing") for idx, item in enumerate(data): try: # the part number must exist # quantity is optional, set as zero if missing ensure_exists(item.get('partno')) qstr = item.get('quantity') if qstr: quantity = int(qstr) else: quantity = 0 if quantity < 0: raise ValueError('quantity must be non-negative') item['quantity'] = quantity except Exception as e: flash('%s (row %d)' % (e, (idx+2)), 'error') success = False return success, headers, data