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

Fix issue with serializers with brief fields and nested serializers #26

Merged
merged 1 commit into from
Sep 18, 2024
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
24 changes: 15 additions & 9 deletions netbox_lifecycle/api/_serializers/contract.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,36 @@ class SupportSKUSerializer(NetBoxModelSerializer):

class Meta:
model = SupportSKU
fields = ('url', 'id', 'display', 'manufacturer', 'sku')
fields = ('url', 'id', 'display', 'manufacturer', 'sku', 'description', 'comments', )
brief_fields = ('url', 'id', 'display', 'manufacturer', 'sku', )


class SupportContractSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_lifecycle-api:hardwarelifecycle-detail')
vendor = VendorSerializer(nested=True)
start = serializers.DateField()
renewal = serializers.DateField()
end = serializers.DateField()
start = serializers.DateField(required=False)
renewal = serializers.DateField(required=False)
end = serializers.DateField(required=False)

class Meta:
model = SupportContract
fields = ('url', 'id', 'display', 'vendor', 'contract_id', 'start', 'renewal', 'end', )
fields = (
'url', 'id', 'display', 'vendor', 'contract_id', 'start', 'renewal', 'end', 'description', 'comments',
)
brief_fields = ('url', 'id', 'display', 'vendor', 'contract_id', )


class SupportContractAssignmentSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_lifecycle-api:licenseassignment-detail')
contract = SupportContractSerializer(nested=True)

device = DeviceSerializer(nested=True)
license = LicenseAssignmentSerializer(nested=True)
sku = SupportSKUSerializer(nested=True, required=False, allow_null=True)
device = DeviceSerializer(nested=True, required=False, allow_null=True)
license = LicenseAssignmentSerializer(nested=True, required=False, allow_null=True)

class Meta:
model = SupportContractAssignment
fields = (
'url', 'id', 'display', 'contract', 'device', 'license', 'end'
'url', 'id', 'display', 'contract', 'sku', 'device', 'license', 'end', 'description', 'comments',
)

brief_fields = ('url', 'id', 'display', 'contract', 'sku', 'device', 'license', )
15 changes: 9 additions & 6 deletions netbox_lifecycle/api/_serializers/hardware.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,22 @@ class HardwareLifecycleSerializer(NetBoxModelSerializer):
assigned_object_type = ContentTypeField(
queryset=ContentType.objects.all()
)
assigned_object = serializers.SerializerMethodField(read_only=True)

end_of_sale = serializers.DateField()
end_of_maintenance = serializers.DateField()
end_of_security = serializers.DateField()
last_contract_date = serializers.DateField()
end_of_maintenance = serializers.DateField(required=False)
end_of_security = serializers.DateField(required=False)
last_contract_date = serializers.DateField(required=False)
end_of_support = serializers.DateField()

class Meta:
model = HardwareLifecycle
fields = (
'url', 'id', 'display', 'assigned_object_type', 'assigned_object_id', 'assigned_object', 'end_of_sale',
'end_of_maintenance', 'end_of_security', 'last_contract_date', 'end_of_support', 'notice', 'documentation'
'url', 'id', 'display', 'assigned_object_type', 'assigned_object_id', 'end_of_sale',
'end_of_maintenance', 'end_of_security', 'last_contract_date', 'end_of_support', 'notice', 'documentation',
'description', 'comments',
)
brief_fields = (
'url', 'id', 'display', 'assigned_object_type', 'assigned_object_id', 'end_of_sale',
)

@extend_schema_field(serializers.JSONField(allow_null=True))
Expand Down
8 changes: 5 additions & 3 deletions netbox_lifecycle/api/_serializers/license.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,17 @@ class LicenseSerializer(NetBoxModelSerializer):

class Meta:
model = License
fields = ('url', 'id', 'display', 'name', 'manufacturer', )
fields = ('url', 'id', 'display', 'name', 'manufacturer', 'description', 'comments', )
brief_fields = ('url', 'id', 'display', 'name', )


class LicenseAssignmentSerializer(NetBoxModelSerializer):
url = serializers.HyperlinkedIdentityField(view_name='plugins-api:netbox_lifecycle-api:licenseassignment-detail')
license = LicenseSerializer(nested=True)
vendor = VendorSerializer(nested=True)
device = DeviceSerializer(nested=True)
device = DeviceSerializer(nested=True, required=False, allow_null=True)

class Meta:
model = LicenseAssignment
fields = ('url', 'id', 'display', 'vendor', 'license', 'device')
fields = ('url', 'id', 'display', 'vendor', 'license', 'device', 'description', 'comments', )
brief_fields = ('url', 'id', 'display', 'vendor', 'license', 'device', )
3 changes: 2 additions & 1 deletion netbox_lifecycle/api/_serializers/vendor.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ class VendorSerializer(NetBoxModelSerializer):

class Meta:
model = Vendor
fields = ('url', 'id', 'display', 'name')
fields = ('url', 'id', 'display', 'name', 'description', 'comments', )
brief_fields = ('url', 'id', 'display', 'name', )
10 changes: 5 additions & 5 deletions netbox_lifecycle/api/views/contract.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from rest_framework.viewsets import ModelViewSet

from netbox.api.viewsets import NetBoxModelViewSet
from netbox_lifecycle.api.serializers import VendorSerializer, SupportContractSerializer, \
SupportContractAssignmentSerializer, SupportSKUSerializer
from netbox_lifecycle.models import Vendor, SupportContract, SupportContractAssignment, SupportSKU
Expand All @@ -12,21 +12,21 @@
)


class VendorViewSet(ModelViewSet):
class VendorViewSet(NetBoxModelViewSet):
queryset = Vendor.objects.all()
serializer_class = VendorSerializer


class SupportSKUViewSet(ModelViewSet):
class SupportSKUViewSet(NetBoxModelViewSet):
queryset = SupportSKU.objects.all()
serializer_class = SupportSKUSerializer


class SupportContractViewSet(ModelViewSet):
class SupportContractViewSet(NetBoxModelViewSet):
queryset = SupportContract.objects.all()
serializer_class = SupportContractSerializer


class SupportContractAssignmentViewSet(ModelViewSet):
class SupportContractAssignmentViewSet(NetBoxModelViewSet):
queryset = SupportContractAssignment.objects.all()
serializer_class = SupportContractAssignmentSerializer
5 changes: 2 additions & 3 deletions netbox_lifecycle/api/views/hardware.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from rest_framework.viewsets import ModelViewSet

from netbox.api.viewsets import NetBoxModelViewSet
from netbox_lifecycle.api.serializers import HardwareLifecycleSerializer
from netbox_lifecycle.models import HardwareLifecycle

Expand All @@ -9,6 +8,6 @@
)


class HardwareLifecycleViewSet(ModelViewSet):
class HardwareLifecycleViewSet(NetBoxModelViewSet):
queryset = HardwareLifecycle.objects.all()
serializer_class = HardwareLifecycleSerializer
7 changes: 3 additions & 4 deletions netbox_lifecycle/api/views/license.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from rest_framework.viewsets import ModelViewSet

from netbox.api.viewsets import NetBoxModelViewSet
from netbox_lifecycle.api.serializers import LicenseSerializer, LicenseAssignmentSerializer
from netbox_lifecycle.models import License, LicenseAssignment

Expand All @@ -10,11 +9,11 @@
)


class LicenseViewSet(ModelViewSet):
class LicenseViewSet(NetBoxModelViewSet):
queryset = License.objects.all()
serializer_class = LicenseSerializer


class LicenseAssignmentViewSet(ModelViewSet):
class LicenseAssignmentViewSet(NetBoxModelViewSet):
queryset = LicenseAssignment.objects.all()
serializer_class = LicenseAssignmentSerializer
Loading