Browse Source

Replace get_component_template_actions() with ButtonsColumn

Jeremy Stretch 5 years ago
parent
commit
f4c14d4854
2 changed files with 39 additions and 54 deletions
  1. 24 47
      netbox/dcim/tables.py
  2. 15 7
      netbox/utilities/tables.py

+ 24 - 47
netbox/dcim/tables.py

@@ -110,21 +110,6 @@ POWERPANEL_POWERFEED_COUNT = """
 """
 
 
-def get_component_template_actions(model_name):
-    return """
-        {{% if perms.dcim.change_{model_name} %}}
-            <a href="{{% url 'dcim:{model_name}_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 %}}
-        {{% if perms.dcim.delete_{model_name} %}}
-            <a href="{{% url 'dcim:{model_name}_delete' pk=record.pk %}}?return_url={{{{ request.path }}}}" class="btn btn-xs btn-danger">
-                <i class="glyphicon glyphicon-trash" aria-hidden="true"></i>
-            </a>
-        {{% endif %}}
-    """.format(model_name=model_name).strip()
-
-
 #
 # Regions
 #
@@ -401,10 +386,9 @@ class ComponentTemplateTable(BaseTable):
 
 
 class ConsolePortTemplateTable(ComponentTemplateTable):
-    actions = tables.TemplateColumn(
-        template_code=get_component_template_actions('consoleporttemplate'),
-        attrs={'td': {'class': 'text-right noprint'}},
-        verbose_name=''
+    actions = ButtonsColumn(
+        model=ConsolePortTemplate,
+        buttons=('edit', 'delete')
     )
 
     class Meta(BaseTable.Meta):
@@ -414,10 +398,9 @@ class ConsolePortTemplateTable(ComponentTemplateTable):
 
 
 class ConsoleServerPortTemplateTable(ComponentTemplateTable):
-    actions = tables.TemplateColumn(
-        template_code=get_component_template_actions('consoleserverporttemplate'),
-        attrs={'td': {'class': 'text-right noprint'}},
-        verbose_name=''
+    actions = ButtonsColumn(
+        model=ConsoleServerPortTemplate,
+        buttons=('edit', 'delete')
     )
 
     class Meta(BaseTable.Meta):
@@ -427,10 +410,9 @@ class ConsoleServerPortTemplateTable(ComponentTemplateTable):
 
 
 class PowerPortTemplateTable(ComponentTemplateTable):
-    actions = tables.TemplateColumn(
-        template_code=get_component_template_actions('powerporttemplate'),
-        attrs={'td': {'class': 'text-right noprint'}},
-        verbose_name=''
+    actions = ButtonsColumn(
+        model=PowerPortTemplate,
+        buttons=('edit', 'delete')
     )
 
     class Meta(BaseTable.Meta):
@@ -440,10 +422,9 @@ class PowerPortTemplateTable(ComponentTemplateTable):
 
 
 class PowerOutletTemplateTable(ComponentTemplateTable):
-    actions = tables.TemplateColumn(
-        template_code=get_component_template_actions('poweroutlettemplate'),
-        attrs={'td': {'class': 'text-right noprint'}},
-        verbose_name=''
+    actions = ButtonsColumn(
+        model=PowerOutletTemplate,
+        buttons=('edit', 'delete')
     )
 
     class Meta(BaseTable.Meta):
@@ -456,10 +437,9 @@ class InterfaceTemplateTable(ComponentTemplateTable):
     mgmt_only = BooleanColumn(
         verbose_name='Management Only'
     )
-    actions = tables.TemplateColumn(
-        template_code=get_component_template_actions('interfacetemplate'),
-        attrs={'td': {'class': 'text-right noprint'}},
-        verbose_name=''
+    actions = ButtonsColumn(
+        model=InterfaceTemplate,
+        buttons=('edit', 'delete')
     )
 
     class Meta(BaseTable.Meta):
@@ -472,10 +452,9 @@ class FrontPortTemplateTable(ComponentTemplateTable):
     rear_port_position = tables.Column(
         verbose_name='Position'
     )
-    actions = tables.TemplateColumn(
-        template_code=get_component_template_actions('frontporttemplate'),
-        attrs={'td': {'class': 'text-right noprint'}},
-        verbose_name=''
+    actions = ButtonsColumn(
+        model=FrontPortTemplate,
+        buttons=('edit', 'delete')
     )
 
     class Meta(BaseTable.Meta):
@@ -485,10 +464,9 @@ class FrontPortTemplateTable(ComponentTemplateTable):
 
 
 class RearPortTemplateTable(ComponentTemplateTable):
-    actions = tables.TemplateColumn(
-        template_code=get_component_template_actions('rearporttemplate'),
-        attrs={'td': {'class': 'text-right noprint'}},
-        verbose_name=''
+    actions = ButtonsColumn(
+        model=RearPortTemplate,
+        buttons=('edit', 'delete')
     )
 
     class Meta(BaseTable.Meta):
@@ -498,10 +476,9 @@ class RearPortTemplateTable(ComponentTemplateTable):
 
 
 class DeviceBayTemplateTable(ComponentTemplateTable):
-    actions = tables.TemplateColumn(
-        template_code=get_component_template_actions('devicebaytemplate'),
-        attrs={'td': {'class': 'text-right noprint'}},
-        verbose_name=''
+    actions = ButtonsColumn(
+        model=DeviceBayTemplate,
+        buttons=('edit', 'delete')
     )
 
     class Meta(BaseTable.Meta):

+ 15 - 7
netbox/utilities/tables.py

@@ -130,25 +130,28 @@ class ButtonsColumn(tables.TemplateColumn):
     :param model: Model class to use for calculating URL view names
     :param prepend_content: Additional template content to render in the column (optional)
     """
+    buttons = ('changelog', 'edit', 'delete')
     attrs = {'td': {'class': 'text-right text-nowrap noprint'}}
     # Note that braces are escaped to allow for string formatting prior to template rendering
     template_code = """
-    <a href="{{% url '{app_label}:{model_name}_changelog' {pk_field}=record.{pk_field} %}}" class="btn btn-default btn-xs" title="Change log">
-        <i class="fa fa-history"></i>
-    </a>
-    {{% if perms.{app_label}.change_{model_name} %}}
+    {{% if "changelog" in buttons %}}
+        <a href="{{% url '{app_label}:{model_name}_changelog' {pk_field}=record.{pk_field} %}}" class="btn btn-default btn-xs" title="Change log">
+            <i class="fa fa-history"></i>
+        </a>
+    {{% endif %}}
+    {{% if "edit" in buttons and perms.{app_label}.change_{model_name} %}}
         <a href="{{% url '{app_label}:{model_name}_edit' {pk_field}=record.{pk_field} %}}?return_url={{{{ request.path }}}}" class="btn btn-xs btn-warning" title="Edit">
             <i class="fa fa-pencil"></i>
         </a>
     {{% endif %}}
-    {{% if perms.{app_label}.delete_{model_name} %}}
+    {{% if "delete" in buttons and perms.{app_label}.delete_{model_name} %}}
         <a href="{{% url '{app_label}:{model_name}_delete' {pk_field}=record.{pk_field} %}}?return_url={{{{ request.path }}}}" class="btn btn-xs btn-danger" title="Delete">
             <i class="fa fa-trash"></i>
         </a>
     {{% endif %}}
     """
 
-    def __init__(self, model, *args, pk_field='pk', prepend_template=None, **kwargs):
+    def __init__(self, model, *args, pk_field='pk', buttons=None, prepend_template=None, **kwargs):
         if prepend_template:
             prepend_template = prepend_template.replace('{', '{{')
             prepend_template = prepend_template.replace('}', '}}')
@@ -157,11 +160,16 @@ class ButtonsColumn(tables.TemplateColumn):
         template_code = self.template_code.format(
             app_label=model._meta.app_label,
             model_name=model._meta.model_name,
-            pk_field=pk_field
+            pk_field=pk_field,
+            buttons=buttons
         )
 
         super().__init__(template_code=template_code, *args, **kwargs)
 
+        self.extra_context.update({
+            'buttons': buttons or self.buttons,
+        })
+
     def header(self):
         return ''