saxix / django-admin-extra-buttons Goto Github PK
View Code? Open in Web Editor NEWLicense: Other
License: Other
Hello,
I have a case where I'd like a button to download a file by returning a FileResponse. However, the current code checks if something else than a HttpResponse is given and Redirects in that case :
django-admin-extra-buttons/src/admin_extra_buttons/handlers.py
Lines 52 to 54 in 778c94c
I made a quick and dirty patch locally to test replacing line 52 with if not isinstance(ret, (FileResponse, HttpResponse)):
and this did the trick for me.
I wondered if there was something else I wasn't aware of and if there was a reason not to give the opportunity to send back files from your excellent Django app. Otherwise, would you mind if I open a PR with this modification ?
Thanks for the excellent work,
Cheers
Sébastien
Hello! Thanks for the useful library. I've tried it out and I've found a few issues. Let's start with the first one.
class MyModelModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):
@button(enabled=False)
def send_newsletter_now(self, request):
self.message_user(request, 'The newsletter emails were sent successfully.')
The button is greyed out, but is still clickable. It should be disabled.
Hello and thank you for this necessary django library :)
I wish I could rename a button, here, the "Refresh" button, for example "Mettre à jour", etc.
Thank you :)
Setting change_list=False removes button from admin change form even if change_form=True property is set.
The confirm action is useful when the user is presented with one option and some information about the action.
For some cases it could be useful to present the user with multiple options. A further extension to this could be to add some description on each option.
Some mockup:
**Do you want to mark order 123 from customer [email protected] as paid?**
<Button: Mark as paid without sending receipt>
Will only mark as paid.
<Button: Mark as paid and send receipt>
Will send an email after marking the order as paid
API
@button(
label="Manage payment",
)
def mark_as_paid(self, request, pk):
order: shop.models.Order = self.get_object(request, pk)
@multi_action(label="Mark as paid without sending receipt", description="Will only mark as paid.")
def _action_charge_order(_request):
order.charge()
self.message_user(_request, message="Charged.")
@multi_action(label="Mark as paid and send receipt", description="Will send an email after marking the order as paid.")
def _action_charge_order_and_email(_request):
order.charge_and_email()
self.message_user(_request, message="Charged and emailed.")
return confirm_action(
self,
request,
actions=[_action_charge_order, _action_charge_order_and_email],
message=f"Do you want to mark order {order.pk} from customer {order.custome} as paid",
)
I've tried permission
property and it seems it hides the button no matter what value is provided from the permission function.
@register(MyModel)
class MyModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):
@button()
def delete_all(self, request):
pass
a simple button
@register(MyModel)
class MyModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):
@button(permission=lambda request, obj: False)
def delete_all(self, request):
pass
the button is hidden.
@register(MyModel)
class MyModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):
@button(permission=lambda request, obj: True)
def delete_all(self, request):
pass
the button is still hidden (why?)
a success message:
self.message_user(request, 'It was successful.')
an error message:
from django.contrib import messages
and
self.message_user(request, 'It failed.', messages.ERROR)
disable_on_click
does not work for me at all.
class MyModelModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):
@button(disable_on_click=True)
def send_newsletter_now(self, request):
time.sleep(10)
self.message_user(request, 'The newsletter emails were sent successfully.')
The button is still clickable after first click (while loading the page).
I guess the title is self-explanatory. There is no example for this decorator in the documentation.
I've tried something like
@view(label="VIEW 1")
def select_view_button(self, request, obj=None):
# Implement any logic you need
return HttpResponse("Selected view button clicked!")
@view(label="VIEW 2")
def select_view_button2(self, request, obj=None):
# Implement any logic you need
return HttpResponse("Selected view 2 button clicked!")
@choice(
label="Select View",
choices=[select_view_button, select_view_button2],
change_form=True,
change_list=False,
)
def group_views(self, request, obj=None):
# Implement any logic you need
return HttpResponse("Selected view button clicked!")
But it keeps complaining that 'ChoiceButton' object has no attribute 'choices'.
reference to the closed issue #12
I've created a custom mixin with override styles for object-tools block (extra buttons are rendering there)
class CustomExtraButtonsMixin(ExtraButtonsMixin):
"""
Extra buttons mixin with buttons layout fix.
"""
class Media:
css = {
'all': ('css/admin/extra-buttons/buttons-layout.css',)
}
and buttons-layout.css
:
.object-tools {
font-size: 0.625rem;
font-weight: bold;
/* Disable default styles */
padding-left: 0;
float: none;
position: unset;
margin-top: 0;
/* Custom styles to fix layout */
display: flex;
flex-wrap: wrap;
padding-bottom: 16px;
gap: 16px;
justify-content: flex-start;
}
@media (max-width: 767px) {
.object-tools li + li {
margin-left: 0!important;
}
}
Result:
and responsive:
Originally posted by @senabo in #12 (comment)
Hey
It would be great to change colors and labels on buttons based on specific conditions.
Love this library, solved a problem I had in a few lines of code. Thanks for building it.
The docs say "add custom buttons to Django Admin pages and/or add views to any ModelAdmin" but all I can find in the examples is the mixin for adding things to a ModelAdmin. Is there a way to use the buttons somewhere not associated with a Model?
If not, feature request :)
Thanks again for sharing this useful code.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.