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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from django import newforms as forms
from django.db import models
from django.dispatch import dispatcher
from django.db.models import signals
from imaging import resize_image,img_to_string,string_to_img,\
get_resized_name
from django.conf import settings
class SmartImageField(models.ImageField):
def __init__(self, verbose_name=None, name=None, *args, **kwargs):
self.width = kwargs.pop('width',None)
self.height = kwargs.pop('height',None)
#self.thumb_width = kwargs.pop('thumb_width',None)
#self.thumb_height = kwargs.pop('thumb_height',None)
self.upload_to = kwargs['upload_to']
super(SmartImageField,self).__init__(verbose_name, name, *args, **kwargs)
def get_save_FIELD_file(self):
def _save_FIELD_file(instance, filename, raw_contents, save=True):
# resize origin image to width, height dimesions
if self.width and self.height:
try:
raw_contents = img_to_string(resize_image(
string_to_img(raw_contents),
width=self.width,height=self.height))
except Exception, err:
pass
# thumb_width, thumb_height are given create thumbnail
#if self.thumb_width and self.thumb_height:
#thumb = resize_image(string_to_img(raw_contents),
#width=self.thumb_width, height=self.thumb_height)
#fname = get_resized_name('%s%s/%s' % (
#settings.MEDIA_ROOT, self.upload_to, filename),
#self.thumb_width, self.thumb_height,
#format=thumb.format)
#thumb.save(fname)
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'