Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SPDX license expressions #62

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* Changelog with backdated changes (#51)
* List filtering (#54)
* Show installations on native objects (#49)
* SPDX license expressions (#47)

### Changed

Expand Down
21 changes: 10 additions & 11 deletions netbox_slm/api/serializers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
from rest_framework import serializers
from rest_framework.serializers import SerializerMethodField, HyperlinkedIdentityField

from netbox.api.serializers import NetBoxModelSerializer
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation, SoftwareLicense


class SoftwareLicenseSerializer(NetBoxModelSerializer):
display = serializers.SerializerMethodField()
url = serializers.HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwarelicense-detail")
display = SerializerMethodField()
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwarelicense-detail")

class Meta:
model = SoftwareLicense
Expand All @@ -17,6 +17,7 @@ class Meta:
"name",
"description",
"type",
"spdx_expression",
"stored_location",
"stored_location_url",
"start_date",
Expand All @@ -39,8 +40,8 @@ def get_display(self, obj):


class SoftwareProductSerializer(NetBoxModelSerializer):
display = serializers.SerializerMethodField()
url = serializers.HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwareproduct-detail")
display = SerializerMethodField()
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwareproduct-detail")

class Meta:
model = SoftwareProduct
Expand All @@ -65,10 +66,8 @@ def get_display(self, obj):


class SoftwareProductInstallationSerializer(NetBoxModelSerializer):
display = serializers.SerializerMethodField()
url = serializers.HyperlinkedIdentityField(
view_name="plugins-api:netbox_slm-api:softwareproductinstallation-detail"
)
display = SerializerMethodField()
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwareproductinstallation-detail")

class Meta:
model = SoftwareProductInstallation
Expand All @@ -94,8 +93,8 @@ def get_display(self, obj):


class SoftwareProductVersionSerializer(NetBoxModelSerializer):
display = serializers.SerializerMethodField()
url = serializers.HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwareproductversion-detail")
display = SerializerMethodField()
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwareproductversion-detail")

class Meta:
model = SoftwareProductVersion
Expand Down
1 change: 1 addition & 0 deletions netbox_slm/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
router.register("softwareproductversions", SoftwareProductVersionViewSet)
router.register("softwareproductinstallations", SoftwareProductInstallationViewSet)
router.register("softwarelicenses", SoftwareLicenseViewSet)

urlpatterns = router.urls
11 changes: 6 additions & 5 deletions netbox_slm/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
SoftwareProductInstallationFilterSet,
SoftwareLicenseFilterSet,
)
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation, SoftwareLicense
from netbox_slm.models import (
SoftwareProduct,
SoftwareProductVersion,
SoftwareProductInstallation,
SoftwareLicense,
)


class NetboxSLMRootView(APIRootView):
"""
NetboxSLM API root view
"""

def get_view_name(self):
return "NetboxSLM"

Expand Down
1 change: 1 addition & 0 deletions netbox_slm/filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ class SoftwareLicenseFilterSet(NetBoxModelFilterSet):
name = CharFilter(lookup_expr="icontains")
description = CharFilter(lookup_expr="icontains")
type = CharFilter(lookup_expr="icontains")
spdx_expression = CharFilter(lookup_expr="icontains", label="SPDX expression")
stored_location = CharFilter(lookup_expr="icontains")

software_product = ModelMultipleChoiceFilter(
Expand Down
16 changes: 15 additions & 1 deletion netbox_slm/forms/software_license.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
from django.urls import reverse_lazy

from netbox.forms import NetBoxModelForm, NetBoxModelFilterSetForm, NetBoxModelImportForm, NetBoxModelBulkEditForm
from netbox_slm.models import SoftwareProduct, SoftwareProductVersion, SoftwareProductInstallation, SoftwareLicense
from netbox_slm.models import (
SoftwareProduct,
SoftwareProductVersion,
SoftwareProductInstallation,
SoftwareLicense,
spdx_license_names,
)
from utilities.forms.constants import BOOLEAN_WITH_BLANK_CHOICES
from utilities.forms.fields import (
CommentField,
Expand All @@ -18,6 +24,7 @@
class SoftwareLicenseForm(NetBoxModelForm):
comments = CommentField()

spdx_expression = ChoiceField(required=False, choices=spdx_license_names(), label="SPDX expression")
stored_location_url = LaxURLField(required=False)
start_date = DateField(required=False, widget=DatePicker())
expiration_date = DateField(required=False, widget=DatePicker())
Expand Down Expand Up @@ -49,6 +56,7 @@ class Meta:
"description",
"software_product",
"type",
"spdx_expression",
"stored_location",
"stored_location_url",
"start_date",
Expand All @@ -70,6 +78,7 @@ class SoftwareLicenseFilterForm(NetBoxModelFilterSetForm):
"name",
"description",
"type",
"spdx_expression",
"stored_location",
"support",
"software_product_id",
Expand All @@ -84,6 +93,7 @@ class SoftwareLicenseFilterForm(NetBoxModelFilterSetForm):
name = CharField(required=False)
description = CharField(required=False)
type = CharField(required=False)
spdx_expression = CharField(required=False, label="SPDX expression")
stored_location = CharField(required=False)
support = ChoiceField(required=False, choices=BOOLEAN_WITH_BLANK_CHOICES)

Expand Down Expand Up @@ -114,6 +124,7 @@ class Meta:
"description",
"software_product",
"type",
"spdx_expression",
"stored_location",
"start_date",
"expiration_date",
Expand All @@ -130,6 +141,7 @@ class SoftwareLicenseBulkEditForm(NetBoxModelBulkEditForm):
fieldsets = (
FieldSet(
"type",
"spdx_expression",
"stored_location",
"stored_location_url",
"start_date",
Expand All @@ -143,6 +155,7 @@ class SoftwareLicenseBulkEditForm(NetBoxModelBulkEditForm):
)
nullable_fields = (
"type",
"spdx_expression",
"stored_location",
"stored_location_url",
"start_date",
Expand All @@ -157,6 +170,7 @@ class SoftwareLicenseBulkEditForm(NetBoxModelBulkEditForm):
comments = CommentField()

type = CharField(required=False)
spdx_expression = ChoiceField(required=False, choices=spdx_license_names(), label="SPDX expression")
stored_location = CharField(required=False)
stored_location_url = LaxURLField(required=False)
start_date = DateField(required=False, widget=DatePicker())
Expand Down
24 changes: 24 additions & 0 deletions netbox_slm/migrations/0010_softwarelicense_spdx_expression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.5 on 2025-02-24 18:03

import netbox_slm.models
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("netbox_slm", "0009_softwareproductversion_description"),
]

operations = [
migrations.AddField(
model_name="softwarelicense",
name="spdx_expression",
field=models.CharField(
blank=True,
max_length=64,
null=True,
validators=[netbox_slm.models.validate_spdx_expression],
),
),
]
18 changes: 18 additions & 0 deletions netbox_slm/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
from django.core.exceptions import ValidationError
from django.db import models
from django.urls import reverse
from django.utils.html import format_html, urlencode
from license_expression import Licensing, get_spdx_licensing

from netbox.models import NetBoxModel
from utilities.querysets import RestrictedQuerySet
from utilities.validators import EnhancedURLValidator

spdx_licensing: Licensing = get_spdx_licensing()


class LaxURLField(models.URLField):
"""
Expand Down Expand Up @@ -129,12 +133,26 @@ def render_type(self):
return "cluster"


def spdx_license_names():
names = [(item[0], item[0]) for item in spdx_licensing.known_symbols.items() if not item[1].is_exception]
names.sort()
names.insert(0, ("", "---------")) # set default value
return names


def validate_spdx_expression(value):
expression_info = spdx_licensing.validate(value)
if expression_info.errors:
raise ValidationError(f"{value} is not a known SPDX license expression")


class SoftwareLicense(NetBoxModel):
name = models.CharField(max_length=128)
comments = models.TextField(blank=True)

description = models.CharField(max_length=255, null=True, blank=True)
type = models.CharField(max_length=128)
spdx_expression = models.CharField(max_length=64, null=True, blank=True, validators=[validate_spdx_expression])
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)
Expand Down
8 changes: 5 additions & 3 deletions netbox_slm/tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class SoftwareProductVersionTable(NetBoxTable):

pk = ToggleColumn()
name = tables.LinkColumn()
software_product = tables.Column(accessor="software_product", linkify=True)
software_product = tables.Column(accessor="software_product", verbose_name="Software Product", linkify=True)
manufacturer = tables.Column(accessor="software_product__manufacturer", linkify=True)
installations = tables.Column(accessor="get_installation_count")

Expand Down Expand Up @@ -89,7 +89,7 @@ class SoftwareProductInstallationTable(NetBoxTable):
platform = tables.Column(accessor="platform", linkify=True)
type = tables.Column(accessor="render_type")
manufacturer = tables.Column(accessor="software_product__manufacturer", linkify=True)
software_product = tables.Column(accessor="software_product", linkify=True)
software_product = tables.Column(accessor="software_product", verbose_name="Software Product", linkify=True)
version = tables.Column(accessor="version", linkify=True)

tags = columns.TagColumn(url_name="plugins:netbox_slm:softwareproductinstallation_list")
Expand Down Expand Up @@ -137,10 +137,11 @@ class SoftwareLicenseTable(NetBoxTable):
name = tables.LinkColumn()

type = tables.Column()
spdx_expression = tables.Column(verbose_name="SPDX expression")
stored_location = tables.Column(accessor="stored_location_txt", linkify=lambda record: record.stored_location_url)

manufacturer = tables.Column(accessor="software_product__manufacturer", linkify=True)
software_product = tables.Column(accessor="software_product", linkify=True)
software_product = tables.Column(accessor="software_product", verbose_name="Software Product", linkify=True)
version = tables.Column(accessor="version", linkify=True)
installation = tables.Column(accessor="installation", linkify=True)

Expand All @@ -153,6 +154,7 @@ class Meta(NetBoxTable.Meta):
"name",
"description",
"type",
"spdx_expression",
"stored_location",
"start_date",
"expiration_date",
Expand Down
4 changes: 4 additions & 0 deletions netbox_slm/templates/netbox_slm/softwarelicense.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ <h5 class="card-header">
<th scope="row">Type</th>
<td>{{ object.type }}</td>
</tr>
<tr>
<th scope="row">SPDX expression</th>
<td>{{ object.spdx_expression }}</td>
</tr>
<tr>
<th scope="row">Software Product</th>
<td>{{ object.software_product|linkify }}</td>
Expand Down
4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ classifiers = [
"Topic :: Internet :: WWW/HTTP",
"Topic :: Internet :: WWW/HTTP :: Dynamic Content",
]
dependencies = []
dependencies = [
"license-expression == 30.4.1",
]

[project.optional-dependencies]
build = [
Expand Down