from StringIO import StringIO from django db import models from django

 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
from StringIO import StringIO
from django.db import models
from django.core.files.uploadedfile import SimpleUploadedFile
from util import resize_image
class ExtendedImageField(models.ImageField):
"""
Extended ImageField which can resize image before saving it.
"""
def __init__(self, *args, **kwargs):
self.width = kwargs.pop('width', None)
self.height = kwargs.pop('height', None)
super(ExtendedImageField, self).__init__(*args, **kwargs)
def save_form_data(self, instance, data):
if self.width and self.height:
content = resize_image(rawdata=data.read(), width=self.width,
height=self.height)
data = SimpleUploadedFile(data.name, content,
data.content_type)
super(ExtendedImageField, self).save_form_data(instance, data)