Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions netbox_attachments/template_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ def get_display_preference(app_model_name: str) -> str:
return display_settings.get(app_model_name, default_display)


def create_add_attachment_button(model_name: str) -> Type[PluginTemplateExtension]:
def create_add_attachment_button(model_name: str, url_pattern_name: str) -> Type[PluginTemplateExtension]:
"""
Creates an 'add attachment' button extension for a model.

Args:
model_name: String in format "<app_label>.<model>" (e.g., "dcim.device")
url_pattern_name: Fully qualified URL pattern name for the attachment list view
(e.g., "dcim:device_device-attachment_list")

Returns:
Type[PluginTemplateExtension]: Button extension class
Expand All @@ -72,17 +74,23 @@ class AddAttachmentButton(PluginTemplateExtension):
models = [model_name]

def buttons(self):
return self.render("netbox_attachments/add_attachment_button.html")
return self.render(
"netbox_attachments/add_attachment_button.html",
extra_context={'object_type_attachment_list': url_pattern_name}
)

return AddAttachmentButton


def register_attachment_tab_view(model: Type[Model]) -> None:
def register_attachment_tab_view(model: Type[Model]) -> str:
"""
Creates and registers an attachment tab view for a model.

Args:
model: Django model class to add the tab view to

Returns:
str: name of the registered view
"""
model_name = model._meta.model_name
view_name = f"{model_name}-attachment_list"
Expand Down Expand Up @@ -115,6 +123,8 @@ def get_children(self, request, parent):

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

return view_name


def get_template_extensions() -> List[Type[PluginTemplateExtension]]:
"""
Expand Down Expand Up @@ -154,12 +164,14 @@ def get_template_extensions() -> List[Type[PluginTemplateExtension]]:

# Handle display as additional tab
if display_preference == "additional_tab":
# Register tab view
view_name = register_attachment_tab_view(model)

# Add button if configured
if should_add_button:
extensions.append(create_add_attachment_button(app_model_name))
url_pattern_name = f"{app_label}:{model_name}_{view_name}"
extensions.append(create_add_attachment_button(app_model_name, url_pattern_name))

# Register tab view
register_attachment_tab_view(model)
continue

# Create panel extension in the specified location
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if perms.netbox_attachments.add_netboxattachment %}
<a href="{% url 'plugins:netbox_attachments:netboxattachment_add' %}?object_type={{ object|content_type_id }}&object_id={{ object.pk }}&return_url={{ object.get_absolute_url }}"
<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 %}"
class="btn btn-secondary">
<i class="mdi mdi-paperclip" aria-hidden="true"></i>
Add Attachment
Expand Down