def upload(self): """ VIEW: Upload page """ if session.get('user_name'): form = FileForm() if request.method == 'POST': if form.validate(): try: file = form.file.data file_name = secure_filename(file.filename) if form.path.data: if os.path.isdir(form.path.data): file.save(form.path.data, file_name) else: os.makedirs(form.path.data) file.save(form.path.data, file_name) else: file.save( os.path.join(app.config['UPLOAD_FOLDER'], file_name)) flash('File uploaded successfully.', 'success') except Exception as exc: flash('File upload failed: ' + str(exc), 'danger') else: flash('Invalid file!', 'warning') return self.render('admin/upload.html', form=form) else: return redirect(url_for('view_index'))
def import_bookmark(request, template_name='bookmark/import_bookmark.html'): file_form = FileForm(request.POST or None, request.FILES or None) if request.method == 'POST' and file_form.is_valid(): file = request.FILES['file'] tags_input = file_form.cleaned_data['tags'] public = file_form.cleaned_data['public'] try: bookmarks = parse_firefox_bookmark(file) for bk in bookmarks: try: bookmark = Bookmark.objects.create(url=bk['url'], title=bk['title'], owner=request.user, public=public) if tags_input: tags = tags_input else: tags = parse_tags(bk['tags']) for tag in tags: bookmark.tags.add(tag) # Update the screen shot update_bk_screen_shot_async(bookmark) except Exception: pass return HttpResponseRedirect(reverse("my_bookmark")) except Exception, e: file_form._errors[NON_FIELD_ERRORS] = ErrorList([e.message])
def upload(req): up_files = 'yes' if req.method == "POST": ff = FileForm(req.POST, req.FILES) folder_name = req.session.get('folder_name') if ff.is_valid(): #获取表单数据 b = ' Byte' file_name = ff.cleaned_data['file_name'] file_loc = ff.cleaned_data['file_loc'] file_handel = req.FILES['file_loc'] file_size = str(file_handel.size) + b file_type_split1 = str(file_handel.name) file_type_split2 = file_type_split1.split('.') file_type = file_type_split2[1] #写入数据库 upload_time = timezone.now() f = Folder.objects.get(folder_name=folder_name) f.modify_time = upload_time f.save() f.file_set.create(file_name=file_name, file_loc=file_loc, file_size=file_size, file_type=file_type, upload_time=upload_time) return HttpResponseRedirect('/online/index/') else: ff = FileForm() return render(req, 'create_folder_new.html', { 'ff': ff, 'up_files': up_files })
def upload_attachment(request, projectID): if 'file' not in request.FILES: raise IOError("Missing file") if request.FILES['file'].size > 10*1024*1024: raise IOError("File too large") form = FileForm(request.POST, request.FILES) if(form.is_valid()): #set up parameters for database insert fileObj = request.FILES['file'] projID = project_api.get_project(projectID) fname = fileObj.name fileUUID = str(uuid.uuid4()) #rename file object to have UUID as name to avoid conflicts when retrieving files fileObj.name=fileUUID f = ProjectFile(file=fileObj, project=projID, name=fname, uuid=fileUUID) f.save() return redirect(request.POST['referer'])
def upload(req): up_files = 'yes' if req.method == "POST": ff = FileForm(req.POST, req.FILES) folder_name = req.session.get('folder_name') if ff.is_valid(): #获取表单数据 b = ' Byte' file_name = ff.cleaned_data['file_name'] file_loc = ff.cleaned_data['file_loc'] file_handel = req.FILES['file_loc'] file_size = str(file_handel.size) + b file_type_split1 = str(file_handel.name) file_type_split2 = file_type_split1.split('.') file_type = file_type_split2[1] #写入数据库 upload_time = timezone.now() f = Folder.objects.get(folder_name=folder_name) f.modify_time = upload_time f.save() f.file_set.create(file_name=file_name, file_loc=file_loc, file_size=file_size, file_type=file_type, upload_time=upload_time) return HttpResponseRedirect('/online/index/') else: ff = FileForm() return render(req, 'create_folder_new.html', {'ff': ff, 'up_files': up_files})
def edit_photos(): upload = FileForm() set_loop = Set.query.order_by(Set.id.desc()).all() if upload.validate_on_submit(): for loop in set_loop: if request.form["select_set"] == loop.tag: file = request.files['file'] bucket.put_object(Key='Tractor Show/' + loop.name + '/' + file.filename, Body=file) newFile = Images(name=file.filename, set=loop) db.session.add(newFile) db.session.commit() return redirect('/dashboard/photos#' + loop.tag) add_set = AddSet() if add_set.validate_on_submit(): render_tag = tag_generator(request.form["set"]) new_set = Set(name=request.form["set"], tag=render_tag) db.session.add(new_set) db.session.commit() return redirect(url_for('edit_photos')) return render_template('authentication/edit-photos.html', upload=upload, add_set=add_set, set_loop=set_loop, s3projects=s3projects)
def upload_attachment(request, projectID): if 'file' not in request.FILES: raise IOError("Missing file") if request.FILES['file'].size > 10 * 1024 * 1024: raise IOError("File too large") form = FileForm(request.POST, request.FILES) if (form.is_valid()): #set up parameters for database insert fileObj = request.FILES['file'] projID = project_api.get_project(projectID) fname = fileObj.name fileUUID = str(uuid.uuid4()) #rename file object to have UUID as name to avoid conflicts when retrieving files fileObj.name = fileUUID f = ProjectFile(file=fileObj, project=projID, name=fname, uuid=fileUUID) f.save() return redirect(request.POST['referer'])
def investing(request): # Handle file upload file_form = FileForm(request.POST) form = DocumentForm(request.POST, request.FILES) if request.method == 'POST' and "uploadFile" in request.POST: # Upload if form.is_valid(): newdoc = Document(docfile = request.FILES['docfile']) newdoc.save() # Redirect to the document list after POST return HttpResponseRedirect(reverse('dataloader.views.investing')) elif request.method == 'POST' and "invest" in request.POST: # Make Investments if file_form.is_valid(): file_name = file_form.cleaned_data['file_input'] print "### FILE FOR INVESTMENTS: " + file_name file_path = settings.MEDIA_ROOT + "/" + file_name print file_path cr = csv.reader(open(file_path)) # Starting from second row for row in cr: if row[0] == 'fund' or row[0] == 'individual': investment_manager.fund(row) elif row[0] == 'buy': investment_manager.buy(row) elif row[0] == 'sell': investment_manager.sell(row) elif row[0] == 'sellbuy': investment_manager.sellbuy(row) return HttpResponseRedirect(reverse('dataloader.views.investing')) elif request.method == 'POST' and "clear" in request.POST: # Clear Investments print "### This should clear everything" Activity.objects.all().delete() StakeHold.objects.all().delete() Port_Indi.objects.all().delete() print "### OK check porfolio page" else: form = DocumentForm() # A empty, unbound form file_form = FileForm() # Load documents for the list page documents = Document.objects.all() return render_to_response( 'nav/investing.html', {'documents': documents, 'form': form, 'file_form':file_form}, context_instance=RequestContext(request) )
def hello(): form = FileForm() if form.validate_on_submit(): f = request.files['file'] f.save(secure_filename('test.pdf')) Parse() flash('Parsed successfully', 'success') return redirect('/') return render_template('index.html', title='Pdf Parser', form=form)
def update_log(request): if request.method == "POST": form = FileForm(request.POST, request.FILES) if form.is_valid(): cd = form.cleaned_data handle_uploaded_file(cd) return HttpResponse("Ok: File %s update success." % cd['name']) else: return HttpResponseBadRequest("<h1>Need more arguments</h1>") else: return HttpResponseBadRequest("<h1>Wrong request type</h1>")
def index(): form = FileForm() if form.validate_on_submit(): filename = secure_filename(form.file.data.filename) print(filename) if filename.split('.')[1] in ALLOWED_EXT: form.file.data.save('uploads/' + filename) else: flash('Incorrect format selected, please try again!') return redirect(url_for('index')) return render_template('index.html', form=form)
def upload_file(request, upload_path=None, upload_link=None): form = FileForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = form.cleaned_data['file'] path = os.path.join(upload_path or UPLOAD_PATH, uploaded_file.name) image_path = default_storage.save(path, uploaded_file) image_url = default_storage.url(image_path) if upload_link: return HttpResponse(image_url) return HttpResponse(json.dumps({'filelink': image_url, 'filename': uploaded_file.name})) return HttpResponseForbidden()
def upload_attachment(request, projectID): if 'file' not in request.FILES: raise IOError("Missing file") if request.FILES['file'].size > 1100000: raise IOError("File too large") form = FileForm(request.POST, request.FILES) if(form.is_valid()): file = request.FILES['file'] f = ProjectFile(project = project_api.get_project(projectID), file = file, name = file.name) f.save() return redirect(request.POST['referer'])
def upload(request): # Handle file upload if request.method == 'POST': form = FileForm(request.POST, request.FILES) if form.is_valid(): logger.debug('File with type ' + request.FILES['docfile'].content_type + ' uploaded.') if request.FILES[ 'docfile'].content_type == u'application/vnd.oasis.opendocument.text': file_name = saveFile(request.FILES['docfile']) logger.info('Converting file ' + file_name) content = subprocess.check_output([ 'OdtConverter/odt2html', '-x', 'OdtConverter/odt2html.xsl', 'EditorApp/static/media/' + file_name ]) logger.info('Deleting file ' + file_name) os.remove('EditorApp/static/media/' + file_name) logger.info('Saving imported document ' + file_name) create_document(None, file_name, content) elif request.FILES['docfile'].content_type[:5] == u'image': saveFile(request.FILES['docfile']) else: form = FileForm() form.error = 'Invalid type of document.' logger.error('Wrong type of document.') return render(request, 'upload.html', {'form': form}) # Redirect to the document list after POST return HttpResponseRedirect('/') else: form = FileForm() # A empty, unbound form return render(request, 'upload.html', {'form': form})
def upload_file(request, upload_path=None, upload_link=None): form = FileForm(request.POST, request.FILES) if form.is_valid(): uploaded_file = form.cleaned_data['file'] file_name, extension = os.path.splitext(uploaded_file.name) timestr = time.strftime(u"%Y%m%d-%H%M%S") full_name = u'{0}{1}'.format(file_name+timestr, extension) path = os.path.join(upload_path or UPLOAD_PATH, full_name) image_path = default_storage.save(path, uploaded_file) image_url = default_storage.url(image_path) if upload_link: return HttpResponse(image_url) return HttpResponse(json.dumps({'filelink': image_url, 'filename': uploaded_file.name})) return HttpResponseForbidden()
def home(): feature_form = FeatureForm() file_form = FileForm() with open ("static/final_svm_model.pkl",'rb') as pickle_file: model = pickle.load(pickle_file) if feature_form.validate_on_submit(): sepal_length= feature_form.sepal_length.data sepal_width = feature_form.sepal_width.data petal_length = feature_form.petal_length.data petal_width = feature_form.petal_width.data data = np.array([sepal_length,sepal_width, petal_length, petal_width]).reshape(1,-1) prediction= model.predict(data) predict_result = "" if prediction == 0: predict_result = 'setosa' elif prediction == 1: predict_result = 'versicolor' else : predict_result = 'virginica' #image_file_name = str(uuid.uuid4()) #image_file = image_file_name + ".png" #search_image(predict_result, image_file) return render_template("home.html", feature_form=feature_form, file_form = file_form, result = predict_result) elif file_form.validate_on_submit(): file = file_form.test_file.data #FileStorage object try: data= pd.read_csv(file,sep=' ', delimiter=r"\s+",index_col=False) prediction = model.predict(data) except Exception: return "Something went worng. PLEASE CHECK THE FILE AGAIN" prediction = prediction.astype(str) prediction[prediction == '0'] = 'setosa' prediction[prediction == '1'] = 'versicolor' prediction[prediction == '2'] = 'virginica' prediction_file_name = str(uuid.uuid4()) predict_result_file = prediction_file_name + ".txt" np.savetxt("static/prediction/"+ predict_result_file, prediction, newline="\r\n" , fmt="%s") return redirect(url_for('predict_txt', filename=predict_result_file)) return render_template("home.html", feature_form=feature_form, file_form = file_form)
def fsubmit(): error = None form = FileForm(request.form) if request.method == "POST": print(form.validate()) print("file submitted : ", form.file) print("name: ", request.files["file"]) # print("url submitted : " ,form.q.data) print(request.files["file"].filename) inp(request.files["file"].filename) elif (request.method == "POST"): error = "WRONG FILE!!!" else: return render_template("uploader.html", form=form) return render_template("uploader.html", form=form, error=error)
def create(request): # return render(request, 'files/create.html', {"foo": "bar"}) print 'entrou no create' if request.method == 'POST': form = FileForm(request.POST, request.FILES) print 'request is post!' print form.errors if form.is_valid(): print 'form is valid!' # print 'analysis name should be ', form.cleaned_data['name'] # file.name = form.cleaned_data['name'] file = File.objects.create(user=request.user) file.file = request.FILES.get('file') file.name = str(os.path.splitext(file.file.name)[0]) file.user = request.user file.save() f = file.file #fix permissions #os.chmod("%s/genomes/%s/" % (settings.BASE_DIR, fastafile.user), 0777) # os.chmod("%s/uploads/%s/%s" % (settings.BASE_DIR, fastafile.user, fastafile.id), 0777) #Align.delay(analysis.id) data = { 'files': [{ 'deleteType': 'DELETE', 'name': file.name, 'url': '', 'thumbnailUrl': '', 'type': 'image/png', 'deleteUrl': '', 'size': f.size }] } response = JSONResponse(data, mimetype=response_mimetype(request)) response['Content-Disposition'] = 'inline; filename=files.json' return response else: form = FileForm() return render(request, 'files/create.html', {'form': form})
def files(): result = [] form = FileForm() files = File.query.all() for file in files: result.append([file.file_name, file.file_link]) return render_template('files.html', rows=result, form=form)
def add_containt(request): if request.user.is_authenticated(): args = {} if request.POST: try: request.POST['file_field'] file_form = FileForm(request.POST, request.FILES) if file_form.is_valid(): file_name = request.FILES['file'] media_containt = Media_containt(file=file_name) media_containt.save() pdf_file_name = str(file_name).replace(' ', '_') image_file_name = str(file_name).replace(' ', '_').split('.') image_file_name[-1] = 'png' image_file_name = '.'.join(image_file_name) cmd = 'gs -o '+media_files_url+image_file_name+' -sDEVICE=pngalpha -dLastPage=1 '+media_files_url+pdf_file_name print cmd subprocess.call(cmd, shell=True) print 'save and redirect' return HttpResponseRedirect('/elab/add/containt/') else: containt_form = ContaintForm() args['containt_form'] = containt_form args['file_form'] = file_form print file_form.errors return render_to_response('html_template/admin_template/admin_pannel.html', args) except Exception, e: print "Exception-->", e containt_form = ContaintForm(request.POST) if containt_form.is_valid(): containt_form.save() return HttpResponseRedirect('/elab/add/containt/') else: file_form = FileForm() args['containt_form'] = containt_form args['file_form'] = file_form print containt_form.errors return render_to_response('html_template/admin_template/admin_pannel.html', args) else: containt_form = ContaintForm() file_form = FileForm() args['containt_form'] = containt_form args['file_form'] = file_form return render_to_response('html_template/admin_template/admin_pannel.html', args)
def list_files(rctx): dir_ = os.path.join(rctx.conf.MEDIA, 'stored') if not os.path.isdir(dir_): os.makedirs(dir_) files = os.listdir(dir_) form = FileForm({}) ui = HtmlUI(from_fields=True, engine=rctx.vals.jinja_env) return dict(files=files, url='/media/stored/', form=form, ui=ui)
def post_file(env, data): dir_ = prepair_dir(env) files = os.listdir(dir_) form = FileForm(env) if form.accept(env.request.POST): tmp_file = form.python_data['file'] if tmp_file is not None: new_path = os.path.join(dir_, tmp_file.file_name) os.rename(tmp_file.path, new_path) raise HTTPSeeOther(location=env.request.url) return env.template.render_to_response('index', { 'files':files, 'url':'/media/stored/', 'form':form })
def post_file(env, data): dir_ = prepair_dir(env) files = os.listdir(dir_) form = FileForm(env) if form.accept(env.request.POST): tmp_file = form.python_data['file'] if tmp_file is not None: new_path = os.path.join(dir_, tmp_file.file_name) os.rename(tmp_file.path, new_path) raise HTTPSeeOther(location=env.request.url) return env.template.render_to_response('index', { 'files': files, 'url': '/media/stored/', 'form': form })
def insert_file(): form = FileForm() name = form.name.data link = form.link.data file = File(file_name=name, file_link=link) db.session.add(file) db.session.commit() return redirect('/file')
def attach_file(request,content_type_id,pk): ct = ContentType.objects.get(id=content_type_id) klass = ct.model_class() obj = klass.objects.get(pk=pk) if request.method == 'GET': form = FileForm() elif request.method == 'POST': form = FileForm(request.POST,request.FILES) if form.is_valid(): obj = form.save(commit=False) obj.uploaded_by=request.user obj.object_id = pk obj.content_type = ct obj.name = obj.file.name obj.save() return Response(FileSerializer(obj).data) else: return Response({'errors':form.errors},status=404)
def list_files(env, data): dir_ = prepair_dir(env) files = os.listdir(dir_) form = FileForm(env) return env.template.render_to_response('index', { 'files': files, 'url': '/media/stored/', 'form': form })
def get_attachments(request, projectID): form = FileForm() context = { 'form': form, 'projectID': projectID, 'referer': request.META['HTTP_REFERER'], 'modalID': 'projAttachModal', 'files': ProjectFile.objects.filter(project__id=projectID) } return render(request, 'Attachments.html', context)
def my_bookmark(request, template_name='bookmark/my_bookmark.html'): form = BookmarkForm() file_form = FileForm() return render_to_response( template_name, RequestContext(request, { 'form': form, 'file_form': file_form, 'active': 'my_bookmark' }))
def batch_import_bulletin_by_excel(request): if request.method == 'POST': form = FileForm(request.POST, request.FILES) if form.is_valid(): if request.FILES['filefield'].name.split('.')[-1] == 'xlsx': try: utils.handle_uploaded_file(request, f=request.FILES['filefield']) except ObjectDoesNotExist, e: error_message = '科室和医生组合不存在,请检查所上传表格内容后重新上传' return render(request, 'publisher/file.html', {'form': form, 'error_message': error_message, 'loginname': request.session['loginname']}) return render(request, 'publisher/file.html', {'form': form, 'error_message': '上传成功!', 'loginname': request.session['loginname']}) else: error_message = '文件格式错误,请上传Excel文件(.xlsl)' form = FileForm() return render(request, 'publisher/file.html', {'form': form, 'error_message': error_message, 'loginname': request.session['loginname']})
def upload_gl(request): if request.method != "POST": forecast_obj = Forecast.objects.get(id=request.GET.get('forecast')) context = {'file_category': 'GL Projections'} context['file_type'] = '.json' context['obj_label'] = forecast_obj.label form = FileForm() context['form'] = form return render_to_response('forecasts/upload_gl.html', RequestContext(request, context)) else: form = FileForm(request.POST, request.FILES) if form.is_valid(): upload = request.FILES.values()[0] file_name = upload._name data = json.loads(upload.file.read()) forecast_obj = Forecast.objects.get(id=request.GET.get('forecast')) forecast_obj.projections = data forecast_obj.save() return HttpResponseRedirect('/forecasts/forecast/%s' % forecast_obj.id)
def file_edit(id): fileobj = g.db.files.find_one({'_id': id}) if fileobj is None: abort(404) if fileobj.get('owner') != g.username or not (g.is_admin): # abort if owner doesn't match current user or current is not admin abort(403) form = FileForm() if form.validate_on_submit(): fileobj = form2object(form, fileobj) flash('not implemented', category="warning") if request.method == 'GET': # get the data from the page-object into the form form = object2form(fileobj, form) return render_template('generic_form.html', form=form, title='Edit File')
def upload_file(request, **config): file_type = config['file_type'] context = {'file_type': file_type,} form = FileForm(request.POST, request.FILES) if form.is_valid(): upload = request.FILES.values()[0] file_name = upload._name file_name_with_timestamp = save_file(upload) company = utils.get_company(request) model = config.pop('model') unique = config.pop('unique') name_cleaner_path = config.pop('name_cleaner') value_cleaner_path = config.pop('value_cleaner') exclude_path = config.pop('exclude') post_process_path = config.pop('post_process') #result is zero if it was able to start the background task successfully. result, out, err = process_incoming_file(model, unique, name_cleaner, value_cleaner, exclude, post_process, file_type=config['file_type'], file_name=file_name_with_timestamp, company=company, task_title="%s data for %s" % (file_type, company), task_success_url=reverse('upload_complete'), ) if result != 0: context.update({'file_name': file_name, 'success': False, 'out': out, 'err': err}) messages.error(request, 'Could not process the file provided, please see below') return render(request, 'uploaded.html', context) else: return HttpResponseRedirect(reverse('task_result', kwargs={'pid':out,})) else: context.update({'file_name': file_name, 'success': False, 'out': out, 'err': err}) messages.error(request, 'Could not process the file provided, please see below') return render(request, 'uploaded.html', context)
def teacher_resource(id): course = Course.query.get(id) resources = course.resources.all() form = FileForm() #:如果当前登录的教师没有教授这门课程,则禁止访问 if current_user.id != course.teacher.id: abort(403) if form.validate_on_submit(): name = form.file.data.filename addr = 'upload/' + str(id) + '/' + 'resource/' url = upload( form.file.data, ['zip', 'rar', 'doc', 'txt', 'docx', 'pdf', 'ppt', 'pptx'], addr) if url is None: flash( u'请选择正确的文件格式上传文件,文件格式可以是["zip", "rar", "doc", "txt", "docx", "pdf", "ppt", "pptx"]' ) return render_template('teacherResource.html', form=form, course=course, resources=resources) resource = Resource.query.filter( and_(Resource.course_id == id, Resource.name == name)).first() #:如果是同名文件,则覆盖,即删除原文件,保留新文件 if resource is not None: #:删除原文件 if os.path.exists(resource.url): os.remove(resource.url) resource.url = url db.session.commit() else: resource = Resource(name=name, url=url, course_id=id) db.session.add(resource) db.session.commit() flash(u'文件上传成功') return redirect(url_for('teacher_resource', id=id)) return render_template('teacherResource.html', form=form, course=course, resources=resources)
def file_upload_typed(request, folder_id=None, response_format='html'): "File upload to preselected folder" folder = None if folder_id: folder = get_object_or_404(Folder, pk=folder_id) if not request.user.profile.has_permission(folder, mode='x'): folder = None if request.POST: if 'cancel' not in request.POST: form = FileForm( request.user.profile, folder_id, request.POST, request.FILES) if form.is_valid(): file = form.save() file.set_user_from_request(request) return HttpResponseRedirect(reverse('documents_file_view', args=[file.id])) else: return HttpResponseRedirect(reverse('document_index')) else: form = FileForm(request.user.profile, folder_id) context = _get_default_context(request) context.update({'form': form, 'folder': folder}) return render_to_response('documents/file_upload_typed', context, context_instance=RequestContext(request), response_format=response_format)
def file_edit(request, file_id, response_format='html'): "File edit page" file = get_object_or_404(File, pk=file_id) if not request.user.profile.has_permission(file, mode='w'): return user_denied(request, message="You don't have access to this File") if request.POST: if 'cancel' not in request.POST: form = FileForm( request.user.profile, None, request.POST, request.FILES, instance=file) if form.is_valid(): file = form.save() return HttpResponseRedirect(reverse('documents_file_view', args=[file.id])) else: return HttpResponseRedirect(reverse('documents_file_view', args=[file.id])) else: form = FileForm(request.user.profile, None, instance=file) context = _get_default_context(request) context.update({'form': form, 'file': file}) return render_to_response('documents/file_edit', context, context_instance=RequestContext(request), response_format=response_format)
def create(request): # return render(request, 'files/create.html', {"foo": "bar"}) print 'entrou no create' if request.method == 'POST': form = FileForm(request.POST, request.FILES) print 'request is post!' print form.errors if form.is_valid(): print 'form is valid!' # print 'analysis name should be ', form.cleaned_data['name'] # file.name = form.cleaned_data['name'] file = File.objects.create(user=request.user) file.file= request.FILES.get('file') file.name= str(os.path.splitext(file.file.name)[0]) file.user = request.user file.save() f = file.file #fix permissions #os.chmod("%s/genomes/%s/" % (settings.BASE_DIR, fastafile.user), 0777) # os.chmod("%s/uploads/%s/%s" % (settings.BASE_DIR, fastafile.user, fastafile.id), 0777) #Align.delay(analysis.id) data = {'files': [{'deleteType': 'DELETE', 'name': file.name, 'url': '', 'thumbnailUrl': '', 'type': 'image/png', 'deleteUrl': '', 'size': f.size}]} response = JSONResponse(data, mimetype=response_mimetype(request)) response['Content-Disposition'] = 'inline; filename=files.json' return response else: form = FileForm() return render(request, 'files/create.html', {'form':form})
def import_bookmark(request, template_name='bookmark/import_bookmark.html'): file_form = FileForm(request.POST or None, request.FILES or None) if request.method == 'POST' and file_form.is_valid(): file = request.FILES['file'] tags_input = file_form.cleaned_data['tags'] public =file_form.cleaned_data['public'] try: bookmarks = parse_firefox_bookmark(file) for bk in bookmarks: try: bookmark = Bookmark.objects.create(url=bk['url'], title=bk['title'], owner=request.user, public=public) if tags_input: tags = tags_input else: tags=parse_tags(bk['tags']) for tag in tags: bookmark.tags.add(tag) # Update the screen shot update_bk_screen_shot_async(bookmark) except Exception: pass return HttpResponseRedirect(reverse("my_bookmark")) except Exception, e: file_form._errors[NON_FIELD_ERRORS] = ErrorList([e.message])
def add_containt(request): if request.user.is_authenticated(): args = {} if request.POST: try: request.POST['file_field'] file_form = FileForm(request.POST, request.FILES) if file_form.is_valid(): file_name = request.FILES['file'] media_containt = Media_containt(file=file_name) media_containt.save() pdf_file_name = str(file_name).replace(' ', '_') image_file_name = str(file_name).replace(' ', '_').split('.') image_file_name[-1] = 'png' image_file_name = '.'.join(image_file_name) cmd = 'gs -o ' + media_files_url + image_file_name + ' -sDEVICE=pngalpha -dLastPage=1 ' + media_files_url + pdf_file_name print cmd subprocess.call(cmd, shell=True) print 'save and redirect' return HttpResponseRedirect('/elab/add/containt/') else: containt_form = ContaintForm() args['containt_form'] = containt_form args['file_form'] = file_form print file_form.errors return render_to_response( 'html_template/admin_template/admin_pannel.html', args) except Exception, e: print "Exception-->", e containt_form = ContaintForm(request.POST) if containt_form.is_valid(): containt_form.save() return HttpResponseRedirect('/elab/add/containt/') else: file_form = FileForm() args['containt_form'] = containt_form args['file_form'] = file_form print containt_form.errors return render_to_response( 'html_template/admin_template/admin_pannel.html', args) else: containt_form = ContaintForm() file_form = FileForm() args['containt_form'] = containt_form args['file_form'] = file_form return render_to_response( 'html_template/admin_template/admin_pannel.html', args)
def batch_import_bulletin_by_excel(request): if request.method == 'POST': form = FileForm(request.POST, request.FILES) if form.is_valid(): if request.FILES['filefield'].name.split('.')[-1] == 'xlsx': utils.handle_uploaded_file(request, f=request.FILES['filefield']) return render(request, 'publisher/file.html', { 'form': form, 'error_message': '上传成功!' }) else: error_message = '文件格式错误,请上传Excel文件(.xlsl)' form = FileForm() return render(request, 'publisher/file.html', { 'form': form, 'error_message': error_message }) else: form = FileForm() return render(request, 'publisher/file.html', {'form': form})
def file_upload(request, response_format='html'): "New file form" if request.POST: if 'cancel' not in request.POST: file = File() form = FileForm( request.user.profile, None, request.POST, request.FILES, instance=file) if form.is_valid(): file = form.save() file.set_user_from_request(request) return HttpResponseRedirect(reverse('documents_file_view', args=[file.id])) else: return HttpResponseRedirect(reverse('document_index')) else: form = FileForm(request.user.profile, None) context = _get_default_context(request) context.update({'form': form}) return render_to_response('documents/file_upload', context, context_instance=RequestContext(request), response_format=response_format)
def file_form(request): # if not request.user.is_staff or not request.user.is_superuser: # raise Http404; if request.POST: form = FileForm(request.POST, request.FILES) if form.is_valid(): instance = form.save(commit=False) instance.user = request.user instance.save() # read CSV file and define parameters input_file_name = re.search(r'/(media/.*)', instance.file.url) fileID = instance.id input_file = vcf.Reader(open(input_file_name.group(1), "r")) # output_somatic_file = open('media/somatic_mutation-'+str(fileID)+'.tsv', 'wb') # output_germline_file = open('media/germline_mutation-'+str(fileID)+'.tsv', 'wb') logs = open('media/logs-'+str(fileID)+'.txt', 'w') somatic_mutation_array = [] germline_mutation_array = [] # Read vcf file and create seperate arrays for somatic and germline mutation for record in input_file: chr = re.search(r'chr(.*)', str(record.CHROM)) start_pos = str(record.POS) if record.REF == '-': end_pos = record.POS + 1 else: end_pos = start_pos ref = str(record.REF) alt = re.search(r'\[(.*)\]', str(record.ALT)) oncoTator = 'http://www.broadinstitute.org/oncotator/mutation/' + chr.group( 1) + '_' + start_pos + '_' + end_pos + '_' + ref + '_' + alt.group(1) try: oncoTator_response = requests.get(oncoTator) except ConnectionError as e: print e logs.write(str(e)) oncoTator_response = "No response" if oncoTator_response != "No response" and oncoTator_response.status_code != 500: mutation = json.loads(oncoTator_response.text) if "ERROR" in mutation: logs.write("OncoTator is giving error for url:" + oncoTator + " Error message: " + str(oncoTator_response.text)) else: if mutation["CGC_Cancer Germline Mut"] == "yes": germline_mutation_array.append(mutation) if mutation["CGC_Cancer Somatic Mut"] == "yes": somatic_mutation_array.append(mutation) logs.close() # write values from arrays to tsv files # fieldnames = [] # for key in somatic_mutation_array[0]: # fieldnames.append(str(key)) # tsv_somatic_file = csv.DictWriter(output_somatic_file, fieldnames=fieldnames, delimiter='\t') # tsv_somatic_file.writeheader() for mutation in somatic_mutation_array: # tsv_somatic_file.writerow(mutation) ssnp = somatic_snp.objects.create(study_name=instance, gene=mutation["gene"], chromosome=mutation["ESP_Chromosome"], start=mutation["start"], end=mutation["end"], ref_allele=mutation["ref_allele"], alt_allele=mutation["alt_allele"], variant_class=mutation["variant_classification"],variant_type=mutation["variant_type"], ensemblID=mutation["HGNC_Ensembl Gene ID"]) # output_somatic_file.close() # tsv_germline_file = csv.DictWriter(output_germline_file, fieldnames=fieldnames, delimiter='\t') # tsv_germline_file.writeheader() for mutation in germline_mutation_array: # tsv_germline_file.writerow(mutation) gsnp = germline_snp.objects.create(study_name=instance, gene=mutation["gene"], chromosome=mutation["ESP_Chromosome"], start=mutation["start"], end=mutation["end"], ref_allele=mutation["ref_allele"], alt_allele=mutation["alt_allele"], variant_class=mutation["variant_classification"],variant_type=mutation["variant_type"], ensemblID=mutation["HGNC_Ensembl Gene ID"]) # output_germline_file.close() # fileread = open('media/germline_mutation-'+str(fileID)+'.tsv', 'r') # TSVfile = File(fileread) # instance.germline_result.save('media/germline_mutation-'+str(fileID)+'.tsv', TSVfile, save=True) # # fileread = open('media/somatic_mutation-' + str(fileID) + '.tsv', 'r') # TSVfile = File(fileread) # instance.somatic_result.save('media/somtic_mutation-' + str(fileID) + '.tsv', TSVfile, save=True) return HttpResponseRedirect('/result/') else: form = FileForm() return render(request, 'oncotator/snp_tools.html', {'form': form})