def addtodb(self): if session["user"] != "admin": return import datetime if request.params['value']: f = float(request.params['value']) else: f = 0.0 r = round(f, 2) newvalue = int(r*100) newitem = Item(rep=int(request.params['rep']), customer=request.params['customer'].upper()[:30], value=newvalue, costcentre=request.params['costcentre'].upper()[:20], ordernum=request.params['ordernum'].upper()[:20], make=request.params['make'].upper()[:30], model=request.params['model'].upper()[:50], part=request.params['part'].upper()[:30], serial=request.params['serial'].upper()[:30], asset=request.params['asset'].upper()[:30], warranty=request.params['warranty'].upper()[:30], issue=request.params['issue'].upper()[:300], state=request.params['state'].upper()[:300], location=request.params['location'].upper()[:300], comment=request.params['comment'].upper()[:300]) if request.params.get('fujitsuowned') == 'yes': newitem.fujitsuowned = True else: newitem.fujitsuowned = False newitem.initials = "AF"; now = datetime.datetime.now() newitem.datein = "%s%s%s" % (now.year, now.month, now.day) Session.add(newitem) Session.commit() fields = ("rep", "datein", "kind", "initials", "customer", "value", "costcentre", "ordernum", "make", "model", "part", "serial", "asset", "warranty", "location", "issue", "state", "solution", "grn", "sap", "rtsdate", "folio", "comment") return dict((field, getattr(newitem, field)) for field in fields)
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')
def addtodb(self): if session["user"] != "admin": return import datetime if request.params["value"]: f = float(request.params["value"]) else: f = 0.0 r = round(f, 2) newvalue = int(r * 100) newitem = Item( rep=int(request.params["rep"]), customer=request.params["customer"].upper()[:30], value=newvalue, costcentre=request.params["costcentre"].upper()[:20], ordernum=request.params["ordernum"].upper()[:20], make=request.params["make"].upper()[:30], model=request.params["model"].upper()[:50], part=request.params["part"].upper()[:30], serial=request.params["serial"].upper()[:30], asset=request.params["asset"].upper()[:30], warranty=request.params["warranty"].upper()[:30], issue=request.params["issue"].upper()[:300], state=request.params["state"].upper()[:300], location=request.params["location"].upper()[:300], comment=request.params["comment"].upper()[:300], ) if request.params.get("fujitsuowned") == "yes": newitem.fujitsuowned = True else: newitem.fujitsuowned = False newitem.initials = "AF" now = datetime.datetime.now() newitem.datein = "%s/%s/%s" % (now.day, now.month, now.year) Session.add(newitem) Session.commit() fields = ( "rep", "datein", "kind", "initials", "customer", "value", "costcentre", "ordernum", "make", "model", "part", "serial", "asset", "warranty", "location", "issue", "state", "solution", "grn", "sap", "rtsdate", "folio", "comment", ) return dict((field, getattr(newitem, field)) for field in fields)