本文整理汇总了Python中django.core.files.uploadedfile.TemporaryUploadedFile.temporary_file_path方法的典型用法代码示例。如果您正苦于以下问题:Python TemporaryUploadedFile.temporary_file_path方法的具体用法?Python TemporaryUploadedFile.temporary_file_path怎么用?Python TemporaryUploadedFile.temporary_file_path使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类django.core.files.uploadedfile.TemporaryUploadedFile
的用法示例。
在下文中一共展示了TemporaryUploadedFile.temporary_file_path方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: value_from_datadict
# 需要导入模块: from django.core.files.uploadedfile import TemporaryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.TemporaryUploadedFile import temporary_file_path [as 别名]
def value_from_datadict(self, data, files, name):
""" Normally returns files.get(name, None). Here we also check `data`.
-- if the appropriate hidden _sticky_file input is set, we can look for
the temporary file instead and return that if it exists.
This method seems to be called multiple times with the same arguments,
so to prevent excessive storage activity the return value is cached
and returned without processing on subsequent calls.
There is an assumption that the arguments will not change between calls
for any given instance, which appears to be valid, so no argument
checks are performed.
"""
if hasattr(self, '_value'):
return self._value
self.user_token = data.get('csrfmiddlewaretoken', None)
# look for normal file
value = super(
StickyFileInput, self).value_from_datadict(data, files, name)
if value and hasattr(value, 'name'):
# got one, save a temporary copy just in case
self.sticky_file_name = value.name
self.sticky_session_id = '%.6f' % time.time()
self.save_sticky_copy(value.file)
else: # check for temporary copy
self.sticky_file_name = (
data.get(
self.get_hidden_input_name(name, 'sticky_file'), None))
self.sticky_session_id = data.get(
self.get_hidden_input_name(name, 'sticky_session_id'), None)
sticky_copy = self.load_sticky_copy()
if sticky_copy:
sticky_copy.seek(0, 2) # seek to end
value = TemporaryUploadedFile(
name = self.sticky_file_name,
content_type = None,
size = sticky_copy.tell(),
charset = None
)
value.file = sticky_copy
value.file.seek(0)
value.temporary_file_path = lambda: self.get_sticky_path()
setattr(self, '_value', value) # cache
return self._value
示例2: test_with_temporary_uploaded_file
# 需要导入模块: from django.core.files.uploadedfile import TemporaryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.TemporaryUploadedFile import temporary_file_path [as 别名]
def test_with_temporary_uploaded_file(self):
temp_uploaded_file = TemporaryUploadedFile(
name='test.jpg',
content_type='image/jpeg',
size=100,
charset=None,
)
with open(temp_uploaded_file.temporary_file_path(), 'wb') as f:
f.write(b'0'*100)
file_upload = FileUpload()
filename = file_upload.serialize_value(temp_uploaded_file)
self.assertEqual(filename, file_upload.serialize_value(temp_uploaded_file))
uploaded_file_path = os.path.join(
settings.MEDIA_ROOT,
file_upload.generate_filename(temp_uploaded_file.name)
)
os.remove(uploaded_file_path)
示例3: test_rack_form_clean_photo
# 需要导入模块: from django.core.files.uploadedfile import TemporaryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.TemporaryUploadedFile import temporary_file_path [as 别名]
def test_rack_form_clean_photo(self):
from fixcity.exif_utils import get_exif_info
from PIL import Image
import os.path
data = self.data.copy()
# Jump through a few hoops to simulate a real upload.
HERE = os.path.abspath(os.path.dirname(__file__))
path = os.path.join(HERE, 'files', 'test_exif.jpg')
content = open(path).read()
photofile = TemporaryUploadedFile('test_exif.jpg', 'image/jpeg',
len(content), None)
photofile.write(content)
photofile.seek(0)
# Okay, now we have something like a file upload.
data['photo'] = photofile
form = RackForm(data, {'photo': photofile})
self.assert_(form.is_valid())
# Make sure it doesn't have a bad rotation.
self.assertEqual({},
get_exif_info(Image.open(photofile.temporary_file_path())))
示例4: ensure_saved
# 需要导入模块: from django.core.files.uploadedfile import TemporaryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.TemporaryUploadedFile import temporary_file_path [as 别名]
def ensure_saved(self, file):
"""This may create a temporary file, which will be deleted when
it's closed, so always close() it but only when you've finished!"""
if isinstance(file, InMemoryUploadedFile):
print "Writing %s to disk (%d bytes)" % (file, file.size)
tmp = TemporaryUploadedFile(name=file.name,
content_type=file.content_type, size=file.size,
charset=file.charset)
file.seek(0)
buf = file.read()
tmp.write(buf)
print "Wrote %d bytes" % len(buf)
tmp.flush()
else:
tmp = file
if isinstance(tmp, TemporaryUploadedFile):
path = tmp.temporary_file_path()
else:
path = tmp.name
return (tmp, path)