Procházet zdrojové kódy

Optimize display of ConfigContext assigned objects

jeremystretch před 4 roky
rodič
revize
a9930bd442
2 změnil soubory, kde provedl 28 přidání a 124 odebrání
  1. 16 0
      netbox/extras/views.py
  2. 12 124
      netbox/templates/extras/configcontext.html

+ 16 - 0
netbox/extras/views.py

@@ -276,6 +276,21 @@ class ConfigContextView(generic.ObjectView):
     queryset = ConfigContext.objects.all()
 
     def get_extra_context(self, request, instance):
+        # Gather assigned objects for parsing in the template
+        assigned_objects = (
+            ('Regions', instance.regions.all),
+            ('Site Groups', instance.site_groups.all),
+            ('Sites', instance.sites.all),
+            ('Device Types', instance.device_types.all),
+            ('Roles', instance.roles.all),
+            ('Platforms', instance.platforms.all),
+            ('Cluster Groups', instance.cluster_groups.all),
+            ('Clusters', instance.clusters.all),
+            ('Tenant Groups', instance.tenant_groups.all),
+            ('Tenants', instance.tenants.all),
+            ('Tags', instance.tags.all),
+        )
+
         # Determine user's preferred output format
         if request.GET.get('format') in ['json', 'yaml']:
             format = request.GET.get('format')
@@ -287,6 +302,7 @@ class ConfigContextView(generic.ObjectView):
             format = 'json'
 
         return {
+            'assigned_objects': assigned_objects,
             'format': format,
         }
 

+ 12 - 124
netbox/templates/extras/configcontext.html

@@ -50,132 +50,20 @@
                 </h5>
                 <div class="card-body">
                     <table class="table table-hover attr-table">
+                      {% for title, objects in assigned_objects %}
                         <tr>
-                            <th scope="row">Regions</th>
-                            <td>
-                                {% if object.regions.all %}
-                                    <ul>
-                                        {% for region in object.regions.all %}
-                                            <li><a href="{{ region.get_absolute_url }}">{{ region }}</a></li>
-                                        {% endfor %}
-                                    </ul>
-                                {% else %}
-                                    <span class="text-muted">None</span>
-                                {% endif %}
-                            </td>
-                        </tr>
-                        <tr>
-                            <th scope="row">Sites</th>
-                            <td>
-                                {% if object.sites.all %}
-                                    <ul>
-                                        {% for site in object.sites.all %}
-                                            <li><a href="{{ site.get_absolute_url }}">{{ site }}</a></li>
-                                        {% endfor %}
-                                    </ul>
-                                {% else %}
-                                    <span class="text-muted">None</span>
-                                {% endif %}
-                            </td>
-                        </tr>
-                        <tr>
-                            <th scope="row">Roles</th>
-                            <td>
-                                {% if object.roles.all %}
-                                    <ul>
-                                        {% for role in object.roles.all %}
-                                            <li><a href="{% url 'dcim:device_list' %}?role={{ role.slug }}">{{ role }}</a></li>
-                                        {% endfor %}
-                                    </ul>
-                                {% else %}
-                                    <span class="text-muted">None</span>
-                                {% endif %}
-                            </td>
-                        </tr>
-                        <tr>
-                            <th scope="row">Platforms</th>
-                            <td>
-                                {% if object.platforms.all %}
-                                    <ul>
-                                        {% for platform in object.platforms.all %}
-                                            <li><a href="{{ platform.get_absolute_url }}">{{ platform }}</a></li>
-                                        {% endfor %}
-                                    </ul>
-                                {% else %}
-                                    <span class="text-muted">None</span>
-                                {% endif %}
-                            </td>
-                        </tr>
-                        <tr>
-                            <th scope="row">Cluster Groups</th>
-                            <td>
-                                {% if object.cluster_groups.all %}
-                                    <ul>
-                                        {% for cluster_group in object.cluster_groups.all %}
-                                            <li><a href="{{ cluster_group.get_absolute_url }}">{{ cluster_group }}</a></li>
-                                        {% endfor %}
-                                    </ul>
-                                {% else %}
-                                    <span class="text-muted">None</span>
-                                {% endif %}
-                            </td>
-                        </tr>
-                        <tr>
-                            <th scope="row">Clusters</th>
-                            <td>
-                                {% if object.clusters.all %}
-                                    <ul>
-                                        {% for cluster in object.clusters.all %}
-                                            <li><a href="{{ cluster.get_absolute_url }}">{{ cluster }}</a></li>
-                                        {% endfor %}
-                                    </ul>
-                                {% else %}
-                                    <span class="text-muted">None</span>
-                                {% endif %}
-                            </td>
-                        </tr>
-                        <tr>
-                            <th scope="row">Tenant Groups</th>
-                            <td>
-                                {% if object.tenant_groups.all %}
-                                    <ul>
-                                        {% for tenant_group in object.tenant_groups.all %}
-                                            <li><a href="{{ tenant_group.get_absolute_url }}">{{ tenant_group }}</a></li>
-                                        {% endfor %}
-                                    </ul>
-                                {% else %}
-                                    <span class="text-muted">None</span>
-                                {% endif %}
-                            </td>
-                        </tr>
-                        <tr>
-                            <th scope="row">Tenants</th>
-                            <td>
-                                {% if object.tenants.all %}
-                                    <ul>
-                                        {% for tenant in object.tenants.all %}
-                                            <li><a href="{{ tenant.get_absolute_url }}">{{ tenant }}</a></li>
-                                        {% endfor %}
-                                    </ul>
-                                {% else %}
-                                    <span class="text-muted">None</span>
-                                {% endif %}
-                            </td>
-                        </tr>
-                        <tr>
-                            <th scope="row">Tags</th>
-                            <td>
-                                {% if object.tags.all %}
-                                    <ul>
-                                        {% for tag in object.tags.all %}
-                                            <li><a href="{{ tag.get_absolute_url }}">{{ tag }}</a></li>
-                                        {% endfor %}
-                                    </ul>
-                                {% else %}
-                                    <span class="text-muted">None</span>
-                                {% endif %}
-                            </td>
+                          <th scope="row">{{ title }}</th>
+                          <td>
+                            <ul class="list-unstyled mb-0">
+                              {% for object in objects %}
+                                <li><a href="{{ object.get_absolute_url }}">{{ object }}</a></li>
+                              {% empty %}
+                                <li class="text-muted">None</li>
+                              {% endfor %}
+                            </ul>
+                          </td>
                         </tr>
+                      {% endfor %}
                     </table>
                 </div>
             </div>