Ticket #25736: e.patch

File e.patch, 2.0 KB (added by Olivier Le Thanh Duong, 9 years ago)
  • django/core/management/base.py

    diff --git a/django/core/management/base.py b/django/core/management/base.py
    index f77add2..4c729cc 100644
    a b class BaseCommand(object):  
    201201    leave_locale_alone = False
    202202    requires_system_checks = True
    203203
     204    to_implement = 'handle' # Method to implement by subclass
     205
    204206    def __init__(self, stdout=None, stderr=None, no_color=False):
    205207        self.stdout = OutputWrapper(stdout or sys.stdout)
    206208        self.stderr = OutputWrapper(stderr or sys.stderr)
    class BaseCommand(object):  
    421423            else:
    422424                self.stdout.write(msg)
    423425
     426    @classmethod
     427    def from_func(klass, func, **kwargs):
     428        """Create a command from a function
     429        Example usage:
     430            from django.core.management.base import LabelCommand
     431
     432            def a(label):
     433                "Help message"
     434                print label
     435
     436            Command = LabelCommand.from_func(a, can_import_settings=False)
     437        """
     438        func_name = klass.to_implement
     439        d = {
     440            func_name: lambda self, *args, **kwargs: func(*args, **kwargs),
     441            'help': func.__doc__,
     442        }
     443        d.update(**kwargs)
     444        return type(str('Command'), (klass,), d)
     445
    424446    def handle(self, *args, **options):
    425447        """
    426448        The actual logic of the command. Subclasses must implement
    class AppCommand(BaseCommand):  
    439461    """
    440462    missing_args_message = "Enter at least one application label."
    441463
     464    to_implement = 'handle_app_config' # Method to implement by subclass
     465
    442466    def add_arguments(self, parser):
    443467        parser.add_argument('args', metavar='app_label', nargs='+',
    444468            help='One or more application label.')
    class LabelCommand(BaseCommand):  
    481505    label = 'label'
    482506    missing_args_message = "Enter at least one %s." % label
    483507
     508    to_implement = 'handle_label' # Method to implement by subclass
     509
    484510    def add_arguments(self, parser):
    485511        parser.add_argument('args', metavar=self.label, nargs='+')
    486512
Back to Top