本文整理匯總了Python中django.core.files.uploadedfile.TemporaryUploadedFile方法的典型用法代碼示例。如果您正苦於以下問題:Python uploadedfile.TemporaryUploadedFile方法的具體用法?Python uploadedfile.TemporaryUploadedFile怎麽用?Python uploadedfile.TemporaryUploadedFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.core.files.uploadedfile
的用法示例。
在下文中一共展示了uploadedfile.TemporaryUploadedFile方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: save
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def save(self, commit=True, use_card_filenames=False):
instance = super(TinyPngForm, self).save(commit=False)
for field in self.fields.keys():
if (hasattr(instance, field)
and field in dir(self.Meta.model)
and type(self.Meta.model._meta.get_field(field)) == models.models.ImageField):
image = self.cleaned_data[field]
if image and (isinstance(image, InMemoryUploadedFile) or isinstance(image, TemporaryUploadedFile)):
filename = image.name
_, extension = os.path.splitext(filename)
if extension.lower() == '.png':
image = shrinkImageFromData(image.read(), filename)
if use_card_filenames and field in models.cardsImagesToName:
image.name = models.cardsImagesToName[field]({
'id': instance.id,
'firstname': instance.idol.name.split(' ')[-1] if instance.idol and instance.idol.name else 'Unknown',
})
else:
image.name = randomString(32) + extension
setattr(instance, field, image)
if commit:
instance.save()
return instance
示例2: _tinypng_images
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def _tinypng_images(self, validated_data):
idolName = self.context['request'].data.get('idol', None)
if not idolName:
idolName = self.instance.idol.name
idolId = validated_data['id'] if 'id' in validated_data else self.instance.id
for (field, value) in validated_data.items():
if value and (isinstance(value, InMemoryUploadedFile) or isinstance(value, TemporaryUploadedFile)):
filename = value.name
value = shrinkImageFromData(value.read(), filename)
validated_data[field] = value
if field in models.cardsImagesToName:
value.name = models.cardsImagesToName[field]({
'id': idolId,
'firstname': idolName.split(' ')[-1] if idolName else 'Unknown',
})
return validated_data
示例3: __setstate__
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def __setstate__(self, pickle_dictionary):
if pickle_dictionary["_context"]["type"] == "file.content" and \
isinstance(pickle_dictionary["_context"]["value"], dict):
arguments = pickle_dictionary["_context"]["value"]
# File data info, especially useful for test pickling
self.base64_file_data_len = len(arguments["content"])
file_content = base64.b64decode(arguments.pop("content"))
# File data info, especially useful for test pickling
self.file_data_len = len(file_content)
file_object = TemporaryUploadedFile(**arguments)
file_object.write(file_content)
file_object.flush()
pickle_dictionary["_context"]["value"] = file_object
self.__dict__.update(pickle_dictionary)
示例4: temporary_uploaded_file
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def temporary_uploaded_file():
def inner(filename, content):
fp = uploadedfile.TemporaryUploadedFile(
name=filename + ".tempfile",
content_type='text/plain',
size=0,
charset='utf8',
)
fp.write(content)
fp.flush()
return fp
return inner
示例5: process_zip
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def process_zip(self):
with file_storage.get_document_as_local_fn(self.source_path) as (local_file_path, _):
with zipfile.ZipFile(local_file_path) as zip_file:
zip_file_filelist = zip_file.filelist
self.log_info('Start extracting {} documents from {}'.format(
len(zip_file_filelist), local_file_path))
for n, a_file in enumerate(zip_file_filelist):
if a_file.is_dir():
continue
file_size = a_file.file_size
file_name = os.path.basename(a_file.filename)
mime_type = self.get_mime_type(file_name)
self.log_info(
'Extract/start LoadDocument for {} of {} files: name={}, size={}, mime_type={}'.format(
n + 1, len(zip_file_filelist), file_name, file_size, mime_type))
with TemporaryUploadedFile(file_name, mime_type, file_size, 'utf-8') as tempfile:
tempfile.file = zip_file.open(a_file)
tempfile.file.seek = lambda *args: 0
self.upload_file(
file_name=file_name,
file_size=file_size,
contents=tempfile,
directory_path=self.directory_path)
示例6: process_tar
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def process_tar(self):
with file_storage.get_document_as_local_fn(self.source_path) as (local_file_path, _):
with tarfile.TarFile(local_file_path) as tar_file:
tar_file_members = tar_file.getmembers()
self.log_info('Start extracting {} documents from {}'.format(
len(tar_file_members), local_file_path))
for n, a_file in enumerate(tar_file_members):
if a_file.isdir():
continue
file_size = a_file.size
file_name = os.path.basename(a_file.name)
mime_type = self.get_mime_type(file_name)
self.log_info(
'Extract/start LoadDocument for {} of {} files: name={}, size={}, mime_type={}'.format(
n + 1, len(tar_file_members), file_name, file_size, mime_type))
with TemporaryUploadedFile(file_name, mime_type, file_size, 'utf-8') as tempfile:
tempfile.file = tar_file.extractfile(a_file)
self.upload_file(
file_name=file_name,
file_size=file_size,
contents=tempfile,
directory_path=self.directory_path)
示例7: temporary_uploaded_file
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def temporary_uploaded_file():
def inner(filename, content):
fp = uploadedfile.TemporaryUploadedFile(
name=filename + '.tempfile',
content_type='text/plain',
size=0,
charset='utf8',
)
fp.write(content)
fp.flush()
return fp
return inner
示例8: validate
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def validate(self, data):
if self.context['request'].method == 'POST' and ('idol' not in self.context['request'].data or not self.context['request'].data['idol']):
raise serializers.ValidationError({
'idol': ['This field is required.'],
})
for (field, value) in data.items():
if value and (isinstance(value, InMemoryUploadedFile) or isinstance(value, TemporaryUploadedFile)):
_, extension = os.path.splitext(value.name)
if extension.lower() != '.png':
raise serializers.ValidationError({
field: ['Only png images are accepted.'],
})
return data
示例9: clean_file
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def clean_file(self):
file = self.cleaned_data['file']
if not magic:
raise forms.ValidationError(_("The file could not be validated"))
# Won't ever raise. Has at most one '.' so lstrip is fine here
ext = os.path.splitext(file.name)[1].lstrip('.').lower()
if ext not in settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE:
raise forms.ValidationError(
_("Unsupported file extension %(extension)s. "
"Supported extensions are %(supported)s.") % {
'extension': ext,
'supported': ", ".join(
sorted(settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE.keys()))})
try:
if isinstance(file, TemporaryUploadedFile):
file_mime = magic.from_file(file.temporary_file_path(), mime=True)
else: # In-memory file
file_mime = magic.from_buffer(file.read(), mime=True)
except magic.MagicException as e:
logger.exception(e)
raise forms.ValidationError(_("The file could not be validated"))
mime = settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE.get(ext, None)
if mime != file_mime:
raise forms.ValidationError(
_("Unsupported file mime type %(mime)s. "
"Supported types are %(supported)s.") % {
'mime': file_mime,
'supported': ", ".join(
sorted(settings.ST_ALLOWED_UPLOAD_FILE_MEDIA_TYPE.values()))})
return file
示例10: new_file
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def new_file(self, file_name, *args, **kwargs):
"""
Create the file object to append to as data is coming in.
"""
super(TemporaryFileUploadHandler, self).new_file(file_name, *args, **kwargs)
self.file = TemporaryUploadedFile(self.file_name, self.content_type, 0, self.charset)
示例11: test_move_temporary_file
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def test_move_temporary_file(self):
"""
The temporary uploaded file is moved rather than copied to the
destination.
"""
with TemporaryUploadedFile('something.txt', 'text/plain', 0, 'UTF-8') as tmp_file:
tmp_file_path = tmp_file.temporary_file_path()
Document.objects.create(myfile=tmp_file)
self.assertFalse(os.path.exists(tmp_file_path), 'Temporary file still exists')
示例12: test_file_context
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import TemporaryUploadedFile [as 別名]
def test_file_context(self):
response = None
with open("threats/test_data/boss.gif", "rb") as boss_reader:
file_data = boss_reader.read()
boss_reader.seek(0)
response = self.client.post(
'/',
{'artifact': '{"type": "file.content"}', 'file': boss_reader},
format="multipart"
)
upload_file_args = {
"name": "boss.gif",
"content_type": "application/octet-stream",
"size": 29927,
"charset": None,
}
self.assertEqual(response.status_code, 200)
temp_file = TemporaryUploadedFile(**upload_file_args)
temp_file.write(file_data)
temp_file.flush()
context = SearchContext({"type": "file.content", "value": temp_file})
context.save()
self.assertEqual(context.file_data_len, len(file_data))
loaded_context = SearchContext.load(context.id)
self.assertEqual(loaded_context.base64_file_data_len, context.base64_file_data_len)
self.assertEqual(loaded_context.file_data_len, context.file_data_len)
with open(loaded_context.value.temporary_file_path(), "rb") as temp_file:
loaded_file_data = temp_file.read()
for counter in range(0, len(loaded_file_data) // 100):
begin = counter * 100
end = begin + 100
self.assertEqual(file_data[begin:end], loaded_file_data[begin:end])
self.assertEqual(len(file_data), len(loaded_file_data))