@@ -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
119129def 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
0 commit comments