from django import newforms as forms from django db import models from

 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
#from django import newforms as forms
from django.db import models
from django.dispatch import dispatcher
from django.db.models import signals
from StringIO import StringIO
import Image
from thumbnail import resize_image,img_to_string,string_to_img
class SmartImageField(models.FileField):
def __init__(self, verbose_name=None, name=None, width=100, height=100, **kwargs):
self.width, self.height = width, height
super(SmartImageField,self).__init__(verbose_name,name,**kwargs)
def get_save_FIELD_file(self):
def _save_FIELD_file(instance, filename, raw_contents, save=True):
raw_contents = img_to_string(resize_image(
string_to_img(raw_contents),
width=self.width,height=self.height))
instance._save_FIELD_file(self, filename, raw_contents, save)
return _save_FIELD_file
def contribute_to_class(self, cls, name):
super(SmartImageField, self).contribute_to_class(cls, name)
# overwrite parent binding to 'save_%s_file'
setattr(cls, 'save_%s_file' % self.name, self.get_save_FIELD_file())
def get_internal_type(self):
return 'ImageField'