-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathfields.py
61 lines (50 loc) · 2.25 KB
/
fields.py
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
61
import uuid
import os
from django.db.models import ImageField as DjangoImageField
from .backends import metadata, storage
from .files import ThumbnailedImageFile
from . import processors, post_processors
class ImageField(DjangoImageField):
attr_class = ThumbnailedImageFile
def __init__(self, *args, **kwargs):
self.resize_source_to = kwargs.pop('resize_source_to', None)
if kwargs.get('storage'):
raise ValueError('Please define storage backend in settings.py instead on the field itself')
kwargs['storage'] = storage.get_backend()
self.delete_on_update = kwargs.pop('delete_on_update', False)
self.metadata_backend = metadata.get_backend()
super(ImageField, self).__init__(*args, **kwargs)
def __unicode__(self):
return self.attname
def pre_save(self, model_instance, add):
"""
Process the source image through the defined processors.
"""
file = getattr(model_instance, self.attname)
# new file is assigned
if file and not file._committed:
# detect if there are old file needs to be cleaned
old_file = getattr(type(model_instance).objects.get(id=model_instance.id),
self.attname)
if old_file:
all_thumbs = [thumb for thumb in old_file.thumbnails.all()]
for thumb_size in all_thumbs:
old_file.thumbnails.delete(thumb_size)
old_file.delete()
image_file = file
if self.resize_source_to:
file.seek(0)
image_file = processors.process(file, self.resize_source_to)
image_file = post_processors.process(image_file, self.resize_source_to)
filename = str(uuid.uuid4()) + os.path.splitext(file.name)[1]
file.save(filename, image_file, save=False)
return file
def south_field_triple(self):
"""
Return a suitable description of this field for South.
Taken from smiley chris' easy_thumbnails
"""
from south.modelsinspector import introspector
field_class = 'django.db.models.fields.files.ImageField'
args, kwargs = introspector(self)
return (field_class, args, kwargs)