From 397a79ba56abfcbfb09e49652ea4a0bd89c70edf Mon Sep 17 00:00:00 2001 From: Matthew Tretter Date: Fri, 24 May 2013 23:21:30 -0400 Subject: [PATCH] Combine source_created and source_changed As discussed in #214, source_created and source_changed didn't really have clear definitions. In truth, their names and separation betray their origins as model receivers in earlier versions. The "source group" abstraction helped us get away from thinking about things exclusively in terms of models, but these remained as an artifact. --- docs/advanced_usage.rst | 5 ++--- docs/caching.rst | 3 +-- imagekit/cachefiles/strategies.py | 5 +---- imagekit/registry.py | 7 +++---- imagekit/signals.py | 3 +-- imagekit/specs/sourcegroups.py | 14 +++++--------- tests/models.py | 10 +++------- tests/test_abstract_models.py | 8 ++++---- 8 files changed, 20 insertions(+), 35 deletions(-) diff --git a/docs/advanced_usage.rst b/docs/advanced_usage.rst index 15dfb13..a2559ad 100644 --- a/docs/advanced_usage.rst +++ b/docs/advanced_usage.rst @@ -177,7 +177,6 @@ Running the "generateimages" management command would now cause thumbnails to be generated (using the "myapp:profile:avatar_thumbnail" spec) for each of the JPEGs in `/path/to/some/pics`. -Note that, since this source group doesnt send the `source_created` or -`source_changed` signals, the corresponding cache file strategy callbacks -would not be called for them. +Note that, since this source group doesnt send the `source_saved` signal, the +corresponding cache file strategy callbacks would not be called for them. diff --git a/docs/caching.rst b/docs/caching.rst index 00d9ae8..ff336b9 100644 --- a/docs/caching.rst +++ b/docs/caching.rst @@ -41,8 +41,7 @@ image is actually generated. It can implement the following four methods: * ``on_existence_required`` - called by ``ImageCacheFile`` when it requires the generated image to exist but may not be concerned with its contents. For example, when you access its ``url`` or ``path`` attribute. -* ``on_source_created`` - called when the source of a spec is created -* ``on_source_changed`` - called when the source of a spec is changed +* ``on_source_saved`` - called when the source of a spec is saved * ``on_source_deleted`` - called when the source of a spec is deleted The default strategy only defines the first two of these, as follows: diff --git a/imagekit/cachefiles/strategies.py b/imagekit/cachefiles/strategies.py index af678ba..fba6a0f 100644 --- a/imagekit/cachefiles/strategies.py +++ b/imagekit/cachefiles/strategies.py @@ -23,10 +23,7 @@ class Optimistic(object): """ - def on_source_created(self, file): - file.generate() - - def on_source_changed(self, file): + def on_source_saved(self, file): file.generate() diff --git a/imagekit/registry.py b/imagekit/registry.py index d781b06..f363399 100644 --- a/imagekit/registry.py +++ b/imagekit/registry.py @@ -1,6 +1,6 @@ from .exceptions import AlreadyRegistered, NotRegistered -from .signals import (content_required, existence_required, source_created, - source_changed, source_deleted) +from .signals import (content_required, existence_required, source_saved, + source_deleted) from .utils import call_strategy_method @@ -70,8 +70,7 @@ class SourceGroupRegistry(object): """ _signals = { - source_created: 'on_source_created', - source_changed: 'on_source_changed', + source_saved: 'on_source_saved', source_deleted: 'on_source_deleted', } diff --git a/imagekit/signals.py b/imagekit/signals.py index 12a0042..7dc57ae 100644 --- a/imagekit/signals.py +++ b/imagekit/signals.py @@ -6,6 +6,5 @@ content_required = Signal() existence_required = Signal() # Source group signals -source_created = Signal() -source_changed = Signal() +source_saved = Signal() source_deleted = Signal() diff --git a/imagekit/specs/sourcegroups.py b/imagekit/specs/sourcegroups.py index 69f2daf..96ec48e 100644 --- a/imagekit/specs/sourcegroups.py +++ b/imagekit/specs/sourcegroups.py @@ -2,9 +2,8 @@ Source groups are the means by which image spec sources are identified. They have two responsibilities: -1. To dispatch ``source_created``, ``source_changed``, and ``source_deleted`` - signals. (These will be relayed to the corresponding specs' cache file - strategies.) +1. To dispatch ``source_saved``, and ``source_deleted`` signals. (These will be + relayed to the corresponding specs' cache file strategies.) 2. To provide the source files that they represent, via a generator method named ``files()``. (This is used by the generateimages management command for "pre-caching" image files.) @@ -15,7 +14,7 @@ from django.db.models.signals import post_init, post_save, post_delete from django.utils.functional import wraps import inspect from ..cachefiles import LazyImageCacheFile -from ..signals import source_created, source_changed, source_deleted +from ..signals import source_saved, source_deleted from ..utils import get_nonabstract_descendants @@ -94,11 +93,8 @@ class ModelSignalRouter(object): old_hashes = instance._ik.get('source_hashes', {}).copy() new_hashes = self.update_source_hashes(instance) for attname, file in self.get_field_dict(instance).items(): - if created: - self.dispatch_signal(source_created, file, sender, instance, - attname) - elif old_hashes[attname] != new_hashes[attname]: - self.dispatch_signal(source_changed, file, sender, instance, + if created or old_hashes[attname] != new_hashes[attname]: + self.dispatch_signal(source_saved, file, sender, instance, attname) @ik_model_receiver diff --git a/tests/models.py b/tests/models.py index 758f2cb..5667cad 100644 --- a/tests/models.py +++ b/tests/models.py @@ -31,8 +31,7 @@ class CountingCacheFileStrategy(object): def __init__(self): self.on_existence_required_count = 0 self.on_content_required_count = 0 - self.on_source_changed_count = 0 - self.on_source_created_count = 0 + self.on_source_saved_count = 0 def on_existence_required(self, file): self.on_existence_required_count += 1 @@ -40,11 +39,8 @@ class CountingCacheFileStrategy(object): def on_content_required(self, file): self.on_content_required_count += 1 - def on_source_changed(self, file): - self.on_source_changed_count += 1 - - def on_source_created(self, file): - self.on_source_created_count += 1 + def on_source_saved(self, file): + self.on_source_saved_count += 1 class AbstractImageModel(models.Model): diff --git a/tests/test_abstract_models.py b/tests/test_abstract_models.py index a3ec774..4621d59 100644 --- a/tests/test_abstract_models.py +++ b/tests/test_abstract_models.py @@ -1,5 +1,5 @@ from django.core.files import File -from imagekit.signals import source_created +from imagekit.signals import source_saved from imagekit.specs.sourcegroups import ImageFieldSourceGroup from imagekit.utils import get_nonabstract_descendants from nose.tools import eq_ @@ -8,7 +8,7 @@ from . models import (AbstractImageModel, ConcreteImageModel, from .utils import get_image_file -def test_source_created_signal(): +def test_source_saved_signal(): source_group = ImageFieldSourceGroup(AbstractImageModel, 'original_image') count = [0] @@ -16,10 +16,10 @@ def test_source_created_signal(): if sender is source_group: count[0] += 1 - source_created.connect(receiver, dispatch_uid='test_source_created') + source_saved.connect(receiver, dispatch_uid='test_source_saved') instance = ConcreteImageModel() img = File(get_image_file()) - instance.original_image.save('test_source_created_signal.jpg', img) + instance.original_image.save('test_source_saved_signal.jpg', img) eq_(count[0], 1)