auto image field w/ prepopulate_from & default

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
from django.db import models
from django.db.models import signals
from django.core.files.base import ContentFile
from django.db.models.fields.files import ImageFieldFile

import cStringIO as StringIO
import Image
import os

class DefaultImageFieldFile(ImageFieldFile):
    def __init__(self, *args, **kwargs):
        super(DefaultImageFieldFile, self).__init__(*args, **kwargs)
        self.default_url = self.field.default_url
        
    def _get_url(self):
        try:
            return super(DefaultImageFieldFile, self)._get_url()
        except ValueError:
            return self.default_url
    url = property(_get_url)
    

class AutoImageField(models.ImageField):
    attr_class = DefaultImageFieldFile
    
    def __init__(self, *args, **kwargs):
        self.prepopulate_from = kwargs.pop('prepopulate_from', None)
        if self.prepopulate_from:
            self.size = kwargs.pop('size')
        self.default_url = kwargs.pop('default', None)
        if self.prepopulate_from or self.default_url:
            kwargs['blank'] = True
        super(AutoImageField, self).__init__(*args, **kwargs)
        
    def _downsize_image(self, instance, name, width, height):
        src = getattr(instance, self.prepopulate_from)
        img = Image.open(src.path)
        img = img.convert("RGB")
        img.thumbnail((width, height), Image.ANTIALIAS)
        io = StringIO.StringIO()
        img.save(io, 'JPEG')
        
        fname = os.path.split(src.path)[-1].split('.')
        fname.pop()
        fname[-1] = fname[-1] + name
        fname.append('jpg')
        fname = '.'.join(fname)
        return ContentFile(io.getvalue()), fname

    def contribute_to_class(self, cls, name):
        super(AutoImageField, self).contribute_to_class(cls, name)
        
        def process_image(instance, **kwargs):
            if not getattr(instance, name, False) and getattr(instance, self.prepopulate_from, False):
                width, height = self.size
                img, fname = self._downsize_image(instance, name, width, height)
                getattr(instance, name).save(fname, img, False)
        
        if self.prepopulate_from:
            signals.pre_save.connect(process_image, sender=cls, weak=False)

More like this

  1. Dynamic thumbnail generator by semente 6 years, 1 month ago
  2. ImageField with per user folder by pigletto 6 years, 3 months ago
  3. Imagefield with variations by fivethreeo 5 years, 8 months ago
  4. Auto Generate/Save Thumbnails using Template Filter (scale max_x, max_y, or both) by ThisbeTom 5 years, 4 months ago
  5. UPDATED: Django Image Thumbnail Filter by danfairs 6 years, 5 months ago

Comments

(Forgotten your password?)