paduszyk / django-management-commands

👨🏻‍💻 Modular discovery, aliasing, and sequencing of Django management commands ⚡️
MIT License
4 stars 1 forks source link

Implement custom `call_command` #9

Open mikicz opened 1 week ago

mikicz commented 1 week ago

Description

Hello, what an interesting project! I'd find it quite useful for something I am working on, where I need to load management commands from outside apps.

One thing we heavily use at work is the call_command function, for running management commands from celery for example, etc. Right now with your solution that wouldn't work yet, as that function is not aware of the customisations (sadly the management command register cannot be easily overwritten)

Suggested Solution

Similar to how a custom implementation of execute_from_command_line must be imported, same should be possible for call_command, which would be aware of these custom commands

Terms

mikicz commented 1 week ago

I have tried this:

def call_command(command_name, *args, **options):
    if isinstance(command_name, BaseCommand):
        return base_call_command(command_name, *args, **options)

    if dotted_path := settings.PATHS.get(command_name):
        command_class = import_command_class(dotted_path)
    else:
        try:
            app_label, name = command_name.rsplit(".", 1)
        except ValueError:
            app_label, name = None, command_name

        command_class = load_command_class(name, app_label)

    command_name = command_class()

    return base_call_command(command_name, *args, **options)

And that worked for when commands are included PATHS or MODULES/SUBMODULES - won't work for ALIASES I think?

There are probably more edge cases though!

I would be happy to contribute if you point me towards anything I'm missing?