django-object-actions icon indicating copy to clipboard operation
django-object-actions copied to clipboard

Add the ability to add a confirm dialog

Open danlamanna opened this issue 4 years ago • 8 comments

For some actions it would be nice to have a way of requiring the user answer a confirm dialog. Would a PR for something like this be accepted?

danlamanna avatar Feb 04 '21 19:02 danlamanna

are you thinking a JavaScript confirmation or a new screen?

crccheck avatar Feb 04 '21 20:02 crccheck

Just javascript confirmation I think.

On Thu, Feb 4, 2021, 3:13 PM Chris Chang [email protected] wrote:

are you thinking a JavaScript confirmation or a new screen?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/crccheck/django-object-actions/issues/116#issuecomment-773575902, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAEUPZLXCMVMTNGQ7HBIRM3S5L5X5ANCNFSM4XDMSCCQ .

danlamanna avatar Feb 04 '21 20:02 danlamanna

sounds good to me. I'm also thinking of changing the button to be a form POST instead of a link. I added this to the next major version milestone.

crccheck avatar Feb 05 '21 04:02 crccheck

what about an intermediate page with a form? how can I add something like that?

selcuko avatar Apr 02 '21 14:04 selcuko

what about an intermediate page with a form? how can I add something like that?

Sounds pretty tricky to do. The same technique documented for Admin Actions would work: https://docs.djangoproject.com/en/3.1/ref/contrib/admin/actions/#actions-that-provide-intermediate-pages

Most of the time, the best practice will be to return an HttpResponseRedirect and redirect the user to a view you’ve written, passing the list of selected objects in the GET query string.

crccheck avatar Apr 02 '21 17:04 crccheck

From https://github.com/kaoslabsinc/django-building-blocks/blob/master/building_blocks/admin/mixins.py (shameless plug)

class AreYouSureActionsAdminMixin(DjangoObjectActions):
    are_you_sure_actions = ()
    are_you_sure_prompt_f = "Are you sure you want to {label} this object?"

    def __init__(self, *args, **kwargs):
        super(AreYouSureActionsAdminMixin, self).__init__(*args, **kwargs)
        for action in self.are_you_sure_actions:
            tool = getattr(self, action)
            label = getattr(tool, 'label', action).lower()
            are_you_sure_prompt = self.are_you_sure_prompt_f.format(tool=tool, label=label)
            tool.__dict__.setdefault('attrs', {})
            tool.__dict__['attrs'].setdefault('onclick', f"""return confirm("{are_you_sure_prompt}");""")

keyvanm avatar Jan 27 '22 01:01 keyvanm

that's an awesome example I wouldn't have thought of. I've pinned this issue to highlight it

crccheck avatar Jan 27 '22 03:01 crccheck