Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a generic view_tab.html template to ease plugin development #12110

Closed
abhi1693 opened this issue Mar 31, 2023 · 5 comments · Fixed by #13388
Closed

Add a generic view_tab.html template to ease plugin development #12110

abhi1693 opened this issue Mar 31, 2023 · 5 comments · Fixed by #13388
Assignees
Labels
status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user

Comments

@abhi1693
Copy link
Member

abhi1693 commented Mar 31, 2023

NetBox version

v3.4.7

Feature type

Change to existing functionality

Proposed functionality

Add view_tab.html to templates/inc with the following minimalistic content

{% extends 'generic/object.html' %}
{% load helpers %}

{% block content %}
    {% include 'inc/table_controls_htmx.html' with table_modal=table_config %}
    <form method="post">
        {% csrf_token %}
        <div class="card">
            <div class="card-body" id="object_list">
                {% include 'htmx/table.html' %}
            </div>
        </div>
    </form>
{% endblock content %}

{% block modals %}
    {{ block.super }}
    {% table_config_form table %}
{% endblock modals %}

To allow table_config to be passed down to the template, make the following change in the python code

class ObjectChildrenView(ObjectView, ActionsMixin, TableMixin):
     table = None
     filterset = None
     template_name = 'inc/view_tabl.html'
 
+    def get_extra_context(self, request, instance):
+        return {
+            'table_config': f'{self.table.__name__}_config'
+        }
+
     def get_children(self, request, parent):
         """
         Return a QuerySet of child objects.

Use case

In a lot of my plugins, wherever I use the ViewTab functionality, I have to ship out my own template along with the code. In 99% cases, it's exactly the same file which I even reuse within the same plugin to avoid writing multiple ones for different classes. It would ease with the plugin development, if NetBox provided a generic template that the code will automatically look for if not specified by the plugin developer and use that.

Database changes

No response

External dependencies

No response

@abhi1693 abhi1693 added the type: feature Introduction of new functionality to the application label Mar 31, 2023
@jeremystretch
Copy link
Member

IMO the only way it makes sense for us to add a template is if it can be utilized for core views as well. For ObjectChildrenView specifically, templates will typically inherit from a model-specific parent template anyway so I'm not sure how useful this would be.

@jeremystretch jeremystretch added the status: under review Further discussion is needed to determine this issue's scope and/or implementation label May 4, 2023
@abhi1693
Copy link
Member Author

I think this can be tackled on how I did for ObjectContactView template. I can provide a PR for this on a similar work and I hope we can simplify most of the templates in use.

@DanSheps
Copy link
Member

DanSheps commented Jun 5, 2023

I don't see how useful a table would be for a "Tab View", IMO you would use ObjectChildrenView for that as it would likely be a related table or similar.

I think it would make sense if there was a generic view and it consisted of a single left pane which simply loops through all the models meta fields (perhaps have a base call to get_extra_content or similar which populates the attribute fields you want to display.

@abhi1693
Copy link
Member Author

abhi1693 commented Jun 6, 2023

80% of my plugins use tab view to display related data in a table and all of them ship with the exact code to do this including the template file.

So, the idea was to reduce the code by introducing a boilerplate template which can be used by the core and plugins.

Since, there are cases where a template a template would need to provide extra functionality, we can provide blocks to do so.

@DanSheps
Copy link
Member

DanSheps commented Jun 6, 2023

I think I missed the part where this was for ObjectChildrenView specifically, the mention of "View Tab" threw me off.

abhi1693 added a commit that referenced this issue Aug 4, 2023
@abhi1693 abhi1693 self-assigned this Aug 4, 2023
@abhi1693 abhi1693 added status: accepted This issue has been accepted for implementation and removed status: under review Further discussion is needed to determine this issue's scope and/or implementation labels Aug 4, 2023
jeremystretch added a commit that referenced this issue Aug 9, 2023
* adds generic tab view template #12110

* Rename view_tab.html and move to generic/

* Fix console ports template

* Move bulk operations view resolution to template

* Avoid setting default template_name on ObjectChildrenView

* Move base_template and table_config context vars to base context

* removed bulk_delete_control from templates

* refactored bulk_controls view

* fixed table_config

* renamed object_tab.html to objectchildren_list.html

* removed unused import

* Refactor template blocks for bulk operation buttons

* Rename object children generic template

* Move disconnect bulk action into a separate template for device components

* Fix cluster devices & VM interfaces views

* minor button label change

---------

Co-authored-by: Jeremy Stretch <[email protected]>
@jeremystretch jeremystretch added type: housekeeping Changes to the application which do not directly impact the end user and removed type: feature Introduction of new functionality to the application labels Aug 9, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 8, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: accepted This issue has been accepted for implementation type: housekeeping Changes to the application which do not directly impact the end user
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants