Skip to content

Commit

Permalink
Merge pull request #188 from mlebreuil/186-support-for-netbox-41
Browse files Browse the repository at this point in the history
changes to support netbox 4.1
  • Loading branch information
mlebreuil authored Sep 12, 2024
2 parents 1f540a2 + a76f353 commit 5f201d0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 55 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@

## Version 2

### Version 2.2.6

* [186](https://github.com/mlebreuil/netbox-contract/issues/186) Code compatibility fix for Netbox 4.1

### Version 2.2.5

* Generally improve filtering options
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[project]
name = "netbox-contract"
version = "2.2.5"
version = "2.2.6"
authors = [
{ name="Marc Lebreuil", email="[email protected]" },
]
Expand Down
2 changes: 1 addition & 1 deletion src/netbox_contract/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ContractsConfig(PluginConfig):
name = 'netbox_contract'
verbose_name = 'Netbox contract'
description = 'Contract management plugin for Netbox'
version = '2.2.5'
version = '2.2.6'
author = 'Marc Lebreuil'
author_email = '[email protected]'
base_url = 'contracts'
Expand Down
66 changes: 13 additions & 53 deletions src/netbox_contract/api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
from drf_yasg.utils import swagger_serializer_method
from netbox.api.fields import ContentTypeField, SerializedPKRelatedField
from netbox.api.serializers import NetBoxModelSerializer, WritableNestedSerializer
from netbox.constants import NESTED_SERIALIZER_PREFIX
from rest_framework import serializers
from tenancy.api.nested_serializers import NestedTenantSerializer
from tenancy.api.serializers_.tenants import TenantSerializer
from utilities.api import get_serializer_for_model

from ..models import (
Expand All @@ -18,22 +17,12 @@
)


class NestedServiceProviderSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(
view_name='plugins-api:netbox_contract-api:serviceprovider-detail'
)

class Meta:
model = ServiceProvider
fields = ('id', 'url', 'display', 'name')


class NestedContractSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(
view_name='plugins-api:netbox_contract-api:contract-detail'
)
yrc = serializers.DecimalField(max_digits=10, decimal_places=2, read_only=True)
tenant = NestedTenantSerializer(many=False, required=False)
tenant = TenantSerializer(nested=True, required=False, allow_null=True)
external_partie_object_type = ContentTypeField(queryset=ContentType.objects.all())
external_partie_object = serializers.SerializerMethodField(read_only=True)

Expand Down Expand Up @@ -67,11 +56,12 @@ class Meta:
@swagger_serializer_method(serializer_or_field=serializers.JSONField)
def get_external_partie_object(self, instance):
serializer = get_serializer_for_model(
instance.external_partie_object_type.model_class(),
prefix=NESTED_SERIALIZER_PREFIX,
instance.external_partie_object_type.model_class()
)
context = {'request': self.context['request']}
return serializer(instance.external_partie_object, context=context).data
return serializer(
instance.external_partie_object, nested=True, context=context
).data


class NestedInvoiceSerializer(WritableNestedSerializer):
Expand All @@ -85,35 +75,6 @@ class Meta:
brief_fields = ('id', 'url', 'display', 'number')


class NestedContractAssignmentSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(
view_name='plugins-api:netbox_contract-api:contractassignment-detail'
)

class Meta:
model = ContractAssignment
fields = ('id', 'url', 'display', 'contract', 'content_object')
brief_fields = ('id', 'url', 'display', 'contract', 'content_object')


class NestedInvoicelineSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(
view_name='plugins-api:netbox_contract-api:invoiceline-detail'
)

class Meta:
model = InvoiceLine
fields = ('id', 'url', 'display', 'invoice', 'accounting_dimensions', 'amount')
brief_fields = (
'id',
'url',
'display',
'invoice',
'accounting_dimensions',
'amount',
)


class NestedAccountingDimensionSerializer(WritableNestedSerializer):
url = serializers.HyperlinkedIdentityField(
view_name='plugins-api:netbox_contract-api:accountingdimension-detail'
Expand All @@ -131,7 +92,7 @@ class ContractSerializer(NetBoxModelSerializer):
)
yrc = serializers.DecimalField(max_digits=10, decimal_places=2, read_only=True)
parent = NestedContractSerializer(many=False, required=False)
tenant = NestedTenantSerializer(many=False, required=False)
tenant = TenantSerializer(nested=True, required=False, allow_null=True)
external_partie_object_type = ContentTypeField(queryset=ContentType.objects.all())
external_partie_object = serializers.SerializerMethodField(read_only=True)

Expand Down Expand Up @@ -195,11 +156,12 @@ class Meta:
@swagger_serializer_method(serializer_or_field=serializers.JSONField)
def get_external_partie_object(self, instance):
serializer = get_serializer_for_model(
instance.external_partie_object_type.model_class(),
prefix=NESTED_SERIALIZER_PREFIX,
instance.external_partie_object_type.model_class()
)
context = {'request': self.context['request']}
return serializer(instance.external_partie_object, context=context).data
return serializer(
instance.external_partie_object, nested=True, context=context
).data


class InvoiceSerializer(NetBoxModelSerializer):
Expand Down Expand Up @@ -363,11 +325,9 @@ class Meta:

@swagger_serializer_method(serializer_or_field=serializers.JSONField)
def get_content_object(self, instance):
serializer = get_serializer_for_model(
instance.content_type.model_class(), prefix=NESTED_SERIALIZER_PREFIX
)
serializer = get_serializer_for_model(instance.content_type.model_class())
context = {'request': self.context['request']}
return serializer(instance.content_object, context=context).data
return serializer(instance.content_object, nested=True, context=context).data


class InvoiceLineSerializer(NetBoxModelSerializer):
Expand Down

0 comments on commit 5f201d0

Please sign in to comment.