Add 'stored_location_url' to SoftwareLicense (#28)

Closes #27
This commit is contained in:
wkoot
2023-09-22 02:42:02 +02:00
committed by GitHub
parent aa41f8a402
commit 2501e41b7c
5 changed files with 60 additions and 15 deletions
+5 -1
View File
@@ -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",
)
@@ -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),
),
]
+17
View File
@@ -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
+14 -13
View File
@@ -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")
@@ -27,7 +27,11 @@
</tr>
<tr>
<th scope="row">Stored location</th>
<td>{{ object.stored_location }}</td>
{% if object.stored_location_url %}
<td><a href="{{ object.stored_location_url }}">{{ object.stored_location_txt }}</a></td>
{% else %}
<td>{{ object.stored_location_txt }}</td>
{% endif %}
</tr>
<tr>
<th scope="row">Start date</th>