Przeglądaj źródła

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
Arthur Hanson 1 rok temu
rodzic
commit
209f596397

+ 12 - 2
netbox/extras/dashboard/widgets.py

@@ -1,3 +1,4 @@
+import logging
 import uuid
 import uuid
 from functools import cached_property
 from functools import cached_property
 from hashlib import sha256
 from hashlib import sha256
@@ -32,6 +33,8 @@ __all__ = (
     'WidgetConfigForm',
     'WidgetConfigForm',
 )
 )
 
 
+logger = logging.getLogger('netbox.data_backends')
+
 
 
 def get_object_type_choices():
 def get_object_type_choices():
     return [
     return [
@@ -54,8 +57,15 @@ def get_models_from_content_types(content_types):
     models = []
     models = []
     for content_type_id in content_types:
     for content_type_id in content_types:
         app_label, model_name = content_type_id.split('.')
         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
     return models
 
 
 
 

+ 29 - 0
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
+        ),
+    ]