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


Python InMemoryUploadedFile.read方法代码示例

本文整理汇总了Python中django.core.files.uploadedfile.InMemoryUploadedFile.read方法的典型用法代码示例。如果您正苦于以下问题:Python InMemoryUploadedFile.read方法的具体用法?Python InMemoryUploadedFile.read怎么用?Python InMemoryUploadedFile.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在django.core.files.uploadedfile.InMemoryUploadedFile的用法示例。


在下文中一共展示了InMemoryUploadedFile.read方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: Avatar

# 需要导入模块: from django.core.files.uploadedfile import InMemoryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.InMemoryUploadedFile import read [as 别名]
class Avatar(models.Model):
    user = models.ForeignKey(User, unique=True)
    avatar = models.ImageField(max_length=1024, upload_to=content_file_name, blank=True, null=True)
    thumbnail = models.ImageField(max_length=1024, upload_to=content_file_name, blank=True, null=True)

    def save(self, *args, **kwargs):
        if self.avatar:
            avatar = Img.open(StringIO.StringIO(self.avatar.read()))
            avatar.thumbnail((100, 100), Img.ANTIALIAS)
            output = StringIO.StringIO()
            avatar.save(output, format='PNG', quality=75)
            output.seek(0)
            self.avatar = InMemoryUploadedFile(output, 'ImageField', "%s.png" % self.user.username, 'image/png',
                                               output.len, None)
            thumbnail = Img.open(StringIO.StringIO(self.avatar.read()))
            thumbnail.thumbnail((25, 25), Img.ANTIALIAS)
            output = StringIO.StringIO()
            thumbnail.save(output, format='PNG', quality=75)
            output.seek(0)
            self.thumbnail = InMemoryUploadedFile(output, 'ImageField', "mini_%s.png" % self.user.username, 'image/png',
                                                  output.len, None)
        super(Avatar, self).save(*args, **kwargs)

    class Meta:
        verbose_name = 'Avatar'
        verbose_name_plural = 'Avatary'
开发者ID:piotrpalkiewicz,项目名称:poetry,代码行数:28,代码来源:models.py

示例2: create_thumbnail

# 需要导入模块: from django.core.files.uploadedfile import InMemoryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.InMemoryUploadedFile import read [as 别名]
def create_thumbnail(file_path):
    thumbnail_filename = utils.get_thumb_filename(os.path.basename(file_path))
    thumbnail_format = utils.get_image_format(os.path.splitext(file_path)[1])
    file_format = thumbnail_format.split('/')[1]

    image_from_url = cStringIO.StringIO(urllib.urlopen(file_path).read())
    image = Image.open(image_from_url)

    # 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)

    cc = CloudContainer('mediaplan-images')
    data = thumbnail.read()
    cc.upload_data(filename=thumbnail_filename, data=data)
    return thumbnail_filename
开发者ID:smartermarketing,项目名称:django-ckeditor,代码行数:34,代码来源:rackspace_backend.py

示例3: save

# 需要导入模块: from django.core.files.uploadedfile import InMemoryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.InMemoryUploadedFile import read [as 别名]
    def save(self, commit=True):
        """
        Saves the uploaded image in the designated location.

        If image type is not SVG, a byteIO of image content_type is created and
        subsequently save; otherwise, the SVG is saved in its existing ``charset``
        as an ``image/svg+xml``.

        *Note*: The dimension of image files (excluding SVG) are set using ``PIL``.

        :param commit: If ``True``, the file is saved to the disk;
                       otherwise, it is held in the memory.
        :type commit: bool
        :return: An instance of saved image if ``commit is True``,
                 else ``namedtuple(path, image)``.
        :rtype: bool, namedtuple
        """
        image = self.files.get('image')
        content_type = image.content_type
        file_name = image.name
        image_extension = content_type.split('/')[-1].upper()
        image_size = image.size

        if content_type.lower() != self._SVG_TYPE:
            # Processing the raster graphic image.
            # Note that vector graphics in SVG format
            # do not require additional processing and
            # may be stored as uploaded.
            image = self._process_raster(image, image_extension)
            image_size = image.tell()
            image.seek(0, SEEK_SET)

        # Processed file (or the actual file in the case of SVG) is now
        # saved in the memory as a Django object.
        uploaded_image = InMemoryUploadedFile(
            file=image,
            field_name=None,
            name=file_name,
            content_type=content_type,
            size=image_size,
            charset=None
        )

        if (content_type.lower() == self._SVG_TYPE
                and MARKDOWNX_SVG_JAVASCRIPT_PROTECTION
                and xml_has_javascript(uploaded_image.read())):

            raise MarkdownxImageUploadError(
                'Failed security monitoring: SVG file contains JavaScript.'
            )

        return self._save(uploaded_image, file_name, commit)
开发者ID:adi-,项目名称:django-markdownx,代码行数:54,代码来源:forms.py

示例4: test_open_resets_file_to_start_and_returns_context_manager

# 需要导入模块: from django.core.files.uploadedfile import InMemoryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.InMemoryUploadedFile import read [as 别名]
 def test_open_resets_file_to_start_and_returns_context_manager(self):
     uf = InMemoryUploadedFile(StringIO('1'), '', 'test', 'text/plain', 1, 'utf8')
     uf.read()
     with uf.open() as f:
         self.assertEqual(f.read(), '1')
开发者ID:LouisAmon,项目名称:django,代码行数:7,代码来源:tests.py

示例5: Student

# 需要导入模块: from django.core.files.uploadedfile import InMemoryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.InMemoryUploadedFile import read [as 别名]
class Student(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, limit_choices_to=Q(groups__name='Students'),
                                related_name='student')
    first_name_en = models.CharField(max_length=50)
    last_name_en = models.CharField(max_length=50)
    first_name_ua = models.CharField(max_length=50)
    last_name_ua = models.CharField(max_length=50)
    course = models.IntegerField(default=3, choices=((1, _('first')), (2, _('second')), (3, _('third')), (4, _('fourth')),
                                                     (5, _('fifth')), (6, _('sixth'))))
    institution = models.CharField(max_length=100,
                                   choices=(('TSNUK', _('Taras Shevchenko National University of Kyiv')),
                                            ('UKMA', _('National University of Kyiv-Mohyla Academy'))))
    group = models.CharField(max_length=100, choices=(('HEP', _('HEP')), ('nuclear', _('Nuclear physics')), ))
    interests_en = models.TextField()
    interests_ua = models.TextField()
    join_date = models.DateField('date joined', auto_now=True)
    photo = models.ImageField(upload_to='students', default='default.jpg')
    photo_small = models.ImageField(upload_to='students_small', default='default_small.jpg', editable=False)

    def __unicode__(self):  # __unicode__ on Python 2
        return '%s  %s' % (self.first_name_en, self.last_name_en)

    def name_en(self):
        return '%s  %s' % (self.first_name_en, self.last_name_en)

    def name_ua(self):
        return '%s  %s' % (self.first_name_ua, self.last_name_ua)

    def save(self, *args, **kwargs):
        if self.photo:
            if Student.objects.filter(pk=self.pk).exists():
                if self.photo != Student.objects.get(pk=self.pk).photo:
                    image = Img.open(StringIO.StringIO(self.photo.read()))
                    image.thumbnail((200, 200), Img.ANTIALIAS)
                    output = StringIO.StringIO()
                    image.save(output, format='JPEG', quality=75)
                    output.seek(0)
                    self.photo = InMemoryUploadedFile(output, 'ImageField', "%s" % self.photo.name, 'image/jpeg',
                                                      output.len, None)
                    image = Img.open(StringIO.StringIO(self.photo.read()))
                    image.thumbnail((50, 50), Img.ANTIALIAS)
                    output = StringIO.StringIO()
                    image.save(output, format='JPEG', quality=75)
                    output.seek(0)
                    self.photo_small = InMemoryUploadedFile(output, 'ImageField', "%s" % self.photo.name, 'image/jpeg',
                                                            output.len, None)
            else:
                image = Img.open(StringIO.StringIO(self.photo.read()))
                image.thumbnail((200, 200), Img.ANTIALIAS)
                output = StringIO.StringIO()
                image.save(output, format='JPEG', quality=75)
                output.seek(0)
                self.photo = InMemoryUploadedFile(output, 'ImageField', "%s" % self.photo.name, 'image/jpeg',
                                                  output.len, None)
                image = Img.open(StringIO.StringIO(self.photo.read()))
                image.thumbnail((50, 50), Img.ANTIALIAS)
                output = StringIO.StringIO()
                image.save(output, format='JPEG', quality=75)
                output.seek(0)
                self.photo_small = InMemoryUploadedFile(output, 'ImageField', "%s" % self.photo.name, 'image/jpeg',
                                                        output.len, None)
        super(Student, self).save(*args, **kwargs)
开发者ID:xzxzxc,项目名称:WGS,代码行数:64,代码来源:models.py

示例6: Professor

# 需要导入模块: from django.core.files.uploadedfile import InMemoryUploadedFile [as 别名]
# 或者: from django.core.files.uploadedfile.InMemoryUploadedFile import read [as 别名]
class Professor(models.Model):
    user = models.OneToOneField(User, on_delete=models.CASCADE, limit_choices_to=Q(groups__name='Professors'),
                                related_name='professor')
    first_name_en = models.CharField(max_length=50)
    last_name_en = models.CharField(max_length=50)
    first_name_ua = models.CharField(max_length=50)
    last_name_ua = models.CharField(max_length=50)
    academic_title = models.CharField(max_length=100, choices=(('doctor', _('Doktor of Physics and Mathematics')),
                                                               ('kandidat', _('Kandidat of Physics and Mathematics')),))
    institution = models.CharField(max_length=100, choices=(('TSNUK', _('Taras Shevchenko National University of Kyiv')),))
    position = models.CharField(max_length=100, choices=(('assistant', _('Teaching assistant')),
                                                         ('academic', _('Academic')), ('docent', _('Docent')),
                                                         ('professor', _('Professor')), ('headOfDepartment', _('Head of Department')), ))
    interests_en = models.TextField()
    interests_ua = models.TextField()
    join_date = models.DateField('date joined', auto_now=True)
    photo = models.ImageField(upload_to='professors', default='default.jpg')
    photo_small = models.ImageField(upload_to='professors_small', default='default_small.jpg', editable=False)

    def __unicode__(self):  # __unicode__ on Python 2
        return '%s  %s' % (self.first_name_en, self.last_name_en)

    def name_en(self):
        return '%s  %s' % (self.first_name_en, self.last_name_en)

    def name_ua(self):
        return '%s  %s' % (self.first_name_ua, self.last_name_ua)

    def save(self, *args, **kwargs):
        # Change image size and create 50x50 thumbnail
        if self.photo:
            if Professor.objects.filter(pk=self.pk).exists():
                if self.photo != Professor.objects.get(pk=self.pk).photo:
                    image = Img.open(StringIO.StringIO(self.photo.read()))
                    image.thumbnail((200, 200), Img.ANTIALIAS)
                    output = StringIO.StringIO()
                    image.save(output, format='JPEG', quality=75)
                    output.seek(0)
                    self.photo = InMemoryUploadedFile(output, 'ImageField', "%s" % self.photo.name, 'image/jpeg',
                                                      output.len, None)
                    image = Img.open(StringIO.StringIO(self.photo.read()))
                    image.thumbnail((50, 50), Img.ANTIALIAS)
                    output = StringIO.StringIO()
                    image.save(output, format='JPEG', quality=75)
                    output.seek(0)
                    self.photo_small = InMemoryUploadedFile(output, 'ImageField', "%s" % self.photo.name, 'image/jpeg',
                                                            output.len, None)
            else:
                image = Img.open(StringIO.StringIO(self.photo.read()))
                image.thumbnail((200, 200), Img.ANTIALIAS)
                output = StringIO.StringIO()
                image.save(output, format='JPEG', quality=75)
                output.seek(0)
                self.photo = InMemoryUploadedFile(output, 'ImageField', "%s" % self.photo.name, 'image/jpeg',
                                                  output.len, None)
                image = Img.open(StringIO.StringIO(self.photo.read()))
                image.thumbnail((50, 50), Img.ANTIALIAS)
                output = StringIO.StringIO()
                image.save(output, format='JPEG', quality=75)
                output.seek(0)
                self.photo_small = InMemoryUploadedFile(output, 'ImageField', "%s" % self.photo.name, 'image/jpeg',
                                                        output.len, None)
        super(Professor, self).save(*args, **kwargs)
开发者ID:xzxzxc,项目名称:WGS,代码行数:65,代码来源:models.py


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