From 3fa63b774ea5ca3da67d78460b41c5b467e3cf5d Mon Sep 17 00:00:00 2001 From: Jeremy Stretch Date: Fri, 19 May 2017 16:03:51 -0400 Subject: [PATCH] Converted home view to a CBV --- netbox/netbox/urls.py | 4 +-- netbox/netbox/views.py | 66 ++++++++++++++++++++++-------------------- 2 files changed, 37 insertions(+), 33 deletions(-) diff --git a/netbox/netbox/urls.py b/netbox/netbox/urls.py index a1e7eb351..6d946d90e 100644 --- a/netbox/netbox/urls.py +++ b/netbox/netbox/urls.py @@ -5,7 +5,7 @@ from django.conf.urls import include, url from django.contrib import admin from django.views.static import serve -from netbox.views import APIRootView, home, handle_500, SearchView, trigger_500 +from netbox.views import APIRootView, handle_500, HomeView, SearchView, trigger_500 from users.views import LoginView, LogoutView @@ -15,7 +15,7 @@ swagger_view = get_swagger_view(title='NetBox API') _patterns = [ # Base views - url(r'^$', home, name='home'), + url(r'^$', HomeView.as_view(), name='home'), url(r'^search/$', SearchView.as_view(), name='search'), # Login/logout diff --git a/netbox/netbox/views.py b/netbox/netbox/views.py index f602a46f4..793f3c9a2 100644 --- a/netbox/netbox/views.py +++ b/netbox/netbox/views.py @@ -115,43 +115,46 @@ SEARCH_TYPES = OrderedDict(( )) -def home(request): +class HomeView(View): + template_name = 'home.html' - stats = { + def get(self, request): - # Organization - 'site_count': Site.objects.count(), - 'tenant_count': Tenant.objects.count(), + stats = { - # DCIM - 'rack_count': Rack.objects.count(), - 'device_count': Device.objects.count(), - 'interface_connections_count': InterfaceConnection.objects.count(), - 'console_connections_count': ConsolePort.objects.filter(cs_port__isnull=False).count(), - 'power_connections_count': PowerPort.objects.filter(power_outlet__isnull=False).count(), + # Organization + 'site_count': Site.objects.count(), + 'tenant_count': Tenant.objects.count(), - # IPAM - 'vrf_count': VRF.objects.count(), - 'aggregate_count': Aggregate.objects.count(), - 'prefix_count': Prefix.objects.count(), - 'ipaddress_count': IPAddress.objects.count(), - 'vlan_count': VLAN.objects.count(), + # DCIM + 'rack_count': Rack.objects.count(), + 'device_count': Device.objects.count(), + 'interface_connections_count': InterfaceConnection.objects.count(), + 'console_connections_count': ConsolePort.objects.filter(cs_port__isnull=False).count(), + 'power_connections_count': PowerPort.objects.filter(power_outlet__isnull=False).count(), - # Circuits - 'provider_count': Provider.objects.count(), - 'circuit_count': Circuit.objects.count(), + # IPAM + 'vrf_count': VRF.objects.count(), + 'aggregate_count': Aggregate.objects.count(), + 'prefix_count': Prefix.objects.count(), + 'ipaddress_count': IPAddress.objects.count(), + 'vlan_count': VLAN.objects.count(), - # Secrets - 'secret_count': Secret.objects.count(), + # Circuits + 'provider_count': Provider.objects.count(), + 'circuit_count': Circuit.objects.count(), - } + # Secrets + 'secret_count': Secret.objects.count(), - return render(request, 'home.html', { - 'search_form': SearchForm(), - 'stats': stats, - 'topology_maps': TopologyMap.objects.filter(site__isnull=True), - 'recent_activity': UserAction.objects.select_related('user')[:50] - }) + } + + return render(request, self.template_name, { + 'search_form': SearchForm(), + 'stats': stats, + 'topology_maps': TopologyMap.objects.filter(site__isnull=True), + 'recent_activity': UserAction.objects.select_related('user')[:50] + }) class SearchView(View): @@ -235,5 +238,6 @@ def trigger_500(request): """ Hot-wired method of triggering a server error to test reporting """ - raise Exception("Congratulations, you've triggered an exception! Go tell all your friends what an exceptional " - "person you are.") + raise Exception( + "Congratulations, you've triggered an exception! Go tell all your friends what an exceptional person you are." + )