python类InMemoryUploadedFile()的实例源码

conftest.py 文件源码 项目:telemetry-analysis-service 作者: mozilla 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def notebook_maker():
    def maker(extension='ipynb'):
        return InMemoryUploadedFile(
            file=io.BytesIO(b'{}'),
            field_name='notebook',
            name='test-notebook.%s' % extension,
            content_type='text/plain',
            size=2,
            charset='utf8',
        )
    return maker
pillow_backend.py 文件源码 项目:website 作者: hackerspace-ntnu 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def create_thumbnail(file_path):
    thumbnail_filename = utils.get_thumb_filename(file_path)
    thumbnail_format = utils.get_image_format(os.path.splitext(file_path)[1])

    image = default_storage.open(file_path)
    image = Image.open(image)
    file_format = image.format

    # Convert to RGB if necessary
    # Thanks to Limodou on DjangoSnippets.org
    # http://www.djangosnippets.org/snippets/20/
    if image.mode not in ('L', 'RGB'):
        image = image.convert('RGB')

    # scale and crop to thumbnail
    imagefit = ImageOps.fit(image, THUMBNAIL_SIZE, Image.ANTIALIAS)
    thumbnail_io = BytesIO()
    imagefit.save(thumbnail_io, format=file_format)

    thumbnail = InMemoryUploadedFile(
        thumbnail_io,
        None,
        thumbnail_filename,
        thumbnail_format,
        len(thumbnail_io.getvalue()),
        None)
    thumbnail.seek(0)

    return default_storage.save(thumbnail_filename, thumbnail)
models.py 文件源码 项目:reggae_chicken 作者: hakumaku 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def clean(self):
        super().clean()
        # It will not allow any uppercase letters in 'email'.
        email_name, domain_part = self.email.strip().split('@')
        self.email = '@'.join([email_name.lower(), domain_part.lower()])
        # self.email = self.__class__.objects.normalize_email(self.email)
        if self.avatar is not None and self.avatar.name != self.USERPROFILE_IMAGE:
            from PIL import Image
            image = Image.open(self.avatar)
            image = image.resize((150,150), Image.ANTIALIAS)
            buffer = BytesIO()
            image = image.convert('RGB')
            image.save(fp=buffer, format='JPEG')
            image_content = ContentFile(buffer.getvalue())
            resized_image = InMemoryUploadedFile(
                # file(ContentFile)
                image_content,
                # field_name(idk)
                None,
                # name
                str(self.avatar),
                # content_type
                'image/jpeg',
                # size
                image_content.tell,
                # charset(idk)
                None
            )
            self.avatar = resized_image
models.py 文件源码 项目:lenuage 作者: laboiteproject 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def generate_qrcode(self):
        url = 'http://'
        url += str(Site.objects.get_current())
        url += '/boites/redirect/'
        url += str(self.api_key)
        img = qrcode.make(url)

        buffer = BytesIO()
        img.save(buffer)

        filename = 'qrcode-%s.png' % str(self.api_key)
        filebuffer = InMemoryUploadedFile(buffer, None, filename,
                                          'image/png', len(buffer.getvalue()), None)
        self.qrcode.save(filename, filebuffer)
vv.py 文件源码 项目:pyfeedback 作者: d120 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def replace_first_line(file, replacement):
    # suppress a type error which was confusing
    if type(file) is not InMemoryUploadedFile:
        with open(file, 'r', encoding='iso-8859-1') as f:
            data = f.readlines()

        if "!DOCTYPE" not in data[0]:
            data[0] = replacement
            with open(file, 'w') as f:
                f.writelines(data)
forms.py 文件源码 项目:django-thumbnail-blur 作者: Madalosso 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def generate_resized_image(source, size):
    img_l = Image.open(source)
    img_l.thumbnail(size, Image.ANTIALIAS)
    data_img = StringIO.StringIO()
    img_l.save(data_img, 'JPEG')
    img_l.close()
    img_temp_file = InMemoryUploadedFile(data_img, None,
                                         str(size[0]) + '.jpg', 'image/jpeg',
                                         data_img.len, None)
    return img_temp_file
phedited.py 文件源码 项目:IV 作者: collinmutembei 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def save(self, *args, **kwargs):
        """overrides the save method for the model
        """

        # fetches the original image from its url and loads it to memory
        image_request = requests.get(self.original_image)
        image_bytes = io.BytesIO(image_request.content)
        image = Image.open(image_bytes)
        filename = os.path.basename(self.original_image)
        # call the apply_effect method on the in-memory original image
        image_with_effect = apply_effects(image, self.effects)
        # creat an empty in-memory bytes section to hold the edited image
        edited_image = io.BytesIO()
        image_with_effect.save(edited_image, format=image.format)
        # initiate an upload from the in-memory edited image
        edited_file = InMemoryUploadedFile(
            edited_image,
            None,
            filename,
            'image/jpeg',
            edited_image.tell,
            None
        )

        self.phedited_image.save(
            filename,
            edited_file,
            save=False,
        )
        super(PheditedImage, self).save(*args, **kwargs)
test_xform_submission_api.py 文件源码 项目:FormShare 作者: qlands 项目源码 文件源码 阅读 18 收藏 0 点赞 0 评论 0
def test_post_submission_anonymous(self):
        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(
                    '%s/submission' % self.user.username, data)
                request.user = AnonymousUser()
                response = self.view(request, username=self.user.username)
                self.assertContains(response, 'Successful submission',
                                    status_code=201)
                self.assertTrue(response.has_header('X-OpenRosa-Version'))
                self.assertTrue(
                    response.has_header('X-OpenRosa-Accept-Content-Length'))
                self.assertTrue(response.has_header('Date'))
                self.assertEqual(response['Content-Type'],
                                 'text/xml; charset=utf-8')
                self.assertEqual(response['Location'],
                                 'http://testserver/%s/submission'
                                 % self.user.username)
test_xform_submission_api.py 文件源码 项目:FormShare 作者: qlands 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def test_post_submission_authenticated(self):
        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)
                auth = DigestAuth('bob', 'bobbob')
                request.META.update(auth(request.META, response))
                response = self.view(request, username=self.user.username)
                self.assertContains(response, 'Successful submission',
                                    status_code=201)
                self.assertTrue(response.has_header('X-OpenRosa-Version'))
                self.assertTrue(
                    response.has_header('X-OpenRosa-Accept-Content-Length'))
                self.assertTrue(response.has_header('Date'))
                self.assertEqual(response['Content-Type'],
                                 'text/xml; charset=utf-8')
                self.assertEqual(response['Location'],
                                 'http://testserver/submission')
test_metadata_viewset.py 文件源码 项目:FormShare 作者: qlands 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def test_windows_csv_file_upload_to_metadata(self):
        data_value = 'transportation.csv'
        path = os.path.join(self.fixture_dir, data_value)
        with open(path) as f:
            f = InMemoryUploadedFile(
                f, 'media', data_value, 'application/octet-stream', 2625, None)
            data = {
                'data_value': data_value,
                'data_file': f,
                'data_type': 'media',
                'xform': self.xform.pk
            }
            self._post_form_metadata(data)
            self.assertEqual(self.metadata.data_file_type, 'text/csv')
data_dictionary.py 文件源码 项目:FormShare 作者: qlands 项目源码 文件源码 阅读 16 收藏 0 点赞 0 评论 0
def set_object_permissions(sender, instance=None, created=False, **kwargs):
    # seems the super is not called, have to get xform from here
    xform = XForm.objects.get(pk=instance.pk)

    if created:
        from formshare.libs.permissions import OwnerRole

        OwnerRole.add(instance.user, xform)

        if instance.created_by and instance.user != instance.created_by:
            OwnerRole.add(instance.created_by, xform)

        from formshare.libs.utils.project_utils import set_project_perms_to_xform
        set_project_perms_to_xform(xform, instance.project)

    if hasattr(instance, 'has_external_choices') \
            and instance.has_external_choices:
        instance.xls.seek(0)
        f = sheet_to_csv(instance.xls.read(), 'external_choices')
        f.seek(0, os.SEEK_END)
        size = f.tell()
        f.seek(0)

        from formshare.apps.main.models.meta_data import MetaData
        data_file = InMemoryUploadedFile(
            file=f,
            field_name='data_file',
            name='itemsets.csv',
            content_type='text/csv',
            size=size,
            charset=None
        )

        MetaData.media_upload(xform, data_file)
import_tools.py 文件源码 项目:FormShare 作者: qlands 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def django_file(path, field_name, content_type):
    # adapted from here:
    # http://groups.google.com/group/django-users/browse_thread/thread/
    # 834f988876ff3c45/
    f = open(path)
    return InMemoryUploadedFile(
        file=f,
        field_name=field_name,
        name=f.name,
        content_type=content_type,
        size=os.path.getsize(path),
        charset=None
    )
viewer_tools.py 文件源码 项目:FormShare 作者: qlands 项目源码 文件源码 阅读 17 收藏 0 点赞 0 评论 0
def django_file(path, field_name, content_type):
    # adapted from here: http://groups.google.com/group/django-users/browse_th\
    # read/thread/834f988876ff3c45/
    f = open(path)
    return InMemoryUploadedFile(
        file=f,
        field_name=field_name,
        name=f.name,
        content_type=content_type,
        size=os.path.getsize(path),
        charset=None
    )


问题


面经


文章

微信
公众号

扫码关注公众号