#29557: add on_commit decorator
-------------------------------------+-------------------------------------
               Reporter:  obayemi    |          Owner:  nobody
                   Type:  New        |         Status:  new
  feature                            |
              Component:             |        Version:  master
  Uncategorized                      |       Keywords:  transaction
               Severity:  Normal     |  on_commit decorator
           Triage Stage:             |      Has patch:  0
  Unreviewed                         |
    Needs documentation:  0          |    Needs tests:  0
Patch needs improvement:  0          |  Easy pickings:  0
                  UI/UX:  0          |
-------------------------------------+-------------------------------------
 I recently had to add on_commit hooks with lambdas to some functions
 (mainly email notifications in post_save signals handler)
 That was a lot of boilerplate so I wrote a simple decorator for making a
 function call only trigger on transaction commit

 {{{
 def call_on_commit(f):
     """
     only call the decorated function at transaction commit
     warning the return value will be ignored
     """
     def handle(*args, **kwargs):
         transaction.on_commit(lambda: f(*args, **kwargs))
     return handle
 }}}

 leading to

 {{{
 @call_on_commit
 def send_message(user, context):
     # send message
 }}}
 instead of
 {{{
 def send_message(user, context):
     def _send_message():
         # send message
     on_commit(do_stuff)
 }}}

 I made a PR on github to implement this feature
 https://github.com/django/django/pull/10167

-- 
Ticket URL: <https://code.djangoproject.com/ticket/29557>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-updates+unsubscr...@googlegroups.com.
To post to this group, send email to django-updates@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/050.2e5916aa653c4f85994d445150726066%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to