def upload(self): """ Really nasty CSV importer that does minimal error checking and expects everything to be laid out in a particular manner. """ import csv csvfile = request.POST['csvfile'] csvreader = csv.reader(csvfile.file, delimiter="\t", quotechar='"') rowsadded = 0 repsadded = [] c.duplicatereps = [] c.unknowncustomers = [] for row in csvreader: rep = row[3][3:][:7] if rep == '': rep = rowsadded # Skip over duplicate REP numbers if rep not in repsadded: newitem = Item(rep=int(rep), kind=row[0].decode('utf-8', 'ignore')[:3], initials=row[1].decode('utf-8', 'ignore')[:3], customer=row[4].decode('utf-8', 'ignore')[:30], costcentre=row[6].decode('utf-8', 'ignore')[:20], ordernum=row[7].decode('utf-8', 'ignore')[:20], make=row[8].decode('utf-8', 'ignore')[:30], model=row[9].decode('utf-8', 'ignore')[:50], part=row[10].decode('utf-8', 'ignore')[:30], serial=row[11].decode('utf-8', 'ignore')[:30], asset=row[12].decode('utf-8', 'ignore')[:30], issue=row[14].decode('utf-8', 'ignore')[:300], state=row[15].decode('utf-8', 'ignore')[:300], location=row[16].decode('utf-8', 'ignore')[:300], solution=row[17].decode('utf-8', 'ignore')[:300], sap=row[18].decode('utf-8', 'ignore')[:8], grn=row[19].decode('utf-8', 'ignore')[:8]) # Sometimes Excel doesn't include the last column (or the CSV # reader doesn't read it properly). if len(row) >= 21: # Sometimes the Folio column has non-numbers in it try: newitem.folio = int(row[21]) except ValueError: newitem.folio = 0 # Ditto for Value column, particularly items added long ago # by a certain "JH". try: f = float(row[5]) except ValueError: f = 0.0 # Store values as integers r = round(f, 2) newitem.value = int(r*100) newitem.datein = parsedate(row[2].decode('utf-8', 'ignore')[:20]) newitem.warranty = parsedate(row[13].decode('utf-8', 'ignore')[:20]) newitem.rtsdate = parsedate(row[20].decode('utf-8', 'ignore')[:20]) # Return custs = ["TESCO", "HSBC", "SPECSAVERS", "HBOS", "RBSG"] def detectcust(cust): for acust in custs: if cust[:len(acust)] == acust: return acust if cust != "UNKNOWN": c.unknowncustomers.append({"rep": newitem.rep, "customer": cust}) return "UNKNOWN" newcust = detectcust(newitem.customer) # If there's a "/" in the customer name, assume it's owned # by Fujitsu if the character after the "/" is an "F" slash = newitem.customer.find("/") if slash != -1: if newitem.customer[slash + 1] == "F": newitem.fujitsuowned = True else: newitem.fujitsuowned = False newitem.customer = newcust Session.add(newitem) rowsadded = rowsadded + 1 repsadded += [rep] else: c.duplicatereps += [rep] print "[dobtrack] Ignoring duplicate rep of %s" % rep Session.commit() print "[dobtrack] Unrecognised 'customer' entries:" print c.unknowncustomers c.rowsadded = rowsadded c.pagetitle = "CSV file uploaded successfully" return render('/upload.mako')