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
This commit is contained in:
Arthur Hanson 2024-05-01 06:56:46 -07:00 committed by GitHub
parent 0a7d1e29b4
commit 209f596397
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 41 additions and 2 deletions

View File

@ -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('.')
try:
content_type = ObjectType.objects.get_by_natural_key(app_label, model_name) content_type = ObjectType.objects.get_by_natural_key(app_label, model_name)
if content_type.model_class():
models.append(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

View File

@ -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
),
]