django-imagekit/imagekit/models/fields/utils.py
Matthew Tretter 7bc82d3624 Remove arguments from generate() method
Previously, we had two places where we were passing kwargs that affected
the image generation: the ImageSpec constructor and the generate method.
These were essentially partial applications. With this commit, there's
only one partial application (when the spec is instantiated), and the
generate method is called without arguments. Therefore, specs can now
be treated as generic generators whose constructors just happen to
accept a source_file.
2012-12-01 21:20:33 -05:00

38 lines
1.6 KiB
Python

from ...files import GeneratedImageCacheFile
from django.db.models.fields.files import ImageField
class ImageSpecFileDescriptor(object):
def __init__(self, field, attname):
self.attname = attname
self.field = field
def __get__(self, instance, owner):
if instance is None:
return self.field
else:
field_name = getattr(self.field, 'source', None)
if field_name:
source_file = getattr(instance, field_name)
else:
image_fields = [getattr(instance, f.attname) for f in
instance.__class__._meta.fields if
isinstance(f, ImageField)]
if len(image_fields) == 0:
raise Exception('%s does not define any ImageFields, so your'
' %s ImageSpecField has no image to act on.' %
(instance.__class__.__name__, self.attname))
elif len(image_fields) > 1:
raise Exception('%s defines multiple ImageFields, but you'
' have not specified a source for your %s'
' ImageSpecField.' % (instance.__class__.__name__,
self.attname))
else:
source_file = image_fields[0]
spec = self.field.get_spec(source_file=source_file) # TODO: What "hints" should we pass here?
file = GeneratedImageCacheFile(spec)
instance.__dict__[self.attname] = file
return file
def __set__(self, instance, value):
instance.__dict__[self.attname] = value