netbox-attachments icon indicating copy to clipboard operation
netbox-attachments copied to clipboard

Top-Button redirects to List-View

Open m-hau opened this issue 2 months ago • 1 comments

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.

The implementation is probably not perfect, I was mostly playing around to see if/how it can be done. It uses the fact that registering a view also always creates a matching url-pattern. The name of that url-pattern is predictably derived from the view name, and can then be passed to Django's url template function (together with the primary key of the object).

It would have actually been easier if the view name wouldn't also include the model name again and just be a constant string (like attachments). This would also make the URLs look a bit nicer, instead of /dcim/devices/1234/device-attachment_list/ you get /dcim/devices/1234/attachments/.

Summary by CodeRabbit

  • Improvements

    • After adding an attachment, users are redirected to the attachment list for that object instead of back to the object's page.
  • Refactor

    • Internal flow for registering attachment tab views and generating the "add attachment" button was unified so the tab and button now use a single, consistent URL pattern, reducing mismatches and improving reliability.

m-hau avatar Oct 20 '25 15:10 m-hau

Walkthrough

Refactors attachment tab/button registration to pass and return URL pattern names between functions, and changes the add-attachment template's post-action redirect to the object's attachment list view resolved via the computed URL pattern.

Changes

Cohort / File(s) Change Summary
View registration & template extension
netbox_attachments/template_content.py
create_add_attachment_button signature changed to accept url_pattern_name: str and injects object_type_attachment_list into template context; register_attachment_tab_view now returns a view_name: str; get_template_extensions captures view_name and builds url_pattern_name before creating the button extension.
Add attachment template
netbox_attachments/templates/netbox_attachments/add_attachment_button.html
return_url for the add-attachment link changed from object.get_absolute_url to the object-type attachment list view URL resolved via the computed object_type_attachment_list pattern using the object's pk.

Sequence Diagram

sequenceDiagram
    participant GTE as get_template_extensions
    participant RTV as register_attachment_tab_view
    participant CAB as create_add_attachment_button
    participant TPL as Template

    GTE->>RTV: register_attachment_tab_view(model)
    RTV->>RTV: compute & register view_name
    RTV-->>GTE: return view_name
    Note over GTE: build url_pattern_name = "{app_label}:{model_name}_{view_name}"
    GTE->>CAB: create_add_attachment_button(model_name, url_pattern_name)
    CAB->>CAB: inject `object_type_attachment_list` = url_pattern_name into context
    CAB-->>TPL: extension renders button with `object_type_attachment_list`
    TPL->>TPL: add-attachment link resolves attachment-list URL with object's pk as return_url

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~25 minutes

Poem

🐰 I hopped through names and stitched a route,
A tab returned, a button gained its suit.
From item page to list it gently goes,
Little changes, bigger rows of bows. 🥕

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title Check ✅ Passed The title "Top-Button redirects to List-View" is directly related to the main change in the pull request. The core objective is to change the redirect target of the "Add Attachment" button from the current page URL to the attachment list view, and the title accurately captures this behavior change. The phrasing is concise and specific without being vague or overly broad, making it clear to someone scanning commit history that the attachment button now redirects to a list view rather than remaining on the current page.
Docstring Coverage ✅ Passed Docstring coverage is 80.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • [ ] 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • [ ] Create PR with unit tests
  • [ ] Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

coderabbitai[bot] avatar Oct 20 '25 15:10 coderabbitai[bot]

@m-hau Thanks for contributing! I’ll add some minor changes to the code and make a release.

Sorry for the delay.

Kani999 avatar Dec 11 '25 07:12 Kani999