mirror of
https://github.com/netbox-community/netbox.git
synced 2024-05-10 07:54:54 +00:00
* Replace masonry with gridstack * Initial work on dashboard widgets * Implement function to save dashboard layout * Define a default dashboard * Clean up widgets * Implement widget configuration views & forms * Permit merging dict value with existing dict in user config * Add widget deletion view * Enable HTMX for widget configuration * Implement view to add dashboard widgets * ObjectCountsWidget: Identify models by app_label & name * Add color customization to dashboard widgets * Introduce Dashboard model to store user dashboard layout & config * Clean up utility functions * Remove hard-coded API URL * Use fixed grid cell height * Add modal close button * Clean up dashboard views * Rebuild JS
This commit is contained in:
76
netbox/extras/dashboard/utils.py
Normal file
76
netbox/extras/dashboard/utils.py
Normal file
@ -0,0 +1,76 @@
|
||||
import uuid
|
||||
|
||||
from django.core.exceptions import ObjectDoesNotExist
|
||||
|
||||
from netbox.registry import registry
|
||||
from extras.constants import DEFAULT_DASHBOARD
|
||||
|
||||
__all__ = (
|
||||
'get_dashboard',
|
||||
'get_default_dashboard',
|
||||
'get_widget_class',
|
||||
'register_widget',
|
||||
)
|
||||
|
||||
|
||||
def register_widget(cls):
|
||||
"""
|
||||
Decorator for registering a DashboardWidget class.
|
||||
"""
|
||||
app_label = cls.__module__.split('.', maxsplit=1)[0]
|
||||
label = f'{app_label}.{cls.__name__}'
|
||||
registry['widgets'][label] = cls
|
||||
|
||||
return cls
|
||||
|
||||
|
||||
def get_widget_class(name):
|
||||
"""
|
||||
Return a registered DashboardWidget class identified by its name.
|
||||
"""
|
||||
try:
|
||||
return registry['widgets'][name]
|
||||
except KeyError:
|
||||
raise ValueError(f"Unregistered widget class: {name}")
|
||||
|
||||
|
||||
def get_dashboard(user):
|
||||
"""
|
||||
Return the Dashboard for a given User if one exists, or generate a default dashboard.
|
||||
"""
|
||||
if user.is_anonymous:
|
||||
dashboard = get_default_dashboard()
|
||||
else:
|
||||
try:
|
||||
dashboard = user.dashboard
|
||||
except ObjectDoesNotExist:
|
||||
# Create a dashboard for this user
|
||||
dashboard = get_default_dashboard()
|
||||
dashboard.user = user
|
||||
dashboard.save()
|
||||
|
||||
return dashboard
|
||||
|
||||
|
||||
def get_default_dashboard():
|
||||
from extras.models import Dashboard
|
||||
dashboard = Dashboard(
|
||||
layout=[],
|
||||
config={}
|
||||
)
|
||||
for widget in DEFAULT_DASHBOARD:
|
||||
id = str(uuid.uuid4())
|
||||
dashboard.layout.append({
|
||||
'id': id,
|
||||
'w': widget['width'],
|
||||
'h': widget['height'],
|
||||
'x': widget.get('x'),
|
||||
'y': widget.get('y'),
|
||||
})
|
||||
dashboard.config[id] = {
|
||||
'class': widget['widget'],
|
||||
'title': widget.get('title'),
|
||||
'config': widget.get('config', {}),
|
||||
}
|
||||
|
||||
return dashboard
|
Reference in New Issue
Block a user