Fixes #6817: Custom field columns should be removed from tables upon their deletion

This commit is contained in:
jeremystretch 2021-10-01 20:22:54 -04:00
parent 1be748b479
commit c9c537a1b9
2 changed files with 20 additions and 9 deletions

View File

@ -12,6 +12,7 @@
### Bug Fixes ### Bug Fixes
* [#6433](https://github.com/netbox-community/netbox/issues/6433) - Fix bulk editing of child prefixes under aggregate view * [#6433](https://github.com/netbox-community/netbox/issues/6433) - Fix bulk editing of child prefixes under aggregate view
* [#6817](https://github.com/netbox-community/netbox/issues/6817) - Custom field columns should be removed from tables upon their deletion
* [#6895](https://github.com/netbox-community/netbox/issues/6895) - Remove errant markup for null values in CSV export * [#6895](https://github.com/netbox-community/netbox/issues/6895) - Remove errant markup for null values in CSV export
* [#7373](https://github.com/netbox-community/netbox/issues/7373) - Fix flashing when server, client, and browser color-mode preferences are mismatched * [#7373](https://github.com/netbox-community/netbox/issues/7373) - Fix flashing when server, client, and browser color-mode preferences are mismatched
* [#7397](https://github.com/netbox-community/netbox/issues/7397) - Fix AttributeError exception when rendering export template for devices via REST API * [#7397](https://github.com/netbox-community/netbox/issues/7397) - Fix AttributeError exception when rendering export template for devices via REST API

View File

@ -29,13 +29,18 @@ class BaseTable(tables.Table):
'class': 'table table-hover object-list', 'class': 'table table-hover object-list',
} }
def __init__(self, *args, user=None, **kwargs): def __init__(self, *args, user=None, extra_columns=None, **kwargs):
# Add custom field columns # Add custom field columns
obj_type = ContentType.objects.get_for_model(self._meta.model) obj_type = ContentType.objects.get_for_model(self._meta.model)
for cf in CustomField.objects.filter(content_types=obj_type): cf_columns = [
self.base_columns[f'cf_{cf.name}'] = CustomFieldColumn(cf) (f'cf_{cf.name}', CustomFieldColumn(cf)) for cf in CustomField.objects.filter(content_types=obj_type)
]
if extra_columns is not None:
extra_columns.extend(cf_columns)
else:
extra_columns = cf_columns
super().__init__(*args, **kwargs) super().__init__(*args, extra_columns=extra_columns, **kwargs)
# Set default empty_text if none was provided # Set default empty_text if none was provided
if self.empty_text is None: if self.empty_text is None:
@ -50,17 +55,22 @@ class BaseTable(tables.Table):
# Apply custom column ordering for user # Apply custom column ordering for user
if user is not None and not isinstance(user, AnonymousUser): if user is not None and not isinstance(user, AnonymousUser):
columns = user.config.get(f"tables.{self.__class__.__name__}.columns") selected_columns = user.config.get(f"tables.{self.__class__.__name__}.columns")
if columns: if selected_columns:
pk = self.base_columns.pop('pk', None) pk = self.base_columns.pop('pk', None)
actions = self.base_columns.pop('actions', None) actions = self.base_columns.pop('actions', None)
for name, column in self.base_columns.items(): for name, column in self.columns.items():
if name in columns: if name in selected_columns:
self.columns.show(name) self.columns.show(name)
else: else:
self.columns.hide(name) self.columns.hide(name)
self.sequence = [c for c in columns if c in self.base_columns] # Rearrange the sequence to list selected columns first, followed by all remaining columns
# TODO: There's probably a more clever way to accomplish this
self.sequence = [
*[c for c in selected_columns if c in self.columns.names()],
*[c for c in self.columns.names() if c not in selected_columns]
]
# Always include PK and actions column, if defined on the table # Always include PK and actions column, if defined on the table
if pk: if pk: