1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

#9416: cleanup & widget improvements

This commit is contained in:
jeremystretch
2023-03-22 15:44:01 -04:00
parent a098c3b0c1
commit e176c7d906
10 changed files with 104 additions and 20 deletions

View File

@@ -1,5 +1,6 @@
from django import forms
from django.urls import reverse_lazy
from django.utils.translation import gettext as _
from netbox.registry import registry
from utilities.forms import BootstrapMixin, add_blank_choice
@@ -33,6 +34,7 @@ class DashboardWidgetAddForm(DashboardWidgetForm):
'hx-get': reverse_lazy('extras:dashboardwidget_add'),
'hx-target': '#widget_add_form',
}
)
),
label=_('Widget type')
)
field_order = ('widget_class', 'title', 'color')

View File

@@ -54,10 +54,7 @@ def get_dashboard(user):
def get_default_dashboard():
from extras.models import Dashboard
dashboard = Dashboard(
layout=[],
config={}
)
dashboard = Dashboard()
for widget in DEFAULT_DASHBOARD:
id = str(uuid.uuid4())
dashboard.layout.append({
@@ -70,6 +67,7 @@ def get_default_dashboard():
dashboard.config[id] = {
'class': widget['widget'],
'title': widget.get('title'),
'color': widget.get('color'),
'config': widget.get('config', {}),
}

View File

@@ -7,6 +7,7 @@ from django import forms
from django.contrib.contenttypes.models import ContentType
from django.core.cache import cache
from django.template.loader import render_to_string
from django.urls import NoReverseMatch, reverse
from django.utils.translation import gettext as _
from utilities.forms import BootstrapMixin
@@ -126,14 +127,26 @@ class ObjectListWidget(DashboardWidget):
model = forms.ChoiceField(
choices=get_content_type_labels
)
page_size = forms.IntegerField(
required=False,
min_value=1,
max_value=100,
help_text=_('The default number of objects to display')
)
def render(self, request):
app_label, model_name = self.config['model'].split('.')
content_type = ContentType.objects.get_by_natural_key(app_label, model_name)
viewname = get_viewname(content_type.model_class(), action='list')
try:
htmx_url = reverse(viewname)
except NoReverseMatch:
htmx_url = None
return render_to_string(self.template_name, {
'viewname': viewname,
'htmx_url': htmx_url,
'page_size': self.config.get('page_size'),
})