From 209f59639768c0a8293d4ee5ef0cc7318fbe8be0 Mon Sep 17 00:00:00 2001 From: Arthur Hanson Date: Wed, 1 May 2024 06:56:46 -0700 Subject: [PATCH] 15815 convert dashboard widgets for users/groups (#15839) * 15815 convert dashboard widgets for users/groups * 15815 review fixes * 15815 catch DoesNotExist for widget content type * 15815 add logging --- netbox/extras/dashboard/widgets.py | 14 +++++++-- .../0115_convert_dashboard_widgets.py | 29 +++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100644 netbox/extras/migrations/0115_convert_dashboard_widgets.py diff --git a/netbox/extras/dashboard/widgets.py b/netbox/extras/dashboard/widgets.py index 23f082ce2..a3d7f05a3 100644 --- a/netbox/extras/dashboard/widgets.py +++ b/netbox/extras/dashboard/widgets.py @@ -1,3 +1,4 @@ +import logging import uuid from functools import cached_property from hashlib import sha256 @@ -32,6 +33,8 @@ __all__ = ( 'WidgetConfigForm', ) +logger = logging.getLogger('netbox.data_backends') + def get_object_type_choices(): return [ @@ -54,8 +57,15 @@ def get_models_from_content_types(content_types): models = [] for content_type_id in content_types: app_label, model_name = content_type_id.split('.') - content_type = ObjectType.objects.get_by_natural_key(app_label, model_name) - models.append(content_type.model_class()) + try: + content_type = ObjectType.objects.get_by_natural_key(app_label, model_name) + if content_type.model_class(): + models.append(content_type.model_class()) + else: + logger.debug(f"Dashboard Widget model_class not found: {app_label}:{model_name}") + except ObjectType.DoesNotExist: + logger.debug(f"Dashboard Widget ObjectType not found: {app_label}:{model_name}") + return models diff --git a/netbox/extras/migrations/0115_convert_dashboard_widgets.py b/netbox/extras/migrations/0115_convert_dashboard_widgets.py new file mode 100644 index 000000000..c85c83ecf --- /dev/null +++ b/netbox/extras/migrations/0115_convert_dashboard_widgets.py @@ -0,0 +1,29 @@ +# Generated by Django 5.0.4 on 2024-04-24 20:09 + +from django.db import migrations + + +def update_dashboard_widgets(apps, schema_editor): + Dashboard = apps.get_model('extras', 'Dashboard') + + for dashboard in Dashboard.objects.all(): + for key, widget in dashboard.config.items(): + if models := widget['config'].get('models'): + models = list(map(lambda x: x.replace('users.netboxgroup', 'users.group'), models)) + models = list(map(lambda x: x.replace('users.netboxuser', 'users.user'), models)) + dashboard.config[key]['config']['models'] = models + dashboard.save() + + +class Migration(migrations.Migration): + + dependencies = [ + ('extras', '0114_customfield_add_comments'), + ] + + operations = [ + migrations.RunPython( + code=update_dashboard_widgets, + reverse_code=migrations.RunPython.noop + ), + ]