GaretJax / django-click

Write Django management command using the click CLI library
MIT License
251 stars 20 forks source link

Feature Request: Make click management commands discoverable and runnable via django admin #40

Closed nefrob closed 10 months ago

nefrob commented 10 months ago

It would be cool if these commands could be available via Django admin with auto generated form fields to provide inputs based on the click command definition. This likely falls out of the scope of this project but would come in handy for discoverability and ease of use. Output from the command could be downloaded after run or dumped in the browser window potentially.

VidyaYadavv1519 commented 10 months ago

can you explain more what exactly you want

nefrob commented 10 months ago

Add a new page to Django admin where django-click management commands can be run. Consider having all commands be available via a dropdown. The click arguments can be provided via text field (or if more fancy generate the correct form field type based on the click argument type, ex. checkbox for boolean). Display the docstring when a management command is selected. Click a run button and the command is run, logging output in the Django admin view.

This is likely better written as a Django admin extension that picks up management commands, and conveniently also automatically finds those defined by django-click rather than something that needs to be built out here.

FlipperPA commented 10 months ago

Django Admin pages usually have to be associated with a model. You can get around this by creating a Proxy Model, but it isn't officially supported. I've done this at my job for this exact purpose, but fully aware that it is an unsupported API. So it is doable - perhaps by adding a decorator to the command class - but I don't think it would be wise to do it project-wide with an unsupported API. Thoughts?

FlipperPA commented 10 months ago

Here's the hack:

from .models import RerunDataDictionary

@admin.register(RerunDataDictionary)
class RerunDataDictionaryAdmin(admin.ModelAdmin):
    """
    Admin for a fake proxy model which allows us to inject into the admin interface.
    """

    def get_urls(self):
        return [path(
            "",
            self.admin_site.admin_view(self.dd_view),
            name='data_dictionary_rerundatadictionary_changelist',
        )]

    def dd_view(self, request):
        output = ""

        if request.method == "POST":
            [... do stuff ...]

        context = {
            "blah": "a",
            "asdf": "b",
        }

        return TemplateResponse(
            request,
            "data_dictionary/rerun_data_dictionary.html",
            context,
        )
nefrob commented 10 months ago

Thanks for the context. Seems like we should close this issue and consider reopening it in the Django repo with the goal of officially making this api official.