Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

15815 convert dashboard widgets for users/groups #15839

Merged
merged 4 commits into from May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 12 additions & 2 deletions netbox/extras/dashboard/widgets.py
@@ -1,3 +1,4 @@
import logging
import uuid
from functools import cached_property
from hashlib import sha256
Expand Down Expand Up @@ -32,6 +33,8 @@
'WidgetConfigForm',
)

logger = logging.getLogger('netbox.data_backends')


def get_object_type_choices():
return [
Expand All @@ -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


Expand Down
29 changes: 29 additions & 0 deletions 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
),
]