Python werkzeug.datastructures.FileStorage() Examples
The following are 30
code examples of werkzeug.datastructures.FileStorage().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
werkzeug.datastructures
, or try the search function
.
Example #1
Source File: file_service_test.py From JusticeAI with MIT License | 7 votes |
def test_file_service_upload(self): file_name = 'testfile.png' with open('test/testfile.png', 'rb') as f: stream = BytesIO(f.read()) file = FileStorage(stream=stream, filename=file_name) file_name_sanitized = file_service.sanitize_name(file) file_path = file_service.generate_path(1, 1, testing=True) file_service.upload_file(file, file_path, file_name_sanitized) self.assertTrue(os.path.exists("{}/{}".format(file_path, file_name))) # Delete test upload folder shutil.rmtree("{}/".format(file_service.UPLOAD_FOLDER_TEST))
Example #2
Source File: test_attachments.py From mma-dexter with Apache License 2.0 | 7 votes |
def test_delete_attachments(self): doc = Document.query.get(self.fx.DocumentData.simple.id) with open("tests/fixtures/smiley.png") as f: upload = FileStorage(f, 'smiley.png', name='file', content_type='image/png') attachment = DocumentAttachment.from_upload(upload, None) attachment.document = doc db.session.commit() self.assertEqual('image/png', attachment.image.original.mimetype) doc = Document.query.get(doc.id) x = list(doc.attachments) for att in x: for y in att.image: print 1 #print [1 for t in att.image] pass self.assertEqual(1, len(doc.attachments)) doc.attachments = [] db.session.commit() self.assertEqual(0, len(doc.attachments))
Example #3
Source File: local_image_store_test.py From betterlifepsi with MIT License | 7 votes |
def testLocalImageSaveAndRemove(self): public_id = str(uuid.uuid4()) data = self.image_file.read() stream = io.BytesIO(data) image = FileStorage(content_type='image/png', filename=u'/etc/init.d/functions.png', name='image_placeholder', content_length=0, stream=stream) result = LocalImageStore.save(image, public_id) self.assertIsNotNone(result) filename = public_id + ".png" self.assertEqual(result['url'], "/static/uploaded/" + filename) self.assertEqual(result['filename'], filename) file_absolute_path = os.path.join(self.app.config['UPLOAD_FOLDER'], result['filename']) uploaded_file = open(file_absolute_path, 'rb') uploaded_data = uploaded_file.read() self.assertEqual(data, uploaded_data) uploaded_file.close() LocalImageStore.remove(file_absolute_path, public_id) try: uploaded_file = open(file_absolute_path) uploaded_file.close() except IOError as e: pass else: self.fail("The file should be deleted!")
Example #4
Source File: utils.py From cellphonedb with MIT License | 6 votes |
def read_data_from_content_type(file: FileStorage, index_column_first: bool = False, separator: str = '', dtype=None) -> pd.DataFrame: if not separator: separator = _get_separator(file.content_type) return _read_data(file.stream, separator, index_column_first, dtype)
Example #5
Source File: test_file_upload.py From flask-file-upload with GNU General Public License v3.0 | 5 votes |
def test_update_files(self, create_app, mock_blog_model): m = mock_blog_model( name="hello", my_video__file_name="my_video.mp4", my_video__mime_type="video/mpeg", my_video__file_type="mp4", ) db.session.add(m) db.session.commit() new_file = FileStorage( stream=open(self.my_video_update, "rb"), filename="my_video_updated.mp4", content_type="video/mpeg", ) blog = m.get_blog() file_upload.update_files( blog, db, files={"my_video": new_file}, ) # Test files / dirs assert "my_video_updated.mp4" in os.listdir("tests/test_path/blogs/1") assert "my_video.mp4" not in os.listdir("tests/test_path/blogs/1")
Example #6
Source File: file_service_test.py From JusticeAI with MIT License | 5 votes |
def test_file_service_format_unsupported(self): file = FileStorage(filename='my_file.zip') self.assertFalse(file_service.is_accepted_format(file))
Example #7
Source File: file_service_test.py From JusticeAI with MIT License | 5 votes |
def test_file_service_format(self): file = FileStorage(filename='my_file.pdf') self.assertTrue(file_service.is_accepted_format(file))
Example #8
Source File: flask_admin_s3_upload.py From flask-admin-s3-upload with Apache License 2.0 | 5 votes |
def populate_obj(self, obj, name): field = getattr(obj, name, None) if field: # If field should be deleted, clean it up if self._should_delete: self._delete_file(field, obj) setattr(obj, name, '') if self.storage_type_field: setattr(obj, self.storage_type_field, '') if self.bucket_name_field: setattr(obj, self.bucket_name_field, '') return if (self.data and isinstance(self.data, FileStorage) and self.data.filename): if field: self._delete_file(field, obj) filename = self.generate_name(obj, self.data) temp_file = BytesIO() self.data.save(temp_file) filename = self._save_file(temp_file, filename) # update filename of FileStorage to our validated name self.data.filename = filename setattr(obj, name, filename) if self.storage_type == 's3': if self.storage_type_field: setattr(obj, self.storage_type_field, self.storage_type) if self.bucket_name_field: setattr(obj, self.bucket_name_field, self.bucket_name) else: if self.storage_type_field: setattr(obj, self.storage_type_field, '') if self.bucket_name_field: setattr(obj, self.bucket_name_field, '')
Example #9
Source File: test_thumbnail.py From zou with GNU Affero General Public License v3.0 | 5 votes |
def test_save_file(self): file_path_fixture = self.get_fixture_file_path("thumbnails/th01.png") th_file = FileStorage( stream=open(file_path_fixture, "rb"), filename="th01.png" ) full_path = thumbnail.save_file(TEST_FOLDER, "instance-id", th_file) thumbnail.turn_into_thumbnail(full_path, thumbnail.RECTANGLE_SIZE) im = Image.open(full_path) (width, height) = im.size self.assertEqual(width, 150) self.assertEqual(height, 100)
Example #10
Source File: file.py From RSSNewsGAE with Apache License 2.0 | 5 votes |
def has_file(self): """Return ``True`` if ``self.data`` is a :class:`~werkzeug.datastructures.FileStorage` object. .. deprecated:: 0.14.1 ``data`` is no longer set if the input is not a non-empty ``FileStorage``. Check ``form.data is not None`` instead. """ warnings.warn(FlaskWTFDeprecationWarning( '"has_file" is deprecated and will be removed in 1.0. The data is ' 'checked during processing instead.' )) return bool(self.data)
Example #11
Source File: file.py From RSSNewsGAE with Apache License 2.0 | 5 votes |
def process_formdata(self, valuelist): valuelist = (x for x in valuelist if isinstance(x, FileStorage) and x) data = next(valuelist, None) if data is not None: self.data = data else: self.raw_data = ()
Example #12
Source File: uploader.py From ckanext-s3filestore with GNU Affero General Public License v3.0 | 5 votes |
def _get_underlying_file(wrapper): if isinstance(wrapper, FlaskFileStorage): return wrapper.stream return wrapper.file
Example #13
Source File: test_file_upload.py From flask-file-upload with GNU General Public License v3.0 | 5 votes |
def test_update_files_2(self, mock_blog_model): db.init_app(app) db.create_all() file_upload = FileUpload() file_upload.init_app(app, db) new_file = FileStorage( stream=open(self.my_video_update, "rb"), filename="my_video_updated.mp4", content_type="video/mpeg", ) model = mock_blog_model(**self.attrs) assert model.my_video__file_name == "my_video.mp4" assert model.my_video__mime_type == "video/mpeg" assert model.my_video__file_type == "mp4" result = file_upload.update_files( model, files={"my_video": new_file}, ) assert result.my_video__file_name == "my_video_updated.mp4" assert result.my_video__mime_type == "mp4" assert result.my_video__file_type == "video/mpeg"
Example #14
Source File: test_media.py From amivapi with GNU Affero General Public License v3.0 | 5 votes |
def test_timezone_error(self): """Test that #150 is fixed.""" obj = self.new_object('test', test_file=FileStorage(BytesIO(lenadata), lenaname)) self.api.get(obj['test_file']['file'], headers={ 'If-Modified-Since': 'Mon, 12 Dec 2016 12:23:46 GMT'}, status_code=200)
Example #15
Source File: reqparse.py From picoCTF with MIT License | 5 votes |
def convert(self, value, op): # Don't cast None if value is None: if not self.nullable: raise ValueError("Must not be null!") return None elif isinstance(self.type, Model) and isinstance(value, dict): return marshal(value, self.type) # and check if we're expecting a filestorage and haven't overridden `type` # (required because the below instantiation isn't valid for FileStorage) elif isinstance(value, FileStorage) and self.type == FileStorage: return value try: return self.type(value, self.name, op) except TypeError: try: if self.type is decimal.Decimal: return self.type(str(value), self.name) else: return self.type(value, self.name) except TypeError: return self.type(value)
Example #16
Source File: file.py From RSSNewsGAE with Apache License 2.0 | 5 votes |
def __call__(self, form, field): if not (isinstance(field.data, FileStorage) and field.data): if self.message is None: message = field.gettext('This field is required.') else: message = self.message raise StopValidation(message)
Example #17
Source File: flask.py From sentry-python with BSD 2-Clause "Simplified" License | 5 votes |
def size_of_file(self, file): # type: (FileStorage) -> int return file.content_length
Example #18
Source File: phenotype.py From ukbrest with MIT License | 5 votes |
def __init__(self, **kwargs): super(QueryAPI, self).__init__() self.parser.add_argument('file', type=FileStorage, location='files', required=True) self.parser.add_argument('section', type=str, required=True) self.parser.add_argument('missing_code', type=str, required=False) self.parser.add_argument('Accept', location='headers', choices=PHENOTYPE_FORMATS.keys(), help='Only {} are supported'.format(' and '.join(PHENOTYPE_FORMATS.keys()))) self.pheno2sql = app.config['pheno2sql']
Example #19
Source File: s3.py From Flask-Store with MIT License | 5 votes |
def save(self): """ Acts as a proxy to the actual save method in the parent class. The save method will be called in a ``greenlet`` so ``gevent`` must be installed. Since the origional request will close the file object we write the file to a temporary location on disk and create a new :class:`werkzeug.datastructures.FileStorage` instance with the stram being the temporary file. """ fp = self.fp temp = TemporaryStore(fp) path = temp.save() filename = self.safe_filename(fp.filename) @copy_current_request_context def _save(): self.fp = FileStorage( stream=open(path, 'rb'), filename=filename, name=fp.name, content_type=fp.content_type, content_length=fp.content_length, headers=fp.headers) super(S3GeventProvider, self).save() # Cleanup - Delete the temp file os.unlink(path) gevent.spawn(_save) self.filename = filename
Example #20
Source File: conftest.py From flask-fs with MIT License | 5 votes |
def filestorage(self, filename, content, content_type=None): return FileStorage( self.file(content), filename, content_type=content_type )
Example #21
Source File: type_filter.py From pre-request with MIT License | 5 votes |
def _type_transform(self, d_type, value): """ :param d_type: :param value: :return: """ if d_type == str and isinstance(value, bytes): return value.decode('utf-8') # 特殊的字符串转bool类型 if d_type == bool and isinstance(value, str): return value not in _false_str_list # 日期转换 if d_type == datetime: try: return datetime.strptime(value, self.rule.fmt) except ValueError: raise ParamsValueError(self.datetime_error_code, filter=self) # 文件处理 from werkzeug.datastructures import FileStorage if d_type == FileStorage: return value try: # FIX: invalid literal for int() with base 10 # 处理int仅能转换纯数字字符串问题 if d_type == int and "." in value: value = value.split(".")[0] return d_type(value) except ValueError: raise ParamsValueError(self.error_code, filter=self)
Example #22
Source File: file_service_test.py From JusticeAI with MIT License | 5 votes |
def test_file_service_name_sanitize(self): file = FileStorage(filename='some/file/path/my_file.pdf') self.assertTrue(file_service.sanitize_name(file) == 'some_file_path_my_file.pdf')
Example #23
Source File: mongo.py From flask-fs with MIT License | 5 votes |
def save(self, wfs, filename=None): '''Save a Werkzeug FileStorage object''' if self.basename and not filename: ext = extension(filename or wfs.filename) filename = '.'.join([self.basename(self._instance), ext]) prefix = self.upload_to(self._instance) if callable(self.upload_to) else self.upload_to self.filename = self.fs.save(wfs, filename, prefix=prefix) return self.filename
Example #24
Source File: test_stack_analyses.py From fabric8-analytics-server with Apache License 2.0 | 5 votes |
def test_sa_success(self, _post_request): """Success stack analyses flow.""" with open(str(Path(__file__).parent.parent.parent) + '/data/manifests/202/npmlist.json', 'rb') as fp: fs = FileStorage(stream=fp, filename='npmlist.json') sa_post_request = StackAnalysesPostRequest(manifest=fs, file_path='/tmp/bin', ecosystem='npm', show_transitive=True) sa = StackAnalyses(sa_post_request) response = sa.post_request() self.assertIsInstance(response, dict) self.assertIn('status', response) self.assertEqual(response['status'], 'success') self.assertIn('id', response)
Example #25
Source File: file_util_test.py From betterlifepsi with MIT License | 5 votes |
def testSaveImage(self): from psi.app.models.product import ProductImage owner = ProductImage() data = self.image_file.read() stream = io.BytesIO(data) image = FileStorage(content_type='image/png', name='image_placeholder', filename=u'/etc/init.d/functions.png', content_length=0, stream=stream) image = save_image(owner, image) self.assertIsNotNone(owner.image) self.assertIsNotNone(image) self.assertIsNotNone(image.path) self.assertIsNotNone(image.public_id) self.assertIn(image.public_id, image.path)
Example #26
Source File: test_stack_analyses.py From fabric8-analytics-server with Apache License 2.0 | 5 votes |
def test_sa_rdb_error(self, _post_request): """Check if 500 is raise upon request save failure.""" with open(str(Path(__file__).parent.parent.parent) + '/data/manifests/202/npmlist.json', 'rb') as fp: fs = FileStorage(stream=fp, filename='npmlist.json') sa_post_request = StackAnalysesPostRequest(manifest=fs, file_path='/tmp/bin', ecosystem='npm', show_transitive=True) sa = StackAnalyses(sa_post_request) with pytest.raises(Exception) as exception: sa.post_request() self.assertIs(exception.type, RDBSaveException)
Example #27
Source File: test_stack_analyses.py From fabric8-analytics-server with Apache License 2.0 | 5 votes |
def test_sa_backbone_error(self, _aggregate_request): """Check if 500 is raise upon invalid response from backbone server.""" with open(str(Path(__file__).parent.parent.parent) + '/data/manifests/202/npmlist.json', 'rb') as fp: fs = FileStorage(stream=fp, filename='npmlist.json') sa_post_request = StackAnalysesPostRequest(manifest=fs, file_path='/tmp/bin', ecosystem='npm', show_transitive=True) sa = StackAnalyses(sa_post_request) with pytest.raises(Exception) as exception: sa.post_request() self.assertIs(exception.type, BackboneServerException)
Example #28
Source File: test_stack_analyses.py From fabric8-analytics-server with Apache License 2.0 | 5 votes |
def test_sa_mismatch_manifest_file_and_ecosystem(self): """Check if 400 is raise upon mismatch between manifest file content and ecosystem type.""" with open(str(Path(__file__).parent.parent.parent) + '/data/manifests/202/npmlist.json', 'rb') as fp: fs = FileStorage(stream=fp, filename='npmlist.json') with pytest.raises(Exception) as exception: sa_post_request = StackAnalysesPostRequest(manifest=fs, file_path='/tmp/bin', ecosystem='pypi', show_transitive=True) sa = StackAnalyses(sa_post_request) sa.post_request() self.assertIs(exception.type, ValidationError)
Example #29
Source File: test_stack_analyses.py From fabric8-analytics-server with Apache License 2.0 | 5 votes |
def test_sa_invalid_manifest_file(self): """Check if 400 is raise upon invalid manifest file.""" with open(str(Path(__file__).parent.parent.parent) + '/data/manifests/400/npmlist.json', 'rb') as fp: fs = FileStorage(stream=fp, filename='npmlist.json') sa_post_request = StackAnalysesPostRequest(manifest=fs, file_path='/tmp/bin', ecosystem='npm', show_transitive=True) sa = StackAnalyses(sa_post_request) with pytest.raises(Exception) as exception: sa.post_request() self.assertIs(exception.type, SAInvalidInputException)
Example #30
Source File: local.py From flask-fs with MIT License | 5 votes |
def save(self, file_or_wfs, filename): self.ensure_path(filename) dest = self.path(filename) if isinstance(file_or_wfs, FileStorage): file_or_wfs.save(dest) else: with open(dest, 'wb') as out: shutil.copyfileobj(file_or_wfs, out) return filename