def list(request): # Handle file upload if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(docfile=request.FILES['docfile']) newdoc.save() # Redirect to the document list after POST return HttpResponseRedirect(reverse('list')) else: form = DocumentForm() # A empty, unbound form # Load documents for the list page documents = Document.objects.all() # Render list page with the documents and the form return render(request, 'list.html', {'documents': documents, 'form': form})
def list(request): if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): newdoc = Document(docfile=request.FILES['docfile']) newdoc.save() newdoc = newdoc.docfile.name newdoc = str(newdoc) wb = xlrd.open_workbook(newdoc) sh = wb.sheet_by_index(0) c = 1 while c < len(sh.col(0)): first = sh.col_values(0)[c] second = sh.col_values(1)[c] x = db.cursor() db.set_character_set('utf8') x.execute('SET NAMES utf8;') x.execute('SET CHARACTER SET utf8;') x.execute('SET character_set_connection=utf8;') x.execute( "INSERT INTO testcont_content(title, description) VALUES('%s','%s');" % (first, second)) db.commit() c = c + 1 # Redirect to the document list after POST return HttpResponseRedirect(reverse('upload.views.list')) else: form = DocumentForm() # A empty, unbound form # Load documents for the list page documents = Document.objects.all() # Render list page with the documents and the form return render_to_response('upload/list.html', { 'documents': documents, 'form': form, 'test': test, }, context_instance=RequestContext(request))
def home(request): global location userInput = UserInputForm if 'import' in request.POST: newdoc = Document(docfile=request.FILES['upload']) newdoc.save() location = newdoc.path() # creating S3 bucket connection conn = boto.connect_s3('AKIAIJZ56E33VC2GBG3Q', 'xfSWxuK9uGAsRwtwdJgIPBhiye0Z3ka5oRqRa8FD') bucket = conn.create_bucket('client1.bucket') k = Key(bucket) filename = str(request.FILES['upload']) filenameKey = re.sub('\.txt$', '', filename) print filenameKey k.key = filenameKey k.set_contents_from_filename(location) return HttpResponseRedirect(reverse('upload.views.home')) else: form = DocumentForm() # An empty, unbound form if 'user_input' in request.POST: form = UserInputForm(request.POST) if form.is_valid(): form.save() myfile = open( os.path.dirname(os.path.abspath(upload.__file__)) + "/media/Watchlists/userinput.txt", 'a') myfile.write(request.POST['keyword'] + "\n") myfile.close() location = os.path.dirname(os.path.abspath( upload.__file__)) + "/media/Watchlists/userinput.txt" conn = boto.connect_s3('AKIAIJZ56E33VC2GBG3Q', 'xfSWxuK9uGAsRwtwdJgIPBhiye0Z3ka5oRqRa8FD') bucket = conn.create_bucket('client1.bucket') k = Key(bucket) filenameKey = "userinput" print filenameKey k.key = filenameKey k.set_contents_from_filename(location) return HttpResponseRedirect(reverse('upload.views.home')) else: form = UserInputForm() # Load documents for the list page documents = Document.objects.all() # Rendner list page with the documents and the form return render_to_response('upload/parallax.html', { 'documents': documents, 'form': form, 'userInput': userInput }, context_instance=RequestContext(request))