def test_allowed_file(self): print(f'Executing {sys._getframe( ).f_code.co_name}...') bad_filename = 'bad_filename.err' self.assertFalse(allowed_file(bad_filename)) good_filename_lower = 'good_filename.png' good_filename_upper = 'good_filename.PNG' self.assertTrue( allowed_file(good_filename_lower) and allowed_file(good_filename_upper)) print(f'Completed {sys._getframe( ).f_code.co_name}!')
def process(): form = UploadForm() if form.validate_on_submit(): file = request.files['upload_file'] filename = secure_filename(file.filename) if file and allowed_file(filename): df = create_pandas_df(file) if df is None: form.upload_file.errors.append("Your file is missing a column 'Address' or 'address'") return render_template('home.html', form=form) else: upload_directory = get_upload_path() if not os.path.exists(upload_directory): os.makedirs(upload_directory) df.to_csv(os.path.join(upload_directory, filename), encoding='utf-8', index=False) return redirect(url_for('result', filename=filename)) else: form.upload_file.errors.append("You must upload only .csv files") return render_template('home.html', form=form) else: return render_template('home.html', form=form)
def change_car(car_id): def construct_response(code, message): return {'code': code, 'message': message} def incorrect_param(p): return construct_response(1, 'Incorrect parameter: {0}'.format(p)) def missing_param(p): return construct_response(1, 'Missing parameter: {0}'.format(p)) car_type = request.form.get('ts_type') car_usage_type = request.form.get('ts_usagetype') car_activities = request.form.get('ts_activities') car_brand = request.form.get('ts_make') car_model = request.form.get('ts_model') car_year = request.form.get('ts_made') car_color = request.form.get('ts_color') car_photo = request.files['file'] if not car_type: return make_response(jsonify(missing_param('car_type')), 400) if not car_usage_type: return make_response(jsonify(missing_param('car_usage_type')), 400) if not car_activities: return make_response(jsonify(missing_param('car_activities')), 400) if not car_brand: return make_response(jsonify(missing_param('car_brand')), 400) if not car_model: return make_response(jsonify(missing_param('car_model')), 400) if not car_year: return make_response(jsonify(missing_param('car_year')), 400) if not car_color: return make_response(jsonify(missing_param('car_color')), 400) if not car_photo: return make_response(jsonify(missing_param('car_photo')), 400) if not allowed_file(car_photo.filename): return make_response(jsonify(incorrect_param('car_photo')), 400) filename = secure_filename(car_photo.filename) car_photo.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) car = Car.query.filter_by(id=car_id).all()[0] car.car_type = car_type car.car_usage_type = car_usage_type car.car_activities = car_activities car.car_brand = car_brand car.car_model = car_model car.car_year = car_year car.car_color = car_color car.user_id = g.user.id car.car_photo = 'img/' + filename db.session.commit() return make_response(jsonify(construct_response(0, 'OK')), 200)
def test_upload_file(client): if request.method == 'POST': with open('gory.jpeg', 'rb') as f: filename = f.name print(filename) filename = allowed_file(filename) post_return = client.post( path='/upload', data={'file': f} ) print(post_return) assert post_return.status_code == 200
def test_allowed_file(): assert allowed_file('photo.jpg') assert allowed_file('photo.JPG') assert allowed_file('photo.Jpg') assert not allowed_file('photo') assert not allowed_file('photo.doc') assert not allowed_file('photo.')
def upload_file(): if request.method == 'POST': file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename)) if Simulation(filename).process_all_habitat_simulations(): return redirect('/static/output/sample_output.txt') else: redirect("/?error=Something went wrong on the server") return redirect("/?error=Something went wrong on the server")
def changeImage(): data = request.files print(data) if 'inputImage' not in data: print("File not found") return redirect(url_for("main.index_GET")) file = data['inputImage'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': print("File unnamed") return redirect(url_for("main.index_GET")) if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(cfg["upload_folder"], filename)) return jsonify({"success": True, "filename": filename})
def upload_image(): if request.method == 'POST': # check if the post request has the file part if 'images' not in request.files: flash('No file part') return redirect(request.url) file = request.files['images'] # if user does not select file, browser also # submit an empty part without filename if file.filename == '': flash('No selected images') return redirect(request.url) if file and app.allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(app.app.config['UPLOAD_FOLDER'], filename)) return redirect(url_for('.display_images')) return render_template('image_upload.html')
def add_user(): try: # file upload request if 'file' not in request.files: error_message = 'Error: No file field defined in request' print(error_message) resp = jsonify(error_message) resp.status_code = 200 return resp file = request.files['file'] if file and allowed_file(file.filename): if os.path.exists(UPLOAD_FOLDER): fullFilename = secure_filename(file.filename) filename, fileExtension = os.path.splitext(fullFilename) newName = filename + '_' + datetime.now().strftime( "%Y%m%d%H%M%S") + fileExtension file.save(os.path.join(app.config['UPLOAD_FOLDER'], newName)) else: raise ValueError("Specified path in server doesn't exist") # form submit request dictJson = request.form.to_dict(flat=True) ttrf_obj = ttrf() for k in dictJson.keys(): exec('ttrf_obj.{0} = dictJson[\'{1}\']'.format(k, k)) # Convert string type to correspondent types ttrf_obj.StringToSqlType() # Add and commit session to update database session.add(ttrf_obj) session.commit() return redirect('/success') except Exception as e: print(e) return unsupported_media_type(e)
def test_allowed_file(): allowed_filename = 'test.png' expected = True actual = allowed_file(allowed_filename) assert expected == actual