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',
|
||||
'config': {
|
||||
'models': [
|
||||
ContentType.objects.get_by_natural_key('ipam', 'aggregate').pk,
|
||||
ContentType.objects.get_by_natural_key('ipam', 'prefix').pk,
|
||||
ContentType.objects.get_by_natural_key('ipam', 'ipaddress').pk,
|
||||
'ipam.aggregate',
|
||||
'ipam.prefix',
|
||||
'ipam.ipaddress',
|
||||
]
|
||||
}
|
||||
},
|
||||
@ -25,9 +25,9 @@ DEFAULT_DASHBOARD = [
|
||||
'title': 'DCIM',
|
||||
'config': {
|
||||
'models': [
|
||||
ContentType.objects.get_by_natural_key('dcim', 'site').pk,
|
||||
ContentType.objects.get_by_natural_key('dcim', 'rack').pk,
|
||||
ContentType.objects.get_by_natural_key('dcim', 'device').pk,
|
||||
'dcim.site',
|
||||
'dcim.rack',
|
||||
'dcim.device',
|
||||
]
|
||||
}
|
||||
},
|
||||
|
@ -7,8 +7,8 @@ from django.utils.safestring import mark_safe
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from utilities.forms import BootstrapMixin
|
||||
from utilities.forms.fields import ContentTypeMultipleChoiceField
|
||||
from utilities.templatetags.builtins.filters import render_markdown
|
||||
from utilities.utils import content_type_identifier, content_type_name
|
||||
from .utils import register_widget
|
||||
|
||||
__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:
|
||||
title = None
|
||||
description = None
|
||||
@ -83,18 +90,15 @@ class ObjectCountsWidget(DashboardWidget):
|
||||
template_name = 'extras/dashboard/widgets/objectcounts.html'
|
||||
|
||||
class ConfigForm(BootstrapMixin, forms.Form):
|
||||
# TODO: Track models by app label & name rather than ContentType ID
|
||||
models = ContentTypeMultipleChoiceField(
|
||||
queryset=ContentType.objects.all()
|
||||
models = forms.MultipleChoiceField(
|
||||
choices=get_content_type_labels
|
||||
)
|
||||
|
||||
def clean_models(self):
|
||||
return [obj.pk for obj in self.cleaned_data['models']]
|
||||
|
||||
def render(self, request):
|
||||
counts = []
|
||||
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
|
||||
counts.append((model, object_count))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user