Skip to content

Commit 3e1345a

Browse files
Enhance button docs (#32)
1 parent 21c23a2 commit 3e1345a

File tree

1 file changed

+35
-39
lines changed

1 file changed

+35
-39
lines changed

docs/src/api/button.md

Lines changed: 35 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,58 +33,56 @@ class MyModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):
3333

3434
!!! Note
3535

36-
AEB try to understand if a button should appear in the `change_form` and/or in the `change_list` page.
37-
If the decorated method has only one argument (es. `def scan(self, request)`), the button will only be visible
38-
on the `change_list` page, if it contains more that one argumente (es. `def scan(self, request, pk)`)
36+
AEB can guess if the button should appear in the `change_form` and/or in the `change_list` page:
37+
if the decorated method has only one argument (es. `def scan(self, request)`), the button will only be visible
38+
on the `change_list` page ; while if it contains more that one argument (es. `def scan(self, request, pk)`)
3939
the button will be visible in the `change_form` page.
4040

4141
## Options
4242

43-
change_form: `None`
44-
: set to `True` do show the button on the `change_form` page
45-
If set to `None` (default), use method signature to display the button
43+
`change_form` (defaults to `None`):
44+
Set to `True` do show the button on the `change_form` page.
45+
If set to `None` (default), use method signature to display the button.
4646

47-
change_list: `None`
48-
: set to `True` do show the button on the `change_list` page
49-
If set to `None` (default), use method signature to display the button
47+
`change_list` (defaults to `None`):
48+
Set to `True` do show the button on the `change_list` page.
49+
If set to `None` (default), use method signature to display the button.
5050

51-
disable_on_click: `True`
52-
: automatically disable button on click() to prevent unintentional double processing
51+
`disable_on_click` (defaults to `True`):
52+
Automatically disable button on `click` to prevent unintentional double processing.
5353

54-
disable_on_edit: `True`
55-
: automatically disable button when any FORM in page is modified
54+
`disable_on_edit` (defaults to `True`):
55+
Automatically disable button when any FORM in page is modified.
5656

57-
enabled: `True`
58-
: bool or callable to set enable status
57+
`enabled` (defaults to `True`):
58+
bool or callable to set enable status. The callable takes the `ButtonWidget` instance as a unique argument ; this argument gives access to the `request`, the template `context`, and the `original` object the is being edited in the admin.
5959

60-
html_attrs: `{}`
61-
: Dictionary of html tags to use in button rendering.
60+
`html_attrs` (defaults to `{}`):
61+
Dictionary of html tags to use in button rendering.
6262

63-
label: `decorated method name`
64-
: button label
63+
`label` (defaults to `decorated method name`):
64+
button label.
6565

66-
visible: `True`
67-
: bool or callable show/hide button
66+
`pattern` (defaults to `<function_name>/<path:arg1>/<path:arg2>/....`):
67+
url pattern to use for the url generation.
6868

69+
`visible` (defaults to `True`):
70+
bool or callable show/hide button. The callable takes the `ButtonWidget` instance as a unique argument ; this argument gives access to the `request`, the template `context`, and the `original` object the is being edited in the admin.
71+
72+
`permission` (defaults to `None`):
73+
Django permission code needed to access the view and display the button, or a callable that takes the `request` and the edited `object` as arguments and that must return a `bool`.
6974

7075
!!! Note
7176

7277
`id` is automacally set if not provided,
7378
`class` is updated/set based on `disable_on_click` and `disable_on_edit` values
7479

75-
label: `decorated method name`
76-
: button label
77-
78-
pattern: `<function_name>/<path:arg1>/<path:arg2>/....`
79-
: url pattern to use for the url genaration.
80-
81-
permission: `None`
82-
: Django permission code needed to access the view and display the button
83-
8480
## Examples
8581

8682
### Simple
83+
8784
Simplest usage. Display a button and create a view on `admin/mymodel/scan`.
85+
8886
```python
8987

9088
@register(MyModel)
@@ -96,12 +94,13 @@ class MyModelAdmin(ExtrButtonsMixi, admin.ModelAdmin):
9694

9795
```
9896
### Check Permissions
97+
9998
Buttons with custom permission, one for `change_list` and other for `change_form`
10099

101100
```python
102101

103102
@register(MyModel)
104-
class MyModelAdmin(ExtrButtonsMixi, admin.ModelAdmin):
103+
class MyModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):
105104

106105
@button(permission=lambda request, obj: request.user.is_superuser)
107106
def delete_all(self, request):
@@ -116,30 +115,27 @@ class MyModelAdmin(ExtrButtonsMixi, admin.ModelAdmin):
116115
```
117116

118117
### Fully featured
119-
Buttons with custom permission, one for `change_list` and other for `change_form`
118+
119+
Two complex buttons, one for `change_list` with custom permission, and one for `change_form` with custom visibility.
120120

121121
```python
122122

123123
@register(MyModel)
124-
class MyModelAdmin(ExtrButtonsMixi, admin.ModelAdmin):
124+
class MyModelAdmin(ExtraButtonsMixin, admin.ModelAdmin):
125125

126126
@button(permission=lambda request, obj: request.user.is_superuser,
127127
html_attrs={'style': 'background-color:var(--button-bg)'},
128128
label=_('Delete All Records'),
129-
change_form=True
130129
)
131130
def delete_all(self, request):
132131
pass
133132

134-
@button(permission=lambda request, obj: request.user.is_superuser,
133+
@button(visible=lambda btn: "special_context_key" in btn.context,
135134
html_attrs={'style': 'background-color:var(--button-bg)'},
136135
enabled=lambda btn: btn.original.status == SUCCESS,
137-
label=_('Delete All Records'),
138-
change_form=True
136+
label=_('Do something special on this one'),
139137
)
140138
def toggle(self, request, pk):
141139
pass
142140

143-
144-
145141
```

0 commit comments

Comments
 (0)