Skip to content

Commit 90895ad

Browse files
📝 Add docstrings to feature/parameterize-namespace
Docstrings generation was requested by @KommuSoft. * #1 (comment) The following files were modified: * `django_adminlink/admin.py` * `django_adminlink/static/js/single_admin_action.js`
1 parent 9860fe6 commit 90895ad

2 files changed

Lines changed: 44 additions & 0 deletions

File tree

django_adminlink/admin.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ class LinkFieldAdminMixin:
1111
admin_url_namespace = "admin"
1212

1313
def _convert_list_display_item(self, field_name):
14+
"""
15+
Converts a list display field name to a callable that renders a link for ForeignKey fields.
16+
17+
If the specified field is a ForeignKey, returns a callable that displays the related object as a clickable link to its admin change page. Otherwise, returns the original field name.
18+
"""
1419
if isinstance(field_name, str):
1520
try:
1621
field = self.model._meta.get_field(field_name)
@@ -30,6 +35,11 @@ def get_list_display(self, request):
3035
return result
3136

3237
def _link_to_model_field(self, field):
38+
"""
39+
Returns a callable that renders a ForeignKey field as a clickable link to the related object's admin change page.
40+
41+
If the related model is not registered with the admin site, returns the field name instead.
42+
"""
3343
related_model = field.related_model
3444
admin_site = self.admin_site_to_link or admin.site
3545
model_admin = admin_site._registry.get(related_model)
@@ -41,6 +51,15 @@ def _link_to_model_field(self, field):
4151

4252
@admin.display(description=field.name, ordering=f"{field.name}")
4353
def column_render(obj):
54+
"""
55+
Renders a foreign key field as a clickable link to the related object's admin change page.
56+
57+
Args:
58+
obj: The model instance containing the foreign key field.
59+
60+
Returns:
61+
An HTML anchor element linking to the related object's admin page, or None if the field is not set.
62+
"""
4463
key = getattr(obj, field.name)
4564
if key is not None:
4665
return format_html(
@@ -65,6 +84,12 @@ class SingleItemActionMixin:
6584

6685
@admin.display(description="actions")
6786
def action_button_column(self, obj):
87+
"""
88+
Renders action buttons for each object in the admin list display.
89+
90+
Each button is configured with data attributes for the action name and object primary key,
91+
and triggers the `get_checkboxes` JavaScript function when clicked.
92+
"""
6893
if isinstance(self.action_buttons, dict):
6994
action_buttons = self.action_buttons.items()
7095
else:
@@ -76,6 +101,12 @@ def action_button_column(self, obj):
76101
)
77102

78103
def get_list_display(self, request):
104+
"""
105+
Extends the list display to include a column of action buttons if any are defined.
106+
107+
If the `action_buttons` attribute is set, appends the `action_button_column` to the list
108+
display; otherwise, returns the default list display.
109+
"""
79110
items = super().get_list_display(request)
80111
if self.action_buttons:
81112
return [*items, self.action_button_column]
@@ -85,6 +116,12 @@ def get_list_display(self, request):
85116

86117
@property
87118
def media(self):
119+
"""
120+
Extends the admin media to include JavaScript for single-item action buttons.
121+
122+
Returns:
123+
The combined media object with the additional JavaScript file included.
124+
"""
88125
return super().media + Media(js=["js/single_admin_action.js"])
89126

90127

django_adminlink/static/js/single_admin_action.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
/**
2+
* Selects a single item in a Django admin list and triggers the specified admin action for that item.
3+
*
4+
* Retrieves the primary key and action from the provided element's data attributes, updates the action selector, unchecks all other items, checks the targeted item, and submits the form to perform the action on that item only.
5+
*
6+
* @param {Element} e - The element containing `data-pk` and `data-action` attributes for the target item and action.
7+
*/
18
function get_checkboxes(e) {
29
const pk = e.getAttribute('data-pk');
310
const action = e.getAttribute('data-action');

0 commit comments

Comments
 (0)