On Wed, Nov 14, 2007 at 08:55:09AM -0500, Marty Alchin wrote:
> On Nov 14, 2007 12:21 AM, Malcolm Tredinnick <[EMAIL PROTECTED]> wrote:
> > Jeremy's still got a valid problem, though, but I'm not sure
> > reintroducing pre_save() and post_save() is a necessary step yet.
> > Hopefully there's something else we can do.
> 
> I wonder if there's a way to modify PyDispatcher (since we're
> distributing our own copy anyway) so that it allows dispatching of a
> particular signal to be suspended temporarily. That way, dispatched
> signals might go into a queue, perhaps and get sent in order when the
> signal is reactivated. Then, Jeremy's code might look something like
> this:
> 
> from django.dispatch import dispatcher
> from django.db.models import signals
> 
> class MyModel(Model):
>     def save(self):
>         queue = dispatcher.queue(signals.post_save)
>         super(MyModel, self).save()
> 
>         #extra work here
> 
>         queue.dispatch()
> 
> def even_more(sender,instance):
>     #stuff that depends on extra work having already occurred
> 
> dispatcher.connect(signals.post_save, even_more_stuff)

This is neat and all, but I don't think at actually solves the problem at hand.
If save is not overridden, when does queue.dispatch() get called?

The only way I can think of overcoming this is to dynamically modify self.save
when it is accessed (by overriding __getattr__).  The signals are fired from
code that is dynamically appended to the save method.  This guarrantees that the
signal is fired at the very end of the save call:

--------------------------------------------------------------------------------
class Model(object):
    def save(self, ...):
        ...

    def __getattr__(self, name):
        value = super(Model, self).__getattr__(name)
        if name == 'save':
            def save(self, *args, **kwargs):
                retval = value(self, *args, **kwargs)
                send_postsave_signal()
                return retval
            return save
        retun value
--------------------------------------------------------------------------------

This adds some overhead, of course, but it does do (roughly) what is being
asked.

-Forest
-- 
Forest Bond
http://www.alittletooquiet.net

Attachment: signature.asc
Description: Digital signature

Reply via email to