From d9e43668ac68e8f5c749949ff9306e38199784d1 Mon Sep 17 00:00:00 2001 From: Thomas Fargeix Date: Sun, 27 Oct 2024 16:50:51 +0100 Subject: [PATCH] Fix exporting of sources and destinations fields Avoid formating to HTML when exporting to CSV. May still have issues with commas and quotes in object alias names. --- netbox_data_flows/models/dataflows.py | 11 ----------- .../{utils/tables.py => tables/columns.py} | 14 ++++++++++++++ netbox_data_flows/tables/dataflows.py | 14 ++++++-------- netbox_data_flows/tables/objectaliases.py | 3 ++- 4 files changed, 22 insertions(+), 20 deletions(-) rename netbox_data_flows/{utils/tables.py => tables/columns.py} (51%) diff --git a/netbox_data_flows/models/dataflows.py b/netbox_data_flows/models/dataflows.py index dcc300d..60c02e4 100644 --- a/netbox_data_flows/models/dataflows.py +++ b/netbox_data_flows/models/dataflows.py @@ -11,7 +11,6 @@ from ipam.constants import SERVICE_PORT_MAX, SERVICE_PORT_MIN from netbox_data_flows.choices import DataFlowInheritedStatusChoices, DataFlowProtocolChoices, DataFlowStatusChoices -from netbox_data_flows.utils.helpers import object_list_to_string from .groups import DataFlowGroup from .objectaliases import ObjectAlias @@ -160,16 +159,6 @@ def destination_port_list(self): return array_to_string(self.destination_ports) - @property - def source_list(self): - sources = self.sources.all() - return object_list_to_string(sources, linkify=True) - - @property - def destination_list(self): - destinations = self.destinations.all() - return object_list_to_string(destinations, linkify=True) - class Meta: ordering = ( "application", diff --git a/netbox_data_flows/utils/tables.py b/netbox_data_flows/tables/columns.py similarity index 51% rename from netbox_data_flows/utils/tables.py rename to netbox_data_flows/tables/columns.py index 73f2e2c..9401858 100644 --- a/netbox_data_flows/utils/tables.py +++ b/netbox_data_flows/tables/columns.py @@ -1,5 +1,19 @@ +import django_tables2 as tables + from netbox.tables import columns +from netbox_data_flows.utils.helpers import object_list_to_string + + +class ObjectAliasListColumn(tables.Column): + """Display the Object Aliases with links but export them without.""" + + def render(self, value): + return object_list_to_string(value.all(), linkify=True) + + def value(self, value): + return object_list_to_string(value.all(), linkify=False, separator=",") + class RuntimeTemplateColumn(columns.TemplateColumn): """Allow setting the extra_context at runtime instead of model instantiation.""" diff --git a/netbox_data_flows/tables/dataflows.py b/netbox_data_flows/tables/dataflows.py index 42f8487..d329400 100644 --- a/netbox_data_flows/tables/dataflows.py +++ b/netbox_data_flows/tables/dataflows.py @@ -4,6 +4,8 @@ from netbox_data_flows.models import DataFlow +from .columns import ObjectAliasListColumn + __all__ = ( "DataFlowTable", @@ -36,12 +38,10 @@ class DataFlowTable(NetBoxTable): accessor=tables.A("destination_port_list"), order_by=tables.A("destination_ports"), ) - sources = tables.Column( - accessor=tables.A("source_list"), + sources = ObjectAliasListColumn( orderable=False, ) - destinations = tables.Column( - accessor=tables.A("destination_list"), + destinations = ObjectAliasListColumn( orderable=False, ) @@ -110,12 +110,10 @@ class DataFlowRuleTable(NetBoxTable): accessor=tables.A("destination_port_list"), order_by=tables.A("destination_ports"), ) - sources = tables.Column( - accessor=tables.A("source_list"), + sources = ObjectAliasListColumn( orderable=False, ) - destinations = tables.Column( - accessor=tables.A("destination_list"), + destinations = ObjectAliasListColumn( orderable=False, ) tags = columns.TagColumn(url_name="plugins:netbox_data_flows:dataflow_rules") diff --git a/netbox_data_flows/tables/objectaliases.py b/netbox_data_flows/tables/objectaliases.py index 7466c26..d5cbd21 100644 --- a/netbox_data_flows/tables/objectaliases.py +++ b/netbox_data_flows/tables/objectaliases.py @@ -3,7 +3,8 @@ from netbox.tables import NetBoxTable, columns from netbox_data_flows.models import ObjectAlias, ObjectAliasTarget -from netbox_data_flows.utils.tables import RuntimeTemplateColumn + +from .columns import RuntimeTemplateColumn __all__ = (