Skip to content

Commit 7f2a032

Browse files
committed
Top-Button redirects to List-View
Change the redirect target of the top "Add Attachment" button from the url you are currently at to the attachment-list-view of the object where the attachment will/was created. This makes it consistent with for example the "Add Component" buttons for devices.
1 parent b961889 commit 7f2a032

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

netbox_attachments/template_content.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,14 @@ def get_display_preference(app_model_name: str) -> str:
5757
return display_settings.get(app_model_name, default_display)
5858

5959

60-
def create_add_attachment_button(model_name: str) -> Type[PluginTemplateExtension]:
60+
def create_add_attachment_button(model_name: str, url_pattern_name: str) -> Type[PluginTemplateExtension]:
6161
"""
6262
Creates an 'add attachment' button extension for a model.
6363
6464
Args:
6565
model_name: String in format "<app_label>.<model>" (e.g., "dcim.device")
66+
url_pattern_name: Fully qualified URL pattern name for the attachment list view
67+
(e.g., "dcim:device_device-attachment_list")
6668
6769
Returns:
6870
Type[PluginTemplateExtension]: Button extension class
@@ -72,17 +74,23 @@ class AddAttachmentButton(PluginTemplateExtension):
7274
models = [model_name]
7375

7476
def buttons(self):
75-
return self.render("netbox_attachments/add_attachment_button.html")
77+
return self.render(
78+
"netbox_attachments/add_attachment_button.html",
79+
extra_context={'object_type_attachment_list': url_pattern_name}
80+
)
7681

7782
return AddAttachmentButton
7883

7984

80-
def register_attachment_tab_view(model: Type[Model]) -> None:
85+
def register_attachment_tab_view(model: Type[Model]) -> str:
8186
"""
8287
Creates and registers an attachment tab view for a model.
8388
8489
Args:
8590
model: Django model class to add the tab view to
91+
92+
Returns:
93+
str: name of the registered view
8694
"""
8795
model_name = model._meta.model_name
8896
view_name = f"{model_name}-attachment_list"
@@ -115,6 +123,8 @@ def get_children(self, request, parent):
115123

116124
register_model_view(model, name=view_name, path=view_path)(AttachmentTabView)
117125

126+
return view_name
127+
118128

119129
def get_template_extensions() -> List[Type[PluginTemplateExtension]]:
120130
"""
@@ -154,12 +164,14 @@ def get_template_extensions() -> List[Type[PluginTemplateExtension]]:
154164

155165
# Handle display as additional tab
156166
if display_preference == "additional_tab":
167+
# Register tab view
168+
view_name = register_attachment_tab_view(model)
169+
157170
# Add button if configured
158171
if should_add_button:
159-
extensions.append(create_add_attachment_button(app_model_name))
172+
url_pattern_name = f"{app_label}:{model_name}_{view_name}"
173+
extensions.append(create_add_attachment_button(app_model_name, url_pattern_name))
160174

161-
# Register tab view
162-
register_attachment_tab_view(model)
163175
continue
164176

165177
# Create panel extension in the specified location

netbox_attachments/templates/netbox_attachments/add_attachment_button.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{% if perms.netbox_attachments.add_netboxattachment %}
2-
<a href="{% url 'plugins:netbox_attachments:netboxattachment_add' %}?object_type={{ object|content_type_id }}&object_id={{ object.pk }}&return_url={{ object.get_absolute_url }}"
2+
<a href="{% url 'plugins:netbox_attachments:netboxattachment_add' %}?object_type={{ object|content_type_id }}&object_id={{ object.pk }}&return_url={% url object_type_attachment_list pk=object.pk %}"
33
class="btn btn-secondary">
44
<i class="mdi mdi-paperclip" aria-hidden="true"></i>
55
Add Attachment

0 commit comments

Comments
 (0)