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.
This commit is contained in:
Matthew Tretter 2013-05-24 23:21:30 -04:00
parent 0d5bfe3751
commit 397a79ba56
8 changed files with 20 additions and 35 deletions

View file

@ -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.

View file

@ -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:

View file

@ -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()

View file

@ -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',
}

View file

@ -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()

View file

@ -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

View file

@ -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):

View file

@ -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)