From 2501e41b7c31e5fc5953660a3f246916faba2441 Mon Sep 17 00:00:00 2001 From: wkoot <3715211+wkoot@users.noreply.github.com> Date: Fri, 22 Sep 2023 02:42:02 +0200 Subject: [PATCH] Add 'stored_location_url' to SoftwareLicense (#28) Closes #27 --- netbox_slm/forms/software_license.py | 6 ++++- ...006_softwarelicense_stored_location_url.py | 19 +++++++++++++ netbox_slm/models.py | 17 ++++++++++++ netbox_slm/tables.py | 27 ++++++++++--------- .../templates/netbox_slm/softwarelicense.html | 6 ++++- 5 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 netbox_slm/migrations/0006_softwarelicense_stored_location_url.py diff --git a/netbox_slm/forms/software_license.py b/netbox_slm/forms/software_license.py index 193f702..7805b9d 100644 --- a/netbox_slm/forms/software_license.py +++ b/netbox_slm/forms/software_license.py @@ -4,13 +4,15 @@ from django.utils.translation import gettext_lazy as _ from netbox.forms import NetBoxModelForm, NetBoxModelImportForm, NetBoxModelBulkEditForm, NetBoxModelFilterSetForm from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation, SoftwareLicense -from utilities.forms.fields import DynamicModelChoiceField, TagFilterField +from utilities.forms.fields import DynamicModelChoiceField, TagFilterField, LaxURLField from utilities.forms.widgets import APISelect, DatePicker class SoftwareLicenseForm(NetBoxModelForm): """Form for creating a new SoftwareLicense object.""" + stored_location_url = LaxURLField(required=False) + software_product = DynamicModelChoiceField( queryset=SoftwareProduct.objects.all(), required=True, @@ -44,6 +46,7 @@ class SoftwareLicenseForm(NetBoxModelForm): "description", "type", "stored_location", + "stored_location_url", "start_date", "expiration_date", "software_product", @@ -76,6 +79,7 @@ class SoftwareLicenseImportForm(NetBoxModelImportForm): "description", "type", "stored_location", + "stored_location_url", "start_date", "expiration_date", ) diff --git a/netbox_slm/migrations/0006_softwarelicense_stored_location_url.py b/netbox_slm/migrations/0006_softwarelicense_stored_location_url.py new file mode 100644 index 0000000..3dcb9e6 --- /dev/null +++ b/netbox_slm/migrations/0006_softwarelicense_stored_location_url.py @@ -0,0 +1,19 @@ +# Generated by Django 4.2.5 on 2023-09-22 00:34 + +from django.db import migrations +import netbox_slm.models + + +class Migration(migrations.Migration): + + dependencies = [ + ('netbox_slm', '0005_add_software_license'), + ] + + operations = [ + migrations.AddField( + model_name='softwarelicense', + name='stored_location_url', + field=netbox_slm.models.LaxURLField(blank=True, max_length=1024, null=True), + ), + ] diff --git a/netbox_slm/models.py b/netbox_slm/models.py index 7a70605..d9f3107 100644 --- a/netbox_slm/models.py +++ b/netbox_slm/models.py @@ -4,6 +4,16 @@ from django.utils import safestring from netbox.models import NetBoxModel from utilities.querysets import RestrictedQuerySet +from utilities.validators import EnhancedURLValidator + + +class LaxURLField(models.URLField): + """ + NetBox Custom Field approach, based on utilities.forms.fields.LaxURLField + Overriding default_validators is needed, as they are always added + """ + + default_validators = [EnhancedURLValidator()] class SoftwareProduct(NetBoxModel): @@ -94,6 +104,7 @@ class SoftwareLicense(NetBoxModel): type = models.CharField(max_length=128) stored_location = models.CharField(max_length=255, null=True, blank=True) + stored_location_url = LaxURLField(max_length=1024, null=True, blank=True) start_date = models.DateField(null=True, blank=True) expiration_date = models.DateField(null=True, blank=True) @@ -110,3 +121,9 @@ class SoftwareLicense(NetBoxModel): def get_absolute_url(self): return reverse("plugins:netbox_slm:softwarelicense", kwargs={"pk": self.pk}) + + @property + def stored_location_txt(self): + if self.stored_location_url and not self.stored_location: + return "Link" + return self.stored_location diff --git a/netbox_slm/tables.py b/netbox_slm/tables.py index 374b6f8..3e3ab9b 100644 --- a/netbox_slm/tables.py +++ b/netbox_slm/tables.py @@ -1,6 +1,5 @@ import django_tables2 as tables from django.db.models import Count -from django_tables2.utils import Accessor from netbox.tables import NetBoxTable, ToggleColumn, columns from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation, SoftwareLicense @@ -11,7 +10,7 @@ class SoftwareProductTable(NetBoxTable): pk = ToggleColumn() name = tables.LinkColumn() - manufacturer = tables.Column(accessor=Accessor("manufacturer"), linkify=True) + manufacturer = tables.Column(accessor="manufacturer", linkify=True) installations = tables.Column(accessor="get_installation_count", verbose_name="Installations") tags = columns.TagColumn(url_name="plugins:netbox_slm:softwareproduct_list") @@ -53,8 +52,8 @@ class SoftwareProductVersionTable(NetBoxTable): pk = ToggleColumn() name = tables.LinkColumn(verbose_name="Version") - software_product = tables.Column(accessor=Accessor("software_product"), linkify=True) - manufacturer = tables.Column(accessor=Accessor("software_product__manufacturer"), linkify=True) + software_product = tables.Column(accessor="software_product", linkify=True) + manufacturer = tables.Column(accessor="software_product__manufacturer", linkify=True) installations = tables.Column(accessor="get_installation_count", verbose_name="Installations") tags = columns.TagColumn(url_name="plugins:netbox_slm:softwareproductversion_list") @@ -96,12 +95,12 @@ class SoftwareProductInstallationTable(NetBoxTable): pk = ToggleColumn() name = tables.LinkColumn() - device = tables.Column(accessor=Accessor("device"), linkify=True) - virtualmachine = tables.Column(accessor=Accessor("virtualmachine"), linkify=True) - platform = tables.Column(accessor=Accessor("platform"), linkify=True) + device = tables.Column(accessor="device", linkify=True) + virtualmachine = tables.Column(accessor="virtualmachine", linkify=True) + platform = tables.Column(accessor="platform", linkify=True) type = tables.Column(accessor="render_type") - software_product = tables.Column(accessor=Accessor("software_product"), linkify=True) - version = tables.Column(accessor=Accessor("version"), linkify=True) + software_product = tables.Column(accessor="software_product", linkify=True) + version = tables.Column(accessor="version", linkify=True) tags = columns.TagColumn(url_name="plugins:netbox_slm:softwareproductinstallation_list") @@ -143,10 +142,12 @@ class SoftwareLicenseTable(NetBoxTable): pk = ToggleColumn() name = tables.LinkColumn() - type = tables.Column(accessor="render_type") - software_product = tables.Column(accessor=Accessor("software_product"), linkify=True) - version = tables.Column(accessor=Accessor("version"), linkify=True) - installation = tables.Column(accessor=Accessor("installation"), linkify=True) + type = tables.Column() + stored_location = tables.Column(accessor="stored_location_txt", linkify=lambda record: record.stored_location_url) + + software_product = tables.Column(accessor="software_product", linkify=True) + version = tables.Column(accessor="version", linkify=True) + installation = tables.Column(accessor="installation", linkify=True) tags = columns.TagColumn(url_name="plugins:netbox_slm:softwarelicense_list") diff --git a/netbox_slm/templates/netbox_slm/softwarelicense.html b/netbox_slm/templates/netbox_slm/softwarelicense.html index 04a39c5..ad1ab42 100644 --- a/netbox_slm/templates/netbox_slm/softwarelicense.html +++ b/netbox_slm/templates/netbox_slm/softwarelicense.html @@ -27,7 +27,11 @@