mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-26 09:16:10 -06:00
ObjectCountsWidget: Identify models by app_label & name
This commit is contained in:
parent
98732f05aa
commit
e80428e5a1
@ -12,9 +12,9 @@ DEFAULT_DASHBOARD = [
|
|||||||
'title': 'IPAM',
|
'title': 'IPAM',
|
||||||
'config': {
|
'config': {
|
||||||
'models': [
|
'models': [
|
||||||
ContentType.objects.get_by_natural_key('ipam', 'aggregate').pk,
|
'ipam.aggregate',
|
||||||
ContentType.objects.get_by_natural_key('ipam', 'prefix').pk,
|
'ipam.prefix',
|
||||||
ContentType.objects.get_by_natural_key('ipam', 'ipaddress').pk,
|
'ipam.ipaddress',
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -25,9 +25,9 @@ DEFAULT_DASHBOARD = [
|
|||||||
'title': 'DCIM',
|
'title': 'DCIM',
|
||||||
'config': {
|
'config': {
|
||||||
'models': [
|
'models': [
|
||||||
ContentType.objects.get_by_natural_key('dcim', 'site').pk,
|
'dcim.site',
|
||||||
ContentType.objects.get_by_natural_key('dcim', 'rack').pk,
|
'dcim.rack',
|
||||||
ContentType.objects.get_by_natural_key('dcim', 'device').pk,
|
'dcim.device',
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -7,8 +7,8 @@ from django.utils.safestring import mark_safe
|
|||||||
from django.utils.translation import gettext as _
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
from utilities.forms import BootstrapMixin
|
from utilities.forms import BootstrapMixin
|
||||||
from utilities.forms.fields import ContentTypeMultipleChoiceField
|
|
||||||
from utilities.templatetags.builtins.filters import render_markdown
|
from utilities.templatetags.builtins.filters import render_markdown
|
||||||
|
from utilities.utils import content_type_identifier, content_type_name
|
||||||
from .utils import register_widget
|
from .utils import register_widget
|
||||||
|
|
||||||
__all__ = (
|
__all__ = (
|
||||||
@ -19,6 +19,13 @@ __all__ = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def get_content_type_labels():
|
||||||
|
return [
|
||||||
|
(content_type_identifier(ct), content_type_name(ct))
|
||||||
|
for ct in ContentType.objects.order_by('app_label', 'model')
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class DashboardWidget:
|
class DashboardWidget:
|
||||||
title = None
|
title = None
|
||||||
description = None
|
description = None
|
||||||
@ -83,18 +90,15 @@ class ObjectCountsWidget(DashboardWidget):
|
|||||||
template_name = 'extras/dashboard/widgets/objectcounts.html'
|
template_name = 'extras/dashboard/widgets/objectcounts.html'
|
||||||
|
|
||||||
class ConfigForm(BootstrapMixin, forms.Form):
|
class ConfigForm(BootstrapMixin, forms.Form):
|
||||||
# TODO: Track models by app label & name rather than ContentType ID
|
models = forms.MultipleChoiceField(
|
||||||
models = ContentTypeMultipleChoiceField(
|
choices=get_content_type_labels
|
||||||
queryset=ContentType.objects.all()
|
|
||||||
)
|
)
|
||||||
|
|
||||||
def clean_models(self):
|
|
||||||
return [obj.pk for obj in self.cleaned_data['models']]
|
|
||||||
|
|
||||||
def render(self, request):
|
def render(self, request):
|
||||||
counts = []
|
counts = []
|
||||||
for content_type_id in self.config['models']:
|
for content_type_id in self.config['models']:
|
||||||
model = ContentType.objects.get(pk=content_type_id).model_class()
|
app_label, model_name = content_type_id.split('.')
|
||||||
|
model = ContentType.objects.get_by_natural_key(app_label, model_name).model_class()
|
||||||
object_count = model.objects.restrict(request.user, 'view').count
|
object_count = model.objects.restrict(request.user, 'view').count
|
||||||
counts.append((model, object_count))
|
counts.append((model, object_count))
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user