Command Signals

synopsis:Signals fired before and after a command is executed.

A signal is thrown pre/post each management command allowing your application to hook into each commands execution.

Basic Example

An example hooking into show_template_tags:

from import pre_command, post_command
from import Command

def pre_receiver(sender, args, kwargs):
  # I'm executed prior to the management command

def post_receiver(sender, args, kwargs, outcome):
  # I'm executed after the management command

pre_command.connect(pre_receiver, Command)
post_command.connect(post_receiver, Command)

Custom Permissions For All Models

You can use the post signal to hook into the update_permissions command so that you can add your own permissions to each model.

For instance, lets say you want to add list and view permissions to each model. You could do this by adding them to the permissions tuple inside your models Meta class but this gets pretty tedious.

An easier solution is to hook into the update_permissions call, as follows;

from django.db.models.signals import post_syncdb
from django.contrib.contenttypes.models import ContentType
from django.contrib.auth.models import Permission
from import post_command
from import Command as UpdatePermissionsCommand

def add_permissions(sender, **kwargs):
  Add view and list permissions to all content types.
  # for each of our content types
  for content_type in ContentType.objects.all():

    for action in ['view', 'list']:
      # build our permission slug
      codename = "%s_%s" % (action, content_type.model)

        Permission.objects.get(content_type=content_type, codename=codename)
        # Already exists, ignore
      except Permission.DoesNotExist:
        # Doesn't exist, add it
                      name="Can %s %s" % (action,
        print "Added %s permission for %s" % (action,
post_command.connect(add_permissions, UpdatePermissionsCommand)

Each time update_permissions is called add_permissions will be called which ensures there are view and list permissions to all content types.

Using pre/post signals on your own commands

The signals are implemented using a decorator on the handle method of a management command, thus using this functionality in your own application is trivial:

from import signalcommand

class Command(BaseCommand):

  def handle(self, *args, **kwargs):