def add2db(csvfile): cmdline = True userinput="yes" randomstring = "" csvdata = csv.reader(open(csvfile), dialect="excel") print addtable(csvdata, cmdline, userinput, randomstring)
def upload(request): """ Upload form with user handling. Can enable/disable functionality with variables at the top of this file. Set them to false to disable. """ if 'confirm' in request.POST: mollist = Molecule.objects.filter(randomstring__contains=request.session.session_key) nummols = len(mollist) for mol in mollist: mol.randomstring = "" mol.save() success = [] success.append(str(nummols) + " molecule(s) were added to the database!") return render(request, 'uploadresult.html', {'success':success}) if 'cancel' in request.POST: Molecule.objects.filter(randomstring__contains=request.session.session_key).delete() success = [] success.append("No molecules were added to the database!") return render(request, 'uploadresult.html', {'success':success}) if 'logout' in request.POST: # Log out the user auth.logout(request) return HttpResponseRedirect("/upload.html") if 'single' in request.POST: form = SubmitSingle(request.POST) form.is_valid() error = [] badsmiles = False randomstring = request.session.session_key #sessionid smiles = form.cleaned_data['smiles'] name = form.cleaned_data['name'] altname = form.cleaned_data['altname'] cas = form.cleaned_data['cas'] storage = form.cleaned_data['storage'] storageID = form.cleaned_data['storageid'] platebarcode = form.cleaned_data['platebarcode'] samplebarcode = form.cleaned_data['samplebarcode'] supplier = form.cleaned_data['supplier'] supplierID = form.cleaned_data['supplierid'] molclass = form.cleaned_data['molclass'] comment = form.cleaned_data['comment'] unit = form.cleaned_data['unit'] amount = form.cleaned_data['amount'] try: pybel.readstring("smi", str(smiles)) except: error.append("Error in SMILES") badsmiles = True if form.is_valid() == False or badsmiles: for item in form.errors: error.append(form[item].errors) return render(request, 'uploadresult.html', {'error':error}) addsingle(name, altname, supplier, supplierID, storage, storageID, unit, amount, cas, smiles, comment, molclass, platebarcode, samplebarcode, randomstring) mollist = Molecule.objects.filter(randomstring__contains=randomstring) return render(request, 'uploadresult.html', {'debugname':name, 'mollist':mollist}) if 'table' in request.POST: form = UploadFileForm(request.POST, request.FILES) form.is_valid() error = [] randomstring = request.session.session_key #sessionid if form.is_valid(): cmdline = False userinput="yes" csvfile = request.FILES['file']#.read() csvdata = csv.reader(csvfile, dialect="excel") addtable(csvdata, cmdline, userinput, randomstring) mollist = Molecule.objects.filter(randomstring__contains=randomstring) return render(request, 'uploadresult.html', {'mollist':mollist}) else: for item in form.errors: error.append(form[item].errors) return render(request, 'uploadresult.html', {'error':error}) if uploadsingle == False and uploadtable == False: return render(request, 'upload.html', {'nogo':'nogo'}) else: if logintoupload == True and not request.user.is_authenticated(): # Serve notice that you need to log in in order to upload form = SubmitSingle() fileform = UploadFileForm() return render(request, 'upload.html', {'form':form, 'fileform':fileform}) else: form = SubmitSingle() fileform = UploadFileForm() return render(request, 'upload.html', {'logedin':'ok', 'single':uploadsingle, 'table':uploadtable, 'form':form, 'fileform':fileform})