Management command decorator

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
from django.core.management.base import BaseCommand

def command(help=None, args=''):
    def _decorate(func):
        _args = args
        _help = help
        class Command(BaseCommand):
            help = _help if _help is not None else func.__doc__
            args = _args
            def handle(self, *args, **kwargs):
                return func(*args, **kwargs)
        func.func_globals['Command'] = Command
        return func
    return _decorate

More like this

  1. Quick-and-dirty decorator for static navigation context by pablo_PXL 2 years, 3 months ago
  2. django_template decorator by fredd4 5 years, 3 months ago
  3. Cache Any Function by jeffwheeler 6 years, 2 months ago
  4. Python-like string interpolation in Javascript by phxx 2 years, 11 months ago
  5. update primary key (and cascade to child tables) by guettli 1 year, 3 months ago

Comments

gmandx (on August 31, 2010):

Well, some how (and I think it can be done) the decorator can introspect the function's positional arguments and keyword arguments, and then use those for the command's arguments and options, respectively. Then the only parameter needed for the decorator usage would be the help text.

#

eternicode (on August 31, 2010):

gmandx, interesting idea. I've done some light reading around, and it looks like the functools module has some stuff that could do this.

#

(Forgotten your password?)