Przeglądaj źródła

General cleanup of tables

Jeremy Stretch 5 lat temu
rodzic
commit
12e2537222

+ 6 - 56
netbox/dcim/tables.py

@@ -24,19 +24,6 @@ MPTT_LINK = """
 </span>
 """
 
-SITE_REGION_LINK = """
-{% if record.region %}
-    <a href="{% url 'dcim:site_list' %}?region={{ record.region.slug }}">{{ record.region }}</a>
-{% else %}
-    &mdash;
-{% endif %}
-"""
-
-COLOR_LABEL = """
-{% load helpers %}
-<label class="label" style="color: {{ record.color|fgcolor }}; background-color: #{{ record.color }}">{{ record }}</label>
-"""
-
 DEVICE_LINK = """
 <a href="{% url 'dcim:device' pk=record.pk %}">
     {{ record.name|default:'<span class="label label-info">Unnamed device</span>' }}
@@ -49,39 +36,6 @@ RACKGROUP_ELEVATIONS = """
 </a>
 """
 
-RACKRESERVATION_ACTIONS = """
-<a href="{% url 'dcim:rackreservation_changelog' pk=record.pk %}" class="btn btn-default btn-xs" title="Change log">
-    <i class="fa fa-history"></i>
-</a>
-{% if perms.dcim.change_rackreservation %}
-    <a href="{% url 'dcim:rackreservation_edit' pk=record.pk %}?return_url={{ request.path }}" class="btn btn-xs btn-warning"><i class="glyphicon glyphicon-pencil" aria-hidden="true"></i></a>
-{% endif %}
-"""
-
-MANUFACTURER_ACTIONS = """
-<a href="{% url 'dcim:manufacturer_changelog' slug=record.slug %}" class="btn btn-default btn-xs" title="Change log">
-    <i class="fa fa-history"></i>
-</a>
-{% if perms.dcim.change_manufacturer %}
-    <a href="{% url 'dcim:manufacturer_edit' slug=record.slug %}?return_url={{ request.path }}" class="btn btn-xs btn-warning"><i class="glyphicon glyphicon-pencil" aria-hidden="true"></i></a>
-{% endif %}
-"""
-
-DEVICEROLE_ACTIONS = """
-<a href="{% url 'dcim:devicerole_changelog' slug=record.slug %}" class="btn btn-default btn-xs" title="Change log">
-    <i class="fa fa-history"></i>
-</a>
-{% if perms.dcim.change_devicerole %}
-    <a href="{% url 'dcim:devicerole_edit' slug=record.slug %}?return_url={{ request.path }}" class="btn btn-xs btn-warning"><i class="glyphicon glyphicon-pencil" aria-hidden="true"></i></a>
-{% endif %}
-"""
-
-DEVICE_PRIMARY_IP = """
-{{ record.primary_ip6.address.ip|default:"" }}
-{% if record.primary_ip6 and record.primary_ip4 %}<br />{% endif %}
-{{ record.primary_ip4.address.ip|default:"" }}
-"""
-
 UTILIZATION_GRAPH = """
 {% load helpers %}
 {% utilization_graph value %}
@@ -149,8 +103,8 @@ class SiteTable(BaseTable):
         order_by=('_name',)
     )
     status = ChoiceFieldColumn()
-    region = tables.TemplateColumn(
-        template_code=SITE_REGION_LINK
+    region = tables.Column(
+        linkify=True
     )
     tenant = tables.TemplateColumn(
         template_code=COL_TENANT
@@ -206,7 +160,7 @@ class RackRoleTable(BaseTable):
     pk = ToggleColumn()
     name = tables.Column(linkify=True)
     rack_count = tables.Column(verbose_name='Racks')
-    color = tables.TemplateColumn(COLOR_LABEL)
+    color = ColorColumn()
     actions = ButtonsColumn(RackRole)
 
     class Meta(BaseTable.Meta):
@@ -506,10 +460,7 @@ class DeviceRoleTable(BaseTable):
         url_params={'role': 'slug'},
         verbose_name='VMs'
     )
-    color = tables.TemplateColumn(
-        template_code=COLOR_LABEL,
-        verbose_name='Label'
-    )
+    color = ColorColumn()
     vm_role = BooleanColumn()
     actions = ButtonsColumn(DeviceRole, pk_field='slug')
 
@@ -577,9 +528,8 @@ class DeviceTable(BaseTable):
         verbose_name='Type',
         text=lambda record: record.device_type.display_name
     )
-    primary_ip = tables.TemplateColumn(
-        template_code=DEVICE_PRIMARY_IP,
-        orderable=False,
+    primary_ip = tables.Column(
+        linkify=True,
         verbose_name='IP Address'
     )
     primary_ip4 = tables.Column(

+ 6 - 0
netbox/extras/choices.py

@@ -78,6 +78,12 @@ class ObjectChangeActionChoices(ChoiceSet):
         (ACTION_DELETE, 'Deleted'),
     )
 
+    CSS_CLASSES = {
+        ACTION_CREATE: 'success',
+        ACTION_UPDATE: 'primary',
+        ACTION_DELETE: 'danger',
+    }
+
 
 #
 # Log Levels for Reports and Scripts

+ 3 - 0
netbox/extras/models/change_logging.py

@@ -152,3 +152,6 @@ class ObjectChange(models.Model):
             self.object_repr,
             self.object_data,
         )
+
+    def get_action_class(self):
+        return ObjectChangeActionChoices.CSS_CLASSES.get(self.action)

+ 6 - 20
netbox/extras/tables.py

@@ -1,6 +1,7 @@
 import django_tables2 as tables
+from django.conf import settings
 
-from utilities.tables import BaseTable, BooleanColumn, ButtonsColumn, ColorColumn, ToggleColumn
+from utilities.tables import BaseTable, BooleanColumn, ButtonsColumn, ChoiceFieldColumn, ColorColumn, ToggleColumn
 from .models import ConfigContext, ObjectChange, Tag, TaggedItem
 
 TAGGED_ITEM = """
@@ -20,20 +21,6 @@ CONFIGCONTEXT_ACTIONS = """
 {% endif %}
 """
 
-OBJECTCHANGE_TIME = """
-<a href="{{ record.get_absolute_url }}">{{ value|date:"SHORT_DATETIME_FORMAT" }}</a>
-"""
-
-OBJECTCHANGE_ACTION = """
-{% if record.action == 'create' %}
-    <span class="label label-success">Created</span>
-{% elif record.action == 'update' %}
-    <span class="label label-primary">Updated</span>
-{% elif record.action == 'delete' %}
-    <span class="label label-danger">Deleted</span>
-{% endif %}
-"""
-
 OBJECTCHANGE_OBJECT = """
 {% if record.action != 3 and record.changed_object.get_absolute_url %}
     <a href="{{ record.changed_object.get_absolute_url }}">{{ record.object_repr }}</a>
@@ -91,12 +78,11 @@ class ConfigContextTable(BaseTable):
 
 
 class ObjectChangeTable(BaseTable):
-    time = tables.TemplateColumn(
-        template_code=OBJECTCHANGE_TIME
-    )
-    action = tables.TemplateColumn(
-        template_code=OBJECTCHANGE_ACTION
+    time = tables.DateTimeColumn(
+        linkify=True,
+        format=settings.SHORT_DATETIME_FORMAT
     )
+    action = ChoiceFieldColumn()
     changed_object_type = tables.Column(
         verbose_name='Type'
     )

+ 2 - 19
netbox/ipam/tables.py

@@ -78,14 +78,6 @@ VRF_LINK = """
 {% endif %}
 """
 
-STATUS_LABEL = """
-{% if record.pk %}
-    <span class="label label-{{ record.get_status_class }}">{{ record.get_status_display }}</span>
-{% else %}
-    <span class="label label-success">Available</span>
-{% endif %}
-"""
-
 VLAN_LINK = """
 {% if record.pk %}
     <a href="{{ record.get_absolute_url }}">{{ record.vid }}</a>
@@ -130,12 +122,6 @@ VLAN_MEMBER_TAGGED = """
 {% endif %}
 """
 
-VLAN_MEMBER_ACTIONS = """
-{% if perms.dcim.change_interface %}
-    <a href="{% if record.device %}{% url 'dcim:interface_edit' pk=record.pk %}{% else %}{% url 'virtualization:vminterface_edit' pk=record.pk %}{% endif %}" class="btn btn-xs btn-warning"><i class="glyphicon glyphicon-pencil"></i></a>
-{% endif %}
-"""
-
 TENANT_LINK = """
 {% if record.tenant %}
     <a href="{% url 'tenancy:tenant' slug=record.tenant.slug %}" title="{{ record.tenant.description }}">{{ record.tenant }}</a>
@@ -587,15 +573,11 @@ class VLANMembersTable(BaseTable):
         template_code=VLAN_MEMBER_TAGGED,
         orderable=False
     )
-    actions = tables.TemplateColumn(
-        template_code=VLAN_MEMBER_ACTIONS,
-        attrs={'td': {'class': 'text-right noprint'}},
-        verbose_name=''
-    )
 
 
 class VLANDevicesTable(VLANMembersTable):
     device = tables.LinkColumn()
+    actions = ButtonsColumn(Interface, buttons=['edit'])
 
     class Meta(BaseTable.Meta):
         model = Interface
@@ -604,6 +586,7 @@ class VLANDevicesTable(VLANMembersTable):
 
 class VLANVirtualMachinesTable(VLANMembersTable):
     virtual_machine = tables.LinkColumn()
+    actions = ButtonsColumn(VMInterface, buttons=['edit'])
 
     class Meta(BaseTable.Meta):
         model = VMInterface

+ 1 - 1
netbox/utilities/tables.py

@@ -197,7 +197,7 @@ class ColorColumn(tables.Column):
     """
     def render(self, value):
         return mark_safe(
-            '<span class="label color-block" style="background-color: #{}">&nbsp;</span>'.format(value)
+            f'<span class="label color-block" style="background-color: #{value}">&nbsp;</span>'
         )
 
 

+ 3 - 10
netbox/virtualization/tables.py

@@ -7,12 +7,6 @@ from utilities.tables import (
 )
 from .models import Cluster, ClusterGroup, ClusterType, VirtualMachine, VMInterface
 
-VIRTUALMACHINE_PRIMARY_IP = """
-{{ record.primary_ip6.address.ip|default:"" }}
-{% if record.primary_ip6 and record.primary_ip4 %}<br />{% endif %}
-{{ record.primary_ip4.address.ip|default:"" }}
-"""
-
 
 #
 # Cluster types
@@ -113,10 +107,9 @@ class VirtualMachineDetailTable(VirtualMachineTable):
         linkify=True,
         verbose_name='IPv6 Address'
     )
-    primary_ip = tables.TemplateColumn(
-        orderable=False,
-        verbose_name='IP Address',
-        template_code=VIRTUALMACHINE_PRIMARY_IP
+    primary_ip = tables.Column(
+        linkify=True,
+        verbose_name='IP Address'
     )
     tags = TagColumn(
         url_name='virtualization:virtualmachine_list'