Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@cshoe
Created November 17, 2014 22:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cshoe/6836658ee792d250450f to your computer and use it in GitHub Desktop.
Save cshoe/6836658ee792d250450f to your computer and use it in GitHub Desktop.
# As mentioned, this is a bit verbose but I like the separation of concerns.
# Basically, you have two functions -- one that does meaningful work and
# another that just serves as the "glue" to connect the Django signal
# infrastructure to your function.
from django.dispatch import receiver
@receiver
def glue_funtion(sender, **kwargs):
"""
This function doesn't do anything but pass args to the function
that's actually going to do the work.
There's not much of a reason to test this function since any tests
aimed at it would really just be testing to see if the the Djagno
signal stuff worked and that the function was registered.
"""
# pick out the kwargs the worker function needs. This could also
# be done in the worker function. **kwargs could just be passed
# straight through. Do whatever feels good.
cool_arg = kwargs['some_key']
user = kwargs['user']
# sender could or could not be passed. All depends on if the worker
# function needs it
worker_function(user, cool_arg)
def worker_function(user, cool_arg):
"""
Do some meaninful work in here -- update a user record, send an email, etc.
Write tests aimed at this function and it's intended side effects.
"""
u.cool_arg = True
u.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment