本文整理汇总了Python中django.core.files.storage.default_storage.open方法的典型用法代码示例。如果您正苦于以下问题:Python default_storage.open方法的具体用法?Python default_storage.open怎么用?Python default_storage.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.storage.default_storage
的用法示例。
在下文中一共展示了default_storage.open方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _upload_instance
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def _upload_instance(self, xml_file, instance_dir_path, files):
xml_doc = clean_and_parse_xml(xml_file.read())
xml = StringIO()
de_node = xml_doc.documentElement
for node in de_node.firstChild.childNodes:
xml.write(node.toxml())
new_xml_file = ContentFile(xml.getvalue())
new_xml_file.content_type = 'text/xml'
xml.close()
attachments = []
for attach in de_node.getElementsByTagName('mediaFile'):
filename_node = attach.getElementsByTagName('filename')
filename = filename_node[0].childNodes[0].nodeValue
if filename in files:
file_obj = default_storage.open(
os.path.join(instance_dir_path, filename))
mimetype, encoding = mimetypes.guess_type(file_obj.name)
media_obj = django_file(file_obj, 'media_files[]', mimetype)
attachments.append(media_obj)
create_instance(self.user.username, new_xml_file, attachments)
示例2: _upload_instances
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def _upload_instances(self, path):
instances_count = 0
dirs, not_in_use = default_storage.listdir(path)
for instance_dir in dirs:
instance_dir_path = os.path.join(path, instance_dir)
i_dirs, files = default_storage.listdir(instance_dir_path)
xml_file = None
if 'submission.xml' in files:
file_obj = default_storage.open(
os.path.join(instance_dir_path, 'submission.xml'))
xml_file = file_obj
if xml_file:
try:
self._upload_instance(xml_file, instance_dir_path, files)
except ExpatError:
continue
except Exception:
pass
else:
instances_count += 1
return instances_count
示例3: get_favicon
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def get_favicon(self, size, rel, update=False):
"""
get or create a favicon for size, rel(attr) and uploaded favicon
optional:
update=True
"""
fav, _ = FaviconImg.objects.get_or_create(
faviconFK=self, size=size, rel=rel)
if update and fav.faviconImage:
fav.del_image()
if self.faviconImage and not fav.faviconImage:
tmp = Image.open(storage.open(self.faviconImage.name))
tmp.thumbnail((size, size), Image.ANTIALIAS)
tmpIO = BytesIO()
tmp.save(tmpIO, format='PNG')
tmpFile = InMemoryUploadedFile(
tmpIO, None, 'fav-%s.png' %
(size,), 'image/png', sys.getsizeof(tmpIO), None)
fav.faviconImage = tmpFile
fav.save()
return fav
示例4: test_shapefile
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def test_shapefile(self):
base = 'dir/geofield.shp'
path = default_storage.path(base)
os.mkdir(os.path.dirname(path))
write_shp(path, _geom)
b = io.BytesIO()
with zipfile.ZipFile(b, 'w') as zf:
for ext in ('dbf', 'prj', 'shp', 'shx'):
fname = base.replace('shp', ext)
with default_storage.open(fname) as fp:
zf.writestr(fname, fp.read())
shutil.rmtree(os.path.dirname(path))
upfile = SimpleUploadedFile('geofield.zip', b.getvalue())
b.close()
result = self.field.to_python(upfile)
self.assertIsInstance(result, OGRGeometry)
self.assertIsNotNone(result.srs)
示例5: create_image
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def create_image(multiband=False):
tmpname = os.path.join(
upload_to.path,
os.path.basename(tempfile.mktemp(prefix='tmin_', suffix='.tif')))
fp = default_storage.open(tmpname, 'w+b')
shape = (5, 5)
if multiband:
shape += (3,)
b = bytearray(range(reduce(operator.mul, shape)))
ras = raster.frombytes(bytes(b), shape)
ras.affine = (-120, 2, 0, 38, 0, -2)
ras.sref = 4326
ras.save(fp)
ras.close()
fp.seek(0)
return fp
示例6: test_open_read
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def test_open_read(self):
self.storage.location = 'root'
stream = io.BytesIO(b'Im a stream')
name = self.storage.save('path/some file.txt', stream)
fh = self.storage.open(name, 'r+b')
try:
self.assertEqual(fh.read(), b'Im a stream')
finally:
fh.close()
stream = io.BytesIO()
self.storage.service.get_blob_to_stream(
container_name=self.storage.azure_container,
blob_name='root/path/some file.txt',
stream=stream,
max_connections=1,
timeout=10)
stream.seek(0)
self.assertEqual(stream.read(), b'Im a stream')
示例7: test_open_read_write
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def test_open_read_write(self):
self.storage.location = 'root'
stream = io.BytesIO(b'Im a stream')
name = self.storage.save('file.txt', stream)
fh = self.storage.open(name, 'r+b')
try:
self.assertEqual(fh.read(), b'Im a stream')
fh.write(b' foo')
fh.seek(0)
self.assertEqual(fh.read(), b'Im a stream foo')
finally:
fh.close()
stream = io.BytesIO()
self.storage.service.get_blob_to_stream(
container_name=self.storage.azure_container,
blob_name='root/file.txt',
stream=stream,
max_connections=1,
timeout=10)
stream.seek(0)
self.assertEqual(stream.read(), b'Im a stream foo')
示例8: test_model_form
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def test_model_form(self):
files = {
'foo_file': SimpleUploadedFile(
name='foo.pdf',
content=b'foo content',
content_type='application/pdf')}
form = FooFileModelForm(data={}, files=files)
self.assertTrue(form.is_valid())
form.save()
self.assertTrue(default_storage.exists('foo_uploads/foo.pdf'))
# check file content was saved
fh = default_storage.open('foo_uploads/foo.pdf', 'r+b')
try:
self.assertEqual(fh.read(), b'foo content')
finally:
fh.close()
示例9: _join_chunks
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def _join_chunks(self):
try:
temp_file = default_storage.open(self.path, 'wb')
for chunk in self.chunks.all():
chunk_bytes = chunk.file.read()
temp_file.write(chunk_bytes)
temp_file.close()
self.final_file = self.path
self.state = self.STATE_COMPLETED
super(FlowFile, self).save()
# send ready signal
if self.join_in_background:
file_is_ready.send(self)
if FLOWJS_AUTO_DELETE_CHUNKS:
self.delete_chunks()
except Exception as e:
self.state = self.STATE_JOINING_ERROR
super(FlowFile, self).save()
# send error signal
if self.join_in_background:
file_joining_failed.send(self)
示例10: create_thumbnail
# 需要导入模块: from django.core.files.storage import default_storage [as 别名]
# 或者: from django.core.files.storage.default_storage import open [as 别名]
def create_thumbnail(self):
if not self.picture:
return False
picture_path, thumbnail_path = self.get_picture_paths()
if thumbnail_path and not storage.exists(thumbnail_path):
try:
picture_file = storage.open(picture_path, "r")
image = Image.open(picture_file)
image = image.crop(get_square_crop_points(image))
image = image.resize((THUMBNAIL_SIZE,
THUMBNAIL_SIZE),
Image.ANTIALIAS)
image.save(thumbnail_path)
except (IOError, KeyError, UnicodeDecodeError):
return False
return True