netbox-community / netbox

The premier source of truth powering network automation. Open source under Apache 2. Try NetBox Cloud free: https://netboxlabs.com/free-netbox-cloud/
http://netboxlabs.com/oss/netbox/
Apache License 2.0
16.16k stars 2.59k forks source link

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

Closed abhi1693 closed 1 year ago

abhi1693 commented 1 year ago

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

jeremystretch commented 1 year ago

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.

abhi1693 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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 commented 1 year ago

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