本文整理匯總了Python中django.core.files.uploadedfile.InMemoryUploadedFile方法的典型用法代碼示例。如果您正苦於以下問題:Python uploadedfile.InMemoryUploadedFile方法的具體用法?Python uploadedfile.InMemoryUploadedFile怎麽用?Python uploadedfile.InMemoryUploadedFile使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.core.files.uploadedfile
的用法示例。
在下文中一共展示了uploadedfile.InMemoryUploadedFile方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: sms_media_to_file
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def sms_media_to_file(file_object, name):
if isinstance(file_object, basestring):
file_object = io.BytesIO(file_object)
def getsize(f):
f.seek(0)
f.read()
s = f.tell()
f.seek(0)
return s
name = name.strip()
content_type, charset = mimetypes.guess_type(name)
size = getsize(file_object)
return InMemoryUploadedFile(file=file_object, name=name,
field_name=None, content_type=content_type,
charset=charset, size=size)
示例2: create_media
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def create_media(media):
"""Download media link"""
if is_valid_url(media.data_value):
filename = media.data_value.split('/')[-1]
data_file = NamedTemporaryFile()
content_type = mimetypes.guess_type(filename)
with closing(requests.get(media.data_value, stream=True)) as r:
for chunk in r.iter_content(chunk_size=CHUNK_SIZE):
if chunk:
data_file.write(chunk)
data_file.seek(os.SEEK_SET, os.SEEK_END)
size = os.path.getsize(data_file.name)
data_file.seek(os.SEEK_SET)
media.data_value = filename
media.data_file = InMemoryUploadedFile(
data_file, 'data_file', filename, content_type,
size, charset=None)
return media
return None
示例3: test_windows_csv_file_upload
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def test_windows_csv_file_upload(self):
count = MetaData.objects.filter(data_type='media').count()
media_file = os.path.join(
self.this_directory, 'fixtures', 'transportation',
'transportation.csv')
f = InMemoryUploadedFile(open(media_file),
'media',
'transportation.csv',
'application/octet-stream',
2625,
None)
MetaData.media_upload(self.xform, f)
media_list = MetaData.objects.filter(data_type='media')
new_count = media_list.count()
self.assertEqual(count + 1, new_count)
media = media_list.get(data_value='transportation.csv')
self.assertEqual(media.data_file_type, 'text/csv')
示例4: test_post_submission_require_auth_anonymous_user
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def test_post_submission_require_auth_anonymous_user(self):
self.user.profile.require_auth = True
self.user.profile.save()
count = Attachment.objects.count()
s = self.surveys[0]
media_file = "1335783522563.jpg"
path = os.path.join(self.main_directory, 'fixtures',
'transportation', 'instances', s, media_file)
with open(path) as f:
f = InMemoryUploadedFile(f, 'media_file', media_file, 'image/jpg',
os.path.getsize(path), None)
submission_path = os.path.join(
self.main_directory, 'fixtures',
'transportation', 'instances', s, s + '.xml')
with open(submission_path) as sf:
data = {'xml_submission_file': sf, 'media_file': f}
request = self.factory.post('/submission', data)
response = self.view(request)
self.assertEqual(response.status_code, 401)
response = self.view(request, username=self.user.username)
self.assertEqual(response.status_code, 401)
self.assertEqual(count, Attachment.objects.count())
示例5: retrieve
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def retrieve(self, key, field_name):
metadata = self.metadata(key)
content = self.backend.get(self.prefix(key) + "_content")
if metadata and content:
out = BytesIO()
out.write(content)
upload = InMemoryUploadedFile(
file=out,
field_name=field_name,
name=metadata["name"],
content_type=metadata["content_type"],
size=metadata["size"],
charset=metadata["charset"],
)
upload.file.seek(0)
else:
upload = None
return upload
示例6: save
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def save(self, *args, **kwargs):
existing_avatar = ForumAvatar.objects.filter(user=self.user).first()
if existing_avatar:
self.id = existing_avatar.id
if not self.image:
self.use_gravatar = True
else:
i = Image.open(self.image)
i.thumbnail((120, 120), Image.ANTIALIAS)
i_io = BytesIO()
i.save(i_io, format='PNG')
self.image = InMemoryUploadedFile(
i_io, None, '%s.png' % self.user_id, 'image/png', None, None
)
print(self.image)
super(ForumAvatar, self).save(*args, **kwargs)
示例7: get_sample_objective
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def get_sample_objective():
dir_path = os.path.dirname(os.path.realpath(__file__))
description_content = "Super objective"
description_filename = "description.md"
description = get_temporary_text_file(description_content, description_filename)
metrics_filename = "metrics.zip"
f = BytesIO(b'')
with open(os.path.join(dir_path,
'../../../fixtures/chunantes/objectives/objective0/metrics.zip'), 'rb') as zip_file:
flength = f.write(zip_file.read())
metrics = InMemoryUploadedFile(f, None, metrics_filename,
'application/zip', flength, None)
metrics.seek(0)
return description, description_filename, metrics, metrics_filename
示例8: simple_logo
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def simple_logo():
"""
Fixture for a simple png image for repositories
"""
# 1x1 px image
simple_png_image = b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x02\x00\x00\x00\x90wS\xde\x00\x00\x00\tpHYs\x00\x00\x0b\x13\x00\x00\x0b\x13\x01\x00\x9a\x9c\x18\x00\x00\x00\x07tIME\x07\xdf\n\x12\x0c+\x19\x84\x1d/"\x00\x00\x00\x19tEXtComment\x00Created with GIMPW\x81\x0e\x17\x00\x00\x00\x0cIDAT\x08\xd7c\xa8\xa9\xa9\x01\x00\x02\xec\x01u\x90\x90\x1eL\x00\x00\x00\x00IEND\xaeB`\x82'
logo = InMemoryUploadedFile(
BytesIO(simple_png_image),
None,
'logo.png',
'image/png',
len(simple_png_image),
None,
None
)
return logo
示例9: setUpForProtocol
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def setUpForProtocol(self, protocol_class, repository):
self.oaisource, _ = OaiSource.objects.get_or_create(
identifier='deposit_oaisource',
name='Repository OAI source',
default_pubtype='preprint')
logo = InMemoryUploadedFile(
BytesIO(simple_png_image),
None, 'logo.png',
'image/png', len(simple_png_image), None, None)
self.repo = repository
self.repo.oaisource = self.oaisource
self.repo.logo = logo
if not self.repo.description:
self.repo.description = 'brsuatiercs'
if not self.repo.name:
self.repo.name = 'Test Repository'
self.repo.protocol = protocol_class.__name__
self.repo.save()
protocol_registry.register(protocol_class)
self.proto = protocol_class(self.repo)
self.form = None
示例10: get_favicon
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [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
示例11: get_wxa_code_unlimited_file
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def get_wxa_code_unlimited_file(file_name, scene, **kwargs):
file = BytesIO()
kw = dict()
for k in ('width', 'auto_color', 'line_color', 'page', 'is_hyaline'):
if k in kwargs:
kw[k] = kwargs[k]
content = wechat.wxa.get_wxa_code_unlimited(scene, **kw)
file.write(content.content)
file.seek(0)
return InMemoryUploadedFile(
file=file,
field_name="",
name=file_name,
content_type="image/jpeg",
size=0,
charset="",
content_type_extra=""
)
示例12: crop_image
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def crop_image(self, data_image, ratio):
image = Image.open(data_image)
cropped_image = image.crop(ratio)
# saving image to memory
thumb_io = io.BytesIO()
cropped_image.save(
thumb_io,
data_image.content_type.split('/')[-1].upper()
)
# creating new InMemoryUploadedFile() based on the modified file
file = InMemoryUploadedFile(
thumb_io,
'photo',
data_image._get_name(),
data_image.content_type,
None, None,
)
return file
示例13: save
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [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
示例14: _tinypng_images
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [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
示例15: save_image
# 需要導入模塊: from django.core.files import uploadedfile [as 別名]
# 或者: from django.core.files.uploadedfile import InMemoryUploadedFile [as 別名]
def save_image(self, imagefile, save_path, file_ext, mime_type):
"""
Save an image to self.storage at `save_path`.
Arguments:
`imagefile`: Raw image data, typically a BytesIO instance.
`save_path`: The path within self.storage where the image should
be saved.
`file_ext`: The file extension of the image-to-be-saved.
`mime_type`: A valid image mime type (as found in
versatileimagefield.utils)
"""
file_to_save = InMemoryUploadedFile(
imagefile,
None,
'foo.%s' % file_ext,
mime_type,
imagefile.tell(),
None
)
file_to_save.seek(0)
self.storage.save(save_path, file_to_save)