开发者

Hacking django-stdimage2 to support multiple images

I am working on a Django project in which users should be allowed to upload multiple images at once. That portion of the project will likely be handled by SWFUpload, unless you have a better suggestion.

The image renaming, resizing, and thumbnail creation will be handled by django-stdimage2.

The Problem

django-stdimage2 renames each image using the field name and ob开发者_运维问答ject primary key. If five images exist for gear row with primary key 1, all five images will be renamed "image_1.jpeg".

Before I introduce a possible solution, here are my models. Basically, one gear row can have many gear_image rows.

class gear(models.Model):
       id = models.AutoField(primary_key=True)
       model = models.CharField(max_length=100)

class gear_images(models.Model):
       id = models.AutoField(primary_key=True)
       gear_id = models.ForeignKey(gear)
       image = StdImageField(upload_to='images/gear', blank=True, size=(640, 480, True), thumbnail_size=(100, 100, True))

A Solution

I was thinking of adding a timestamp, in milliseconds and rounded, to the filename. I'm neither a Python or Django pro but I poked around in django-stdimage2's fields.py file and I think I located the code I need to edit to make this work. The two lines of code that are commented out are my proposed solutions:

def _rename_resize_image(self, instance=None, **kwargs):
    '''
    Renames the image, and calls methods to resize and create the thumbnail
    '''
    if not kwargs.get('raw', None):
        if getattr(instance, self.name):
            filename = getattr(instance, self.name).path
            ext = os.path.splitext(filename)[1].lower().replace('jpg', 'jpeg')
            # time_stamp = int(round(time.time() * 1000))
            # dst = self.generate_filename(instance, '%s_%s_%s%s' % (self.name, instance._get_pk_val(), time_stamp, ext))
            dst = self.generate_filename(instance, '%s_%s%s' % (self.name, instance._get_pk_val(), ext))
            dst_fullpath = os.path.join(settings.MEDIA_ROOT, dst)
            if os.path.normcase(os.path.abspath(filename)) != os.path.normcase(os.path.abspath(dst_fullpath)):
                os.rename(filename, dst_fullpath)
                if self.size:
                    self._resize_image(dst_fullpath, self.size)
                if self.thumbnail_size:
                    thumbnail_filename = self._get_thumbnail_filename(dst_fullpath)
                    shutil.copyfile(dst_fullpath, thumbnail_filename)
                    self._resize_image(thumbnail_filename, self.thumbnail_size)
                setattr(instance, self.attname, dst)
                instance.save()

Each image name would look something like: image_1_159753456.jpeg

Do you think this is a good work-around? I am open to other ideas also.

Thank you :)


I do not think you have a problem here at all.

django-stdimage{2} will rename to the object id of your gear_images model, not its parent gear model. So one gear can have many images, each will have the gear_images pk appended to the filename.

So really, you only have a problem if it's important to you to use the gear model's pk in the filename instead of the gear_images pk. If you're ok with the latter, then you don't need to do anything, it should just work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜