From f4c14d48546ff1f4dc3309b027ec82a7699e62b7 Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Thu, 2 Jul 2020 16:47:28 -0400 Subject: [PATCH] Replace get_component_template_actions() with ButtonsColumn --- netbox/dcim/tables.py | 71 +++++++++++++------------------------- netbox/utilities/tables.py | 22 ++++++++---- 2 files changed, 39 insertions(+), 54 deletions(-) diff --git a/netbox/dcim/tables.py b/netbox/dcim/tables.py index 587749277..442c0cf47 100644 --- a/netbox/dcim/tables.py +++ b/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} %}} - - - - {{% endif %}} - {{% if perms.dcim.delete_{model_name} %}} - - - - {{% 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): diff --git a/netbox/utilities/tables.py b/netbox/utilities/tables.py index ec3d5dff5..a4f15a836 100644 --- a/netbox/utilities/tables.py +++ b/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 = """ - - - - {{% if perms.{app_label}.change_{model_name} %}} + {{% if "changelog" in buttons %}} + + + + {{% endif %}} + {{% if "edit" in buttons and perms.{app_label}.change_{model_name} %}} {{% endif %}} - {{% if perms.{app_label}.delete_{model_name} %}} + {{% if "delete" in buttons and perms.{app_label}.delete_{model_name} %}} {{% 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 ''