Esempio n. 1
0
def process_spreadsheet_file():
    ''' Page to handle processing excel file (xlsx). Creates json and redirects to editor-UI '''

    global g_active_json, g_active_filename, g_active_xlsx_fname

    form = FileUploadForm()
    if form.validate_on_submit():
        f = form.in_file.data
        filename = secure_filename(f.filename)
        if not exists(STORAGE_FOLDER):
            makedirs(STORAGE_FOLDER)
        g_active_xlsx_fname = STORAGE_FOLDER + filename
        f.save(g_active_xlsx_fname)
        g_active_json, _ = process_file(g_active_xlsx_fname)
        g_active_filename = '.'.join(
            g_active_xlsx_fname.split('.')[:-1]) + '.ccut.json'
        with open(g_active_filename, 'w') as outfile:
            dump(g_active_json, outfile, indent=2)

        # TODO: color original xlsx
        return redirect(url_for('edit_annotation_file'))
    else:
        return render_template('file_upload.html',
                               form=form,
                               title='Process spreadsheet file',
                               file_ext='.xlsx')
Esempio n. 2
0
def uploading():
    form = FileUploadForm()
    if request.method == 'POST':
        if form.validate_on_submit() and 'picFile' in request.files:
            file = request.files['picFile']
            #flash(file.filename)
            if changeFileExtension(file):
                filename = file.filename
                if images.file_allowed(file, filename):
                    #filename = secure_filename(filename)
                    filename = changeFileName(filename)
                    path = 'pict'
                    name_for_save = images.resolve_conflict(path, filename)
                    images.save(file, path, name_for_save)
                    # db
                    db = get_db()
                    db.execute(
                        'INSERT INTO PICTURES ( path, filename, comment, data ) VALUES (?,?,?,?)',
                        [
                            path, name_for_save, request.form['comment'],
                            time.time()
                        ])
                    db.commit()
                    return redirect(url_for('pictures'))
            form.errors['picFile'] = ('file extension is not supported', )
    header = 'Страница загрузки'.decode('utf-8')
    return render_template('upload.html',
                           title=app.config['TITLE'],
                           header=header,
                           form=form)
Esempio n. 3
0
def file_upload():
    if request.method == 'POST':
        logger.info(request.form)
        form = FileUploadForm(CombinedMultiDict((request.files, request.form)))
        file = request.files.get('file')
        form.hash.data = hasher.hash(file)
        logger.info(form.hash.data)
        if form.validate():
            logger.info('Form is valid')
            file.stream.seek(0)
            timestamp = int(datetime.datetime.now().timestamp())
            filename = str(timestamp) + secure_filename(file.filename)
            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            logger.info(filename + ' Saved')
            db_file = File(name=form.upload_name.data,
                           cve_number=form.cve_number.data,
                           full_path=os.path.join(app.config['UPLOAD_FOLDER'],
                                                  filename),
                           description=form.description.data,
                           file_type=form.upload_type.data,
                           hash=form.hash.data)
            db.session.add(db_file)
            db.session.commit()
            # logger.info(str(db_file) + ' saved to db')
            flash('File Uploaded', 'success')
        else:
            # logger.info(form.errors)
            flash_form_errors(form)
        if request.referrer:
            return redirect(request.referrer)
        else:
            return redirect(url_for('index'))
Esempio n. 4
0
def kaoqin():
    print 'this is index ()'
    form = FileUploadForm()
    filedata = []
    if form.validate_on_submit():
        for upload in form.uploads.entries:
            filedata.append(upload)
    return render_template("download.html", form=form)
Esempio n. 5
0
def upload(request):
    if request.method == 'POST':
        form = FileUploadForm(request.POST, request.FILES)
        if form.is_valid():
            f = Upload()
            f.filename = form.cleaned_data['file']
            f.save()
#        messages.add_message(request, messages.SUCCESS, u'文件 {0} 上传成功, 大小为{1}KB'.format(f.filename.name.split('/')[-1], int(f.filename.size)/1024))
#        return HttpResponseRedirect(reverse('push'))
        return HttpResponse(u'文件 {0} 上传成功, 大小为{1}KB'.format(f.filename.name.split('/')[-1], int(f.filename.size)/1024))
Esempio n. 6
0
 def get_response(self, request, *args, **kwargs):
     try:
         folder = Folder.objects.get(
             pk=request.POST.get('selected_folder_id', 0))
     except Folder.DoesNotExist:
         raise Http404
     form = FileUploadForm(request.POST, request.FILES)
     if form.is_valid():
         file = form.save(commit=False)
         file.folder = folder
         file.save()
         return {'result': 'success'}
     else:
         return {'result': 'error'}
Esempio n. 7
0
def index():
    form = FileUploadForm()

    filedata = []

    if form.validate_on_submit():
        print 'form.validate !!!'
        for upload in form.uploads.entries:
            filedata.append(upload)
        print len(filedata)

    for file1 in filedata:
        print file1.data.filename

    return render_template("index.html", form=form, filedata=filedata)
Esempio n. 8
0
def upload():
    form = FileUploadForm()
    header = 'Cтраница загрузки'.decode('utf-8')
    return render_template('upload.html',
                           title=app.config['TITLE'],
                           header=header,
                           form=form)
Esempio n. 9
0
def list(request):
    # Handle file upload
    form = FileUploadForm(request.POST or None, request.FILES)
    if form.is_valid():
        for obj_file in request.FILES.getlist('file'):
            fileup = File(file=obj_file)
            fileup.save()
        # Redirect to the search view after POST
        return HttpResponseRedirect(reverse('files'))
    results = File.objects.all()

    return render_to_response('search/search.html', {
        'results': results,
        'form': form
    },
                              context_instance=RequestContext(request))
def list(request):
    # Handle file upload
    form = FileUploadForm(request.POST or None, request.FILES)
    if form.is_valid():
        for obj_file in request.FILES.getlist('file'):
            fileup = File(file = obj_file)
            fileup.save()
        # Redirect to the search view after POST
        return HttpResponseRedirect(reverse('files'))
    results = File.objects.all()
    
    return render_to_response(
        'search/search.html',
        {'results': results, 'form': form},
        context_instance = RequestContext(request)
    )
Esempio n. 11
0
 def get_response(self, request, *args, **kwargs):
     try:
         folder = Folder.objects.get(pk=request.POST.get('selected_folder_id', 0))
     except Folder.DoesNotExist:
         raise Http404
     form = FileUploadForm(request.POST, request.FILES)
     if form.is_valid():
         file = form.save(commit=False)
         file.folder = folder
         file.save()
         return {
             'result': 'success'
         }
     else:
         return {
             'result': 'error'
         }
Esempio n. 12
0
def upload_file(request):         #上传文件
    """
    文件接收 view
    :param request: 请求
    :return:
    """
    if request.method == 'POST':        #POST和GET是HTTP协议定义的与服务器交互的方法。GET一般用于获取/查询 资源信息,而POST一般用于更新 资源信息。GET提交,请求的数据会附在URL之后(就是把数据放置在HTTP协议头中),以?分割URL和传输数据,多个参数用&连接;POST提交:把提交的数据放置在是HTTP包的包体中。GET提交的数据会在地址栏中显示出来,而POST提交,地址栏不会改变
        my_form = FileUploadForm(request.POST, request.FILES) #文件上传的时候,文件数据被保存在 request. FILES
        if my_form.is_valid():
          #  f = my_form.cleaned_data['my_file']
          #  handle_uploaded_file(f)
            filemodel = uploadfile()                 #保存记录到数据库
            filemodel.filepath = my_form.cleaned_data['my_file']  #不管表单提交的是什么数据,一旦通过调用 is_valid() 成功验证( is_valid() 返回 True ),验证后的表单数据将位于 form.cleaned_data字典中。这些数据已经为你转换好为Python 的类型。
            filemodel.save()
        return HttpResponse('Upload Success')
    else:
        my_form = FileUploadForm()
    return render(request, 'tb/upload.html', {'form': my_form})   #根据html的请求,视图将返回特定的html模板以及字典参数数据
Esempio n. 13
0
def upload_file(login):
    form = FileUploadForm()
    form.validate()
    if len(form.file.errors) > 0:
        flash('Brak pliku do wysłania', category='alert-warning')
        return redirect(url_for('files'))

    token = create_upload_token(login)
    url = Config.FILE_STORE_URL + f'/files?user={login}&token={token}'
    files = {'file': (form.file.data.filename, form.file.data)}

    r = requests.post(url, files=files)
    if r.status_code == 200:
        flash('Plik został przesłany', category='alert-success')
    else:
        flash('Nie udało się przesłać pliku', category='alert-danger')

    return redirect(url_for('files'))
Esempio n. 14
0
def upload():
    import os
    from werkzeug.utils import secure_filename
    form = FileUploadForm()
    if form.validate_on_submit():
            data = request.files[form.filename.name].read()
            filename = secure_filename(form.filename.data.filename)
            file_path = os.path.join('./app/uploads/', filename)
            open(file_path, 'w').write(data)
            parsexlsdata.delay(file_path)
            flash('Data nahrana a budou se zpracovavat',category='info')
    task= json.loads(inspect().data)
    if task['task'][0]['id'] == '':
        busy=True
    else:
        busy=False
        flash('Data jsou zpracovavana pockejte nekolik minut a akci zopakujte pokud potrebujete...',category='info')


    return render_template("fileupload.html", form = form , user = user ,busy=busy)
Esempio n. 15
0
def load_annotation_file():
    ''' Page to handle uploading annotation file (json). Redirects to editor-UI '''

    global g_active_json, g_active_filename

    form = FileUploadForm()
    if form.validate_on_submit():
        f = form.in_file.data
        filename = secure_filename(f.filename)
        if not exists(STORAGE_FOLDER):
            makedirs(STORAGE_FOLDER)
        g_active_filename = STORAGE_FOLDER + filename
        f.save(g_active_filename)
        with open(g_active_filename, 'r') as read_file_h:
            g_active_json = load(read_file_h)
        return redirect(url_for('edit_annotation_file'))
    else:
        return render_template('file_upload.html',
                               form=form,
                               title='Upload annotation file',
                               file_ext='.json')
Esempio n. 16
0
def datasets():
	if not current_user.is_authenticated:
		return redirect(url_for('no_access'))

	datasets = Dataset.query.filter(Dataset.user_id == current_user.id).all()

	for ds in datasets:
		ds.distinctive_name = ds.distinctive_name or ds.filename
		if ds.distinctive_name == ds.filename:
			ds.display_filename = ''
		else: 
			ds.display_filename = ds.filename
			
	model = {
		'title': 'Datasets',
		'datasets': datasets
	}
	form = FileUploadForm()
	if form.validate_on_submit():

		dsFile = form.fileName.data

		separator = form.separator.data
		distinctive_name = form.distinctive_name.data

		filename = secure_filename(dsFile.filename)
		guid = str(uuid.uuid4())
		
		dsFile.seek(0)
		dt = dsFile.read()
		
		dbDs = Dataset(filename, guid, g.user, datetime.datetime.utcnow(), separator, distinctive_name, dt)
		
		db.session.add(dbDs)
		db.session.commit()
		return redirect(url_for('datasets'))
	
	model['form'] = form

	return render_template('datasets.html', model = model)
Esempio n. 17
0
def uploadFile(request):
    if request.method == 'POST':
        #print request.FILES['file'].name

        form = FileUploadForm(request.POST, request.FILES)

        if form.is_valid():
            form.save()
            print 'upload successful'

    else:
        form = FileUploadForm()
        print 'upload unsuccessful'

    return render(request, '../templates/index.html', {'form': form})
Esempio n. 18
0
def script(request):
    check_file()
    file_list = Upload.objects.all()
    upload_form = FileUploadForm()
    if request.method == 'POST':
        hosts = request.POST.getlist('host')
        f_script = request.POST.get('script')
        result = list()
        for host in hosts:
            asset = Asset.objects.get(id=int(host))
            if asset.ip:
                jid1 = saltapi.exeScript(asset.ip, f_script)
                result.append(jid1)
            elif asset.other_ip:
                jid2 = saltapi.exeScript(asset.other_ip, f_script)
                result.append(jid2)
        return JsonResponse(response, safe=False)
    locals().update({'asset_list': asset_list})
    return render(request, 'salts/script.html', locals())
Esempio n. 19
0
def push(request):
    check_file()
    if request.method == 'POST':
        hosts = request.POST.getlist('host')
        push_file = request.POST.get('push_file')
        dest = request.POST.get('dest')
        result = list()
        for host in hosts:
            asset = Asset.objects.get(id=int(host))
            if asset.ip:
                jid1 = saltapi.pushFile(asset.ip, push_file, dest)
                result.append(jid1)
            elif asset.other_ip:
                jid2 = saltapi.pushFile(asset.other_ip, push_file, dest)
                result.append(jid2)
        return JsonResponse(response, safe=False)
    file_list = Upload.objects.all()
    form = FileUploadForm()
    locals().update({'asset_list': asset_list})
    return render(request, 'salts/push.html', locals())
Esempio n. 20
0
def upload_file(request):
    """
    Upload file
    :param request: HTTP request
    :return: json data in python dict
    """
    if request.method == 'POST':
        form = FileUploadForm(request.POST, request.FILES)
        if form.is_valid():
            form.save()
            return dict(
                filelink=form.get_media_url(),
                filename=form.get_filename()
            )
    return {}
Esempio n. 21
0
def add_inventory():
    if is_authenticated(request) and is_staff(request):
        form = CreateProduct()
        upload_image = FileUploadForm()
        if request.method == "GET":
            return render_template("pages/staff_pages/add_inventory.html",
                                   form=form,
                                   upload_image=upload_image,
                                   user=get_user(request),
                                   staff=is_staff(request))
        elif request.method == "POST":
            image = upload_image.file.data
            image_link = upload(image)
            products = dat_loader.load_data("Products")["data"]
            products_id = dat_loader.load_data("Products")["id"]
            new_product = Product(products_id,
                                  form.title.data, form.description.data,
                                  int(form.stock.data), form.retail_price.data,
                                  form.cost_price.data, image_link)
            products.append(new_product)
            dat_loader.write_data("Products", products)
            return redirect("/dashboard/inventory/")
    else:
        return redirect("/login/")
Esempio n. 22
0
def upload_file():
    form = FileUploadForm()
    if form.validate_on_submit():
        filename = form.excel_file.data.filename
        print filename
Esempio n. 23
0
def upload_form(request):
    profile_form = ProfilesForm(initial={'profile': -1})
    file_upload_form = FileUploadForm()
    file_download_form = FileDownloadForm()

    submitted_alert = False
    success_alert = False

    if request.method == 'POST':

        # Initialize a session for the request
        s = Session()

        # Retrieve file, if present
        if 'upload' in request.FILES.keys():
            try:
                f = request.FILES['upload']
                files = {'file': f}

                profile = str(request.POST["profile"])
                if profile == '-1':
                    req = Request('POST',
                                  UPLOAD_WITHOUT_PROFILE_URL,
                                  files=files,
                                  headers={'Authorization': PARSER_JWT})

                else:
                    req = Request('POST',
                                  UPLOAD_WITH_PROFILE_URL,
                                  data={"profile": profile},
                                  files=files,
                                  headers={'Authorization': PARSER_JWT})

                prep = req.prepare()
                resp = s.send(prep, timeout=None)

                if resp.status_code == 200:
                    success_alert = True
            except:
                pass
        else:
            try:
                print request.POST
                method = str(request.POST["method"])
                url = str(request.POST["url"])
                directory = str(request.POST["directory"])
                filename = str(request.POST["name"])
                username = str(request.POST["username"])
                password = str(request.POST["password"])
                profile = str(request.POST["profile"])
                print 'info'
                print method, url, directory, filename, username, password, profile

                if profile == '-1':
                    metadataProfileId = None
                else:
                    metadataProfileId = profile

                data = dict()
                data["downloadMethod"] = method
                data["downloadURL"] = url
                data["downloadDirectory"] = directory
                data["fileName"] = filename
                data["username"] = username
                data["password"] = password
                data["metadataProfileId"] = metadataProfileId

                req = Request('POST',
                              DOWNLOAD_URL,
                              json=data,
                              headers={'Authorization': PARSER_JWT})
                prep = req.prepare()
                resp = s.send(prep, timeout=None)

                if resp.status_code == 200:
                    success_alert = True
            except:
                pass

        s.close()

        submitted_alert = True

    return render(
        request, 'upload-form.html', {
            'profile_form': profile_form,
            'upload_form': file_upload_form,
            'download_form': file_download_form,
            'submitted_alert': submitted_alert,
            'success_alert': success_alert
        })
Esempio n. 24
0
def upload_form(request):
    #   check that the file was posted and that the FILES object contains key of image
    if request.method == 'POST' and request.FILES['image']:

        #   retrieve the file obj
        image = request.FILES['image']
        document = request.FILES['document']
        """
            NEED TO USE FILESYSTEMSTORAGE TO SAVE TO CORRECT LOCATION
        """

        #   save the IMAGE to the filesystem and retrieve the URL from where it is stored
        #img_folder = 'media/images/'
        #fs_img = FileSystemStorage(location=img_folder,base_url='/media/images/')
        fs_img = FileSystemStorage(location=settings.FS_IMAGE_UPLOADS,
                                   base_url=settings.FS_IMAGE_URL)
        imageName = fs_img.save(image.name, image)
        uploaded_image_url = fs_img.url(imageName)

        #   save the DOCUMENT to the filesystem and retrieve the URL from where it is stored
        #doc_folder = 'media/documents/'
        #fs_doc = FileSystemStorage(location=doc_folder,base_url='/media/documents/')
        fs_doc = FileSystemStorage(location=settings.FS_DOCUMENT_UPLOADS,
                                   base_url=settings.FS_DOCUMENT_URL)
        documentName = fs_doc.save(document.name, document)
        """
            File saves to the correct location e.g. media/documents/band.txt
            BUT
            .url is incorrect e.g. media/band.txt
        """
        uploaded_document_url = fs_doc.url(documentName)
        """
            NEED TO PASS
                uploaded_image_url
                uploaded_document_url
            TO THE FORM __INIT__ function so that it can be saved to the model
            So:
                list created containing URLs to be used as KWARGS in forms.py init function
        """
        _URL_LIST = [uploaded_image_url, uploaded_document_url]

        #form = FileUploadForm(uploaded_image_url,uploaded_document_url,request.POST)
        #form = FileUploadForm(request.user,request.POST)

        #   Declare the form to use and pass params
        form = FileUploadForm(request.user, request.POST, _urls=_URL_LIST)

        #   check the form is valid and if yes, save it
        if form.is_valid():
            form.save()
            messages.success(
                request, "You have successfully registered as an Entertainer")
            return render(
                request, 'uploads/upload_form.html', {
                    'uploaded_image_url': uploaded_image_url,
                    'uploaded_document_url': uploaded_document_url
                })
    else:
        #   If page was just loaded then an empty form is displayed
        form = FileUploadForm(request.user)
    return render(request, 'uploads/upload_form.html', {'form': form})
Esempio n. 25
0
def upload():
    dates = ImportantDate.query.order_by(ImportantDate.date_time)
    if not current_user.is_admin:
        #throw a 403 error. we could do a custom error page later.
        abort(403)
    formUpload = FileUploadForm()
    if formUpload.validate_on_submit():
        if 'file' not in request.files:
            flash('No File')
            return redirect(request.url)
        file = request.files['file']
        try:
            num_rows_deleted = db.session.query(Checksheet).delete()
            db.session.commit()
            flash('Checksheet database successfully dumped', 'success')
        except:
            db.session.rollback()
            flash('There was an error dumping the Checksheet database',
                  'danger')

        try:
            parse_dates = [
                'Initial essay', 'Date 1', 'Date 2', 'Date 3', 'Date 4',
                'Date 5', 'Date 6', 'Date 7', 'Date 8'
            ]
            checksheet = pd.read_csv(file, parse_dates=parse_dates)
            checksheet.dropna(axis=0, how='all', inplace=True)
            checksheet.columns = [
                'lastName', 'firstName', 'honors_id', 'email', 'admitted',
                'dupontCode', 'status', 'comments', 'term', 'major', 'advisor',
                'initialEssayDate', 'coCur1', 'coCurDate1', 'coCur2',
                'coCurDate2', 'coCur3', 'coCurDate3', 'coCur4', 'coCurDate4',
                'coCur5', 'coCurDate5', 'coCur6', 'coCurDate6', 'coCur7',
                'coCurDate7', 'coCur8', 'coCurDate8', 'fsemHN', 'fsemHNDate',
                'hnCourse1', 'hnCourse1Date', 'hnCourse2', 'hnCourse2Date',
                'hnCourse3', 'hnCourse3Date', 'hnCourse4', 'hnCourse4Date',
                'hnCourse5', 'hnCourse5Date', 'researchCourse',
                'researchCourseDate', 'capstoneCourse', 'capstoneCourseDate',
                'hon201', 'hon201Date', 'leadership', 'mentoring',
                'portfolio4', 'portfolio1', 'portfolio2', 'portfolio3', 'exit'
            ]
            checksheet.to_sql('checksheets',
                              con=db.engine,
                              if_exists='append',
                              index=False)
            flash('Upload Successful!')
            print checksheet['coCurDate1']

            # drop old users from database
            try:
                students = User.query.filter_by(is_admin=0).all()
                if students is not None:
                    for student in students:
                        print("student=%s" % student.username)
                        try:
                            found = Checksheet.query.filter_by(
                                honors_id=student.honors_id).first()
                            print(found)
                            if found is None:
                                try:
                                    user = User.query.get_or_404(student.id)
                                    db.session.delete(user)
                                    db.session.commit()
                                except:
                                    db.session.rollback()
                                    flash('User not deleted', 'danger')
                        except:
                            flash('Error when searching Checksheet table',
                                  'danger')
                else:
                    flash('There are currently no student accounts.',
                          'warning')
            except:
                flash('Error searching Users table', 'danger')
        except:
            flash(
                'The file you uploaded has an error. Please check the format and try again.'
            )

        return redirect(url_for('admin.upload'))

    return render_template('admin/upload.html',
                           title="Upload",
                           formUpload=formUpload,
                           dates=dates)