django-object-actions
django-object-actions copied to clipboard
Add the ability to add a confirm dialog
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?
are you thinking a JavaScript confirmation or a new screen?
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 .
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.
what about an intermediate page with a form? how can I add something like that?
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.
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}");""")
that's an awesome example I wouldn't have thought of. I've pinned this issue to highlight it