fixed reverse relationship naming (to default) and added ordering methods for calculated attributes

This commit is contained in:
Hedde van der Heide
2022-04-15 09:00:36 +02:00
parent a02acbc561
commit 18fed2c029
4 changed files with 16 additions and 7 deletions
-6
View File
@@ -17,7 +17,6 @@ class SoftwareProduct(NetBoxModel):
manufacturer = models.ForeignKey( manufacturer = models.ForeignKey(
to='dcim.Manufacturer', to='dcim.Manufacturer',
on_delete=models.PROTECT, on_delete=models.PROTECT,
related_name='software_products',
null=True, blank=True null=True, blank=True
) )
@@ -42,7 +41,6 @@ class SoftwareProductVersion(NetBoxModel):
software_product = models.ForeignKey( software_product = models.ForeignKey(
to='netbox_slm.SoftwareProduct', to='netbox_slm.SoftwareProduct',
on_delete=models.PROTECT, on_delete=models.PROTECT,
related_name='softwareproduct_versions'
) )
name = models.CharField(max_length=64) name = models.CharField(max_length=64)
@@ -67,26 +65,22 @@ class SoftwareProductInstallation(NetBoxModel):
device = models.ForeignKey( device = models.ForeignKey(
to='dcim.Device', to='dcim.Device',
on_delete=models.PROTECT, on_delete=models.PROTECT,
related_name='softwareproduct_installations',
null=True, null=True,
blank=True blank=True
) )
virtualmachine = models.ForeignKey( virtualmachine = models.ForeignKey(
to='virtualization.VirtualMachine', to='virtualization.VirtualMachine',
on_delete=models.PROTECT, on_delete=models.PROTECT,
related_name='softwareproduct_installations',
null = True, null = True,
blank = True blank = True
) )
software_product = models.ForeignKey( software_product = models.ForeignKey(
to='netbox_slm.SoftwareProduct', to='netbox_slm.SoftwareProduct',
on_delete=models.PROTECT, on_delete=models.PROTECT,
related_name='software_products'
) )
version = models.ForeignKey( version = models.ForeignKey(
to='netbox_slm.SoftwareProductVersion', to='netbox_slm.SoftwareProductVersion',
on_delete=models.PROTECT, on_delete=models.PROTECT,
related_name='softwareproduct_versions'
) )
objects = RestrictedQuerySet.as_manager() objects = RestrictedQuerySet.as_manager()
+13
View File
@@ -1,5 +1,6 @@
import django_tables2 as tables import django_tables2 as tables
from django.db.models import Count
from django_tables2.utils import Accessor from django_tables2.utils import Accessor
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation
from netbox.tables import NetBoxTable, ChoiceFieldColumn, ToggleColumn, columns from netbox.tables import NetBoxTable, ChoiceFieldColumn, ToggleColumn, columns
@@ -45,6 +46,12 @@ class SoftwareProductTable(NetBoxTable):
"installations", "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): class SoftwareProductVersionTable(NetBoxTable):
"""Table for displaying SoftwareProductVersion objects.""" """Table for displaying SoftwareProductVersion objects."""
@@ -90,6 +97,12 @@ class SoftwareProductVersionTable(NetBoxTable):
"installations", "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): class SoftwareProductInstallationTable(NetBoxTable):
"""Table for displaying SoftwareProductInstallation objects.""" """Table for displaying SoftwareProductInstallation objects."""
@@ -24,6 +24,8 @@
<a href="{% url 'plugins:netbox_slm:softwareproductversion' pk=version.pk %}"> <a href="{% url 'plugins:netbox_slm:softwareproductversion' pk=version.pk %}">
<span class="badge" style="color: #ffffff; background-color: #9e9e9e">{{ version }}</span> <span class="badge" style="color: #ffffff; background-color: #9e9e9e">{{ version }}</span>
</a> </a>
{% empty %}
n/a
{% endfor %} {% endfor %}
</td> </td>
</tr> </tr>
+1 -1
View File
@@ -28,7 +28,7 @@ class SoftwareProductView(generic.ObjectView):
queryset = SoftwareProduct.objects.all() queryset = SoftwareProduct.objects.all()
def get_extra_context(self, request, instance): def get_extra_context(self, request, instance):
versions = instance.softwareproduct_versions.all() versions = instance.softwareproductversion_set.all()
return {"versions": versions} return {"versions": versions}