当前位置: 首页>>代码示例>>Python>>正文


Python uploadedfile.InMemoryUploadedFile方法代码示例

本文整理汇总了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) 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:19,代码来源:tools.py

示例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 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:23,代码来源:meta_data.py

示例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') 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:19,代码来源:test_form_metadata.py

示例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()) 
开发者ID:awemulya,项目名称:kobo-predict,代码行数:24,代码来源:test_xform_submission_api.py

示例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 
开发者ID:mozilla,项目名称:telemetry-analysis-service,代码行数:20,代码来源:cache.py

示例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) 
开发者ID:ericls,项目名称:niji,代码行数:18,代码来源:models.py

示例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 
开发者ID:SubstraFoundation,项目名称:substra-backend,代码行数:20,代码来源:common.py

示例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 
开发者ID:dissemin,项目名称:dissemin,代码行数:18,代码来源:conftest.py

示例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 
开发者ID:dissemin,项目名称:dissemin,代码行数:24,代码来源:test_protocol.py

示例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 
开发者ID:arteria,项目名称:django-favicon-plus,代码行数:25,代码来源:models.py

示例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=""
    ) 
开发者ID:007gzs,项目名称:meeting,代码行数:20,代码来源:biz.py

示例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 
开发者ID:dima-kov,项目名称:django-croppie,代码行数:22,代码来源:fields.py

示例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 
开发者ID:MagiCircles,项目名称:SchoolIdolAPI,代码行数:25,代码来源:forms.py

示例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 
开发者ID:MagiCircles,项目名称:SchoolIdolAPI,代码行数:18,代码来源:serializers.py

示例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) 
开发者ID:respondcreate,项目名称:django-versatileimagefield,代码行数:24,代码来源:base.py


注:本文中的django.core.files.uploadedfile.InMemoryUploadedFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。