From 18fed2c0299013c68a5702e5f9e02d53957ebbf8 Mon Sep 17 00:00:00 2001 From: Hedde van der Heide Date: Fri, 15 Apr 2022 09:00:36 +0200 Subject: [PATCH] fixed reverse relationship naming (to default) and added ordering methods for calculated attributes --- src/netbox_slm/models.py | 6 ------ src/netbox_slm/tables.py | 13 +++++++++++++ .../templates/netbox_slm/softwareproduct.html | 2 ++ src/netbox_slm/views.py | 2 +- 4 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/netbox_slm/models.py b/src/netbox_slm/models.py index 298b885..37f56c4 100644 --- a/src/netbox_slm/models.py +++ b/src/netbox_slm/models.py @@ -17,7 +17,6 @@ class SoftwareProduct(NetBoxModel): manufacturer = models.ForeignKey( to='dcim.Manufacturer', on_delete=models.PROTECT, - related_name='software_products', null=True, blank=True ) @@ -42,7 +41,6 @@ class SoftwareProductVersion(NetBoxModel): software_product = models.ForeignKey( to='netbox_slm.SoftwareProduct', on_delete=models.PROTECT, - related_name='softwareproduct_versions' ) name = models.CharField(max_length=64) @@ -67,26 +65,22 @@ class SoftwareProductInstallation(NetBoxModel): device = models.ForeignKey( to='dcim.Device', on_delete=models.PROTECT, - related_name='softwareproduct_installations', null=True, blank=True ) virtualmachine = models.ForeignKey( to='virtualization.VirtualMachine', on_delete=models.PROTECT, - related_name='softwareproduct_installations', null = True, blank = True ) software_product = models.ForeignKey( to='netbox_slm.SoftwareProduct', on_delete=models.PROTECT, - related_name='software_products' ) version = models.ForeignKey( to='netbox_slm.SoftwareProductVersion', on_delete=models.PROTECT, - related_name='softwareproduct_versions' ) objects = RestrictedQuerySet.as_manager() diff --git a/src/netbox_slm/tables.py b/src/netbox_slm/tables.py index 59fe1f1..3accc26 100644 --- a/src/netbox_slm/tables.py +++ b/src/netbox_slm/tables.py @@ -1,5 +1,6 @@ import django_tables2 as tables +from django.db.models import Count from django_tables2.utils import Accessor from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation from netbox.tables import NetBoxTable, ChoiceFieldColumn, ToggleColumn, columns @@ -45,6 +46,12 @@ class SoftwareProductTable(NetBoxTable): "installations", ) + def order_installations(self, queryset, is_descending): + queryset = queryset.annotate( + count=Count('softwareproductinstallation__id') + ).order_by(("-" if is_descending else "") + "count") + return queryset, True + class SoftwareProductVersionTable(NetBoxTable): """Table for displaying SoftwareProductVersion objects.""" @@ -90,6 +97,12 @@ class SoftwareProductVersionTable(NetBoxTable): "installations", ) + def order_installations(self, queryset, is_descending): + queryset = queryset.annotate( + count=Count('softwareproductinstallation__id') + ).order_by(("-" if is_descending else "") + "count") + return queryset, True + class SoftwareProductInstallationTable(NetBoxTable): """Table for displaying SoftwareProductInstallation objects.""" diff --git a/src/netbox_slm/templates/netbox_slm/softwareproduct.html b/src/netbox_slm/templates/netbox_slm/softwareproduct.html index aa3fa97..b179995 100644 --- a/src/netbox_slm/templates/netbox_slm/softwareproduct.html +++ b/src/netbox_slm/templates/netbox_slm/softwareproduct.html @@ -24,6 +24,8 @@ {{ version }} + {% empty %} + n/a {% endfor %} diff --git a/src/netbox_slm/views.py b/src/netbox_slm/views.py index 5c6bc49..504d093 100644 --- a/src/netbox_slm/views.py +++ b/src/netbox_slm/views.py @@ -28,7 +28,7 @@ class SoftwareProductView(generic.ObjectView): queryset = SoftwareProduct.objects.all() def get_extra_context(self, request, instance): - versions = instance.softwareproduct_versions.all() + versions = instance.softwareproductversion_set.all() return {"versions": versions}