Files
NetBox-SLM/netbox_slm/api/serializers.py
T
MrBlakeandClaude Opus 5.5 0a81a04746 docs: state fork, original authors and Apache 2.0 license
Follow the attribution requirements of the Apache License 2.0 for this fork of
ICTU/netbox_slm:

- add a NOTICE file with the original copyright (ICTU) and the fork's notice
- mark every file changed since upstream 1.9.0 as modified (section 4b);
  the original copyright header in __init__.py is kept
- add a visible fork notice and a section on origin, credits, changes and
  license to the README
- point project URLs to this fork (with an Upstream link), list ICTU as
  author and MrBlake as maintainer, and stop showing ICTU's email address
  as the plugin contact
- ship NOTICE in the package and drop the MANIFEST entry for the removed
  SECURITY.md

Bump version to 1.14.1.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-11-13 15:52:13 +01:00

295 lines
11 KiB
Python

# Modified by MrBlake for the NetBox-SLM fork: https://git.mrblake.cc/MrBlake/NetBox-SLM
# Based on ICTU/netbox_slm (Apache License 2.0). See the LICENSE and NOTICE files.
from django.core.files.uploadedfile import UploadedFile
from rest_framework import serializers
from rest_framework.serializers import SerializerMethodField, HyperlinkedIdentityField
from django.utils.translation import gettext_lazy as _
from netbox.api.serializers import NetBoxModelSerializer
from netbox_slm.certificate_parser import CertificateParseError, apply_certificate_file
from netbox_slm.models import (
Certificate,
SoftwareProduct,
SoftwareProductVersion,
SoftwareProductInstallation,
SoftwareLicense,
)
class SoftwareLicenseSerializer(NetBoxModelSerializer):
display = SerializerMethodField()
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwarelicense-detail")
class Meta:
model = SoftwareLicense
fields = (
"id",
"display",
"url",
"name",
"description",
"type",
"spdx_expression",
"stored_location",
"stored_location_url",
"start_date",
"expiration_date",
"lifetime",
"renewal_interval",
"renewal_interval_unit",
"support",
"license_amount",
"license_file",
"license_key",
"provider_portal_url",
"expiration_notifications_enabled",
"default_notification_intervals",
"custom_notification_interval",
"custom_notification_interval_unit",
"email_notifications",
"software_product",
"version",
"installation",
"tenant_group",
"tenant",
"tags",
"comments",
"custom_field_data",
"created",
"last_updated",
)
brief_fields = ("id", "display", "url", "name", "description")
def get_display(self, obj):
return f"{obj}"
def validate(self, attrs):
request = self.context.get("request")
if (
"email_notifications" in attrs
and request is not None
and not request.user.has_perm("netbox_slm.manage_license_emails")
):
raise serializers.ValidationError(
{"email_notifications": _("You do not have permission to change email notifications.")}
)
tenant_group = attrs.get("tenant_group", getattr(self.instance, "tenant_group", None))
tenant = attrs.get("tenant", getattr(self.instance, "tenant", None))
if tenant_group and tenant and tenant.group_id != tenant_group.pk:
raise serializers.ValidationError(
{"tenant": _("The selected tenant does not belong to the selected tenant group.")}
)
lifetime = attrs.get("lifetime", getattr(self.instance, "lifetime", False))
renewal_interval = attrs.get("renewal_interval", getattr(self.instance, "renewal_interval", None))
renewal_interval_unit = attrs.get(
"renewal_interval_unit", getattr(self.instance, "renewal_interval_unit", "")
)
expiration_date = attrs.get("expiration_date", getattr(self.instance, "expiration_date", None))
if bool(renewal_interval) != bool(renewal_interval_unit):
raise serializers.ValidationError(
{"renewal_interval": _("Renewal interval and unit must be specified together.")}
)
if lifetime and (renewal_interval or renewal_interval_unit or expiration_date):
raise serializers.ValidationError(
{"lifetime": _("A lifetime license cannot have a renewal interval or expiration date.")}
)
custom_interval = attrs.get(
"custom_notification_interval", getattr(self.instance, "custom_notification_interval", None)
)
custom_interval_unit = attrs.get(
"custom_notification_interval_unit",
getattr(self.instance, "custom_notification_interval_unit", ""),
)
if bool(custom_interval) != bool(custom_interval_unit):
raise serializers.ValidationError(
{
"custom_notification_interval": _(
"Custom notification interval and unit must be specified together."
)
}
)
return attrs
class SoftwareProductSerializer(NetBoxModelSerializer):
display = SerializerMethodField()
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwareproduct-detail")
class Meta:
model = SoftwareProduct
fields = (
"id",
"display",
"url",
"name",
"description",
"manufacturer",
"description",
"tags",
"comments",
"custom_field_data",
"created",
"last_updated",
)
brief_fields = ("id", "display", "url", "name", "description")
def get_display(self, obj):
return f"{obj}"
class SoftwareProductInstallationSerializer(NetBoxModelSerializer):
display = SerializerMethodField()
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwareproductinstallation-detail")
class Meta:
model = SoftwareProductInstallation
fields = (
"id",
"display",
"url",
"device",
"virtualmachine",
"cluster",
"software_product",
"version",
"tags",
"comments",
"custom_field_data",
"created",
"last_updated",
)
brief_fields = ("id", "display", "url")
def get_display(self, obj):
return f"{obj}"
class SoftwareProductVersionSerializer(NetBoxModelSerializer):
display = SerializerMethodField()
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:softwareproductversion-detail")
class Meta:
model = SoftwareProductVersion
fields = (
"id",
"display",
"url",
"name",
"description",
"release_date",
"documentation_url",
"end_of_support",
"filename",
"file_checksum",
"file_link",
"release_type",
"software_product",
"tags",
"comments",
"custom_field_data",
"created",
"last_updated",
)
brief_fields = ("id", "display", "url", "name", "description")
def get_display(self, obj):
return f"{obj}"
class CertificateSerializer(NetBoxModelSerializer):
display = SerializerMethodField()
url = HyperlinkedIdentityField(view_name="plugins-api:netbox_slm-api:certificate-detail")
class Meta:
model = Certificate
fields = (
"id",
"display",
"url",
"name",
"description",
"type",
"common_name",
"subject_alternative_names",
"issuer",
"serial_number",
"fingerprint",
"start_date",
"expiration_date",
"auto_renewal",
"certificate_file",
"stored_location",
"stored_location_url",
"provider_portal_url",
"expiration_notifications_enabled",
"default_notification_intervals",
"custom_notification_interval",
"custom_notification_interval_unit",
"email_notifications",
"software_product",
"installation",
"device",
"virtualmachine",
"tenant_group",
"tenant",
"tags",
"comments",
"custom_field_data",
"created",
"last_updated",
)
brief_fields = ("id", "display", "url", "name", "description", "expiration_date")
extra_kwargs = {"name": {"required": False, "allow_blank": True}}
def get_display(self, obj):
return f"{obj}"
def validate(self, attrs):
request = self.context.get("request")
if (
"email_notifications" in attrs
and request is not None
and not request.user.has_perm("netbox_slm.manage_certificate_emails")
):
raise serializers.ValidationError(
{"email_notifications": _("You do not have permission to change email notifications.")}
)
tenant_group = attrs.get("tenant_group", getattr(self.instance, "tenant_group", None))
tenant = attrs.get("tenant", getattr(self.instance, "tenant", None))
if tenant_group and tenant and tenant.group_id != tenant_group.pk:
raise serializers.ValidationError(
{"tenant": _("The selected tenant does not belong to the selected tenant group.")}
)
uploaded_file = attrs.get("certificate_file")
if isinstance(uploaded_file, UploadedFile):
try:
apply_certificate_file(attrs, uploaded_file)
except CertificateParseError as error:
raise serializers.ValidationError({"certificate_file": str(error)}) from error
if not attrs.get("name", getattr(self.instance, "name", "")):
raise serializers.ValidationError({"name": _("Enter a name or upload a certificate file.")})
start_date = attrs.get("start_date", getattr(self.instance, "start_date", None))
expiration_date = attrs.get("expiration_date", getattr(self.instance, "expiration_date", None))
if start_date and expiration_date and start_date > expiration_date:
raise serializers.ValidationError(
{"expiration_date": _("The expiration date must not be before the start date.")}
)
custom_interval = attrs.get(
"custom_notification_interval", getattr(self.instance, "custom_notification_interval", None)
)
custom_interval_unit = attrs.get(
"custom_notification_interval_unit",
getattr(self.instance, "custom_notification_interval_unit", ""),
)
if bool(custom_interval) != bool(custom_interval_unit):
raise serializers.ValidationError(
{
"custom_notification_interval": _(
"Custom notification interval and unit must be specified together."
)
}
)
return attrs