Skip to content

Commit

Permalink
Decouple generator from model
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewwithanm committed Feb 12, 2012
1 parent 1054d05 commit 2a422f5
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions imagekit/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@ def __init__(self, processors=None, format=None, options={},
self.storage = storage
self.cache_state_backend = cache_state_backend or get_default_cache_state_backend()

def process_content(self, filename, content, model=None):
def process_content(self, content, filename=None, source_file=None):
img = open_image(content)
original_format = img.format

# Run the processors
processors = self.processors
if callable(processors):
processors = processors(instance=model, file=content)
processors = processors(source_file)
img = ProcessorPipeline(processors or []).process(img)

options = dict(self.options or {})
Expand Down Expand Up @@ -90,15 +90,15 @@ def generate_file(self, filename, source_file, save=True):
"""
if source_file: # TODO: Should we error here or something if the source_file doesn't exist?
# Process the original image file.

try:
fp = source_file.storage.open(source_file.name)
except IOError:
return
fp.seek(0)
fp = StringIO(fp.read())

img, content = self.process_content(filename, fp,
getattr(source_file, 'instance', None))
img, content = self.process_content(fp, filename, source_file)

if save:
storage = self.storage or source_file.storage
Expand Down Expand Up @@ -187,8 +187,16 @@ def __init__(self, processors=None, format=None, options={},
raise Exception('The pre_cache argument has been removed in favor'
' of cache state backends.')

self.generator = SpecFileGenerator(processors, format=format,
options=options, autoconvert=autoconvert, image_cache_backend=image_cache_backend)
# The generator accepts a callable value for processors, but it
# takes different arguments than the callable that ImageSpecField
# expects, so we create a partial application and pass that instead.
# TODO: Should we change the signatures to match? Even if `instance` is not part of the signature, it's accessible through the source file object's instance property.
p = lambda file: processors(instance=file.instance, file=file) if \
callable(processors) else processors

self.generator = SpecFileGenerator(p, format=format, options=options,
autoconvert=autoconvert,
cache_state_backend=cache_state_backend)
self.image_field = image_field
self.storage = storage
self.cache_to = cache_to
Expand Down Expand Up @@ -427,7 +435,8 @@ def _post_delete_handler(sender, instance=None, **kwargs):
class ProcessedImageFieldFile(ImageFieldFile):
def save(self, name, content, save=True):
new_filename = self.field.generate_filename(self.instance, name)
img, content = self.field.generator.process_content(new_filename, content)
img, content = self.field.generator.process_content(content,
new_filename, self)
return super(ProcessedImageFieldFile, self).save(name, content, save)


Expand Down

0 comments on commit 2a422f5

Please sign in to comment.