Skip to content

Commit

Permalink
15156 review changes
Browse files Browse the repository at this point in the history
  • Loading branch information
arthanson committed Jun 20, 2024
1 parent 1ff4e16 commit 0447572
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 39 deletions.
40 changes: 1 addition & 39 deletions netbox/netbox/api/serializers/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,52 +6,14 @@
from drf_spectacular.types import OpenApiTypes

from utilities.api import get_related_object_by_attrs
from .fields import NetBoxAPIHyperlinkedIdentityField, NetBoxURLHyperlinkedIdentityField

__all__ = (
'BaseModelSerializer',
'ValidatedModelSerializer',
)


class BaseNetBoxHyperlinkedIdentityField(serializers.HyperlinkedIdentityField):
"""
Overrides HyperlinkedIdentityField to use standard NetBox view naming
instead of passing in the view_name. Initialize with a blank view_name
and it will get replaced in the get_url call. Derived classes must
define a get_view_name.
"""
def get_url(self, obj, view_name, request, format):
"""
Given an object, return the URL that hyperlinks to the object.
May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
attributes are not configured to correctly match the URL conf.
"""
# Unsaved objects will not yet have a valid URL.
if hasattr(obj, 'pk') and obj.pk in (None, ''):
return None

lookup_value = getattr(obj, self.lookup_field)
kwargs = {self.lookup_url_kwarg: lookup_value}

model_name = self.parent.Meta.model._meta.model_name
app_name = self.parent.Meta.model._meta.app_label
view_name = self.get_view_name(app_name, model_name)
return self.reverse(view_name, kwargs=kwargs, request=request, format=format)


class NetBoxAPIHyperlinkedIdentityField(BaseNetBoxHyperlinkedIdentityField):

def get_view_name(self, app_name, model_name):
return f'{app_name}-api:{model_name}-detail'


class NetBoxURLHyperlinkedIdentityField(BaseNetBoxHyperlinkedIdentityField):

def get_view_name(self, app_name, model_name):
return f'{app_name}:{model_name}'


class BaseModelSerializer(serializers.ModelSerializer):
url = NetBoxAPIHyperlinkedIdentityField(view_name="")
display_url = NetBoxURLHyperlinkedIdentityField(view_name="")
Expand Down
45 changes: 45 additions & 0 deletions netbox/netbox/api/serializers/fields.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
from rest_framework import serializers

__all__ = (
'NetBoxAPIHyperlinkedIdentityField',
'NetBoxURLHyperlinkedIdentityField',
)


class BaseNetBoxHyperlinkedIdentityField(serializers.HyperlinkedIdentityField):
"""
Overrides HyperlinkedIdentityField to use standard NetBox view naming
instead of passing in the view_name. Initialize with a blank view_name
and it will get replaced in the get_url call. Derived classes must
define a get_view_name.
"""
def get_url(self, obj, view_name, request, format):
"""
Given an object, return the URL that hyperlinks to the object.
May raise a `NoReverseMatch` if the `view_name` and `lookup_field`
attributes are not configured to correctly match the URL conf.
"""
# Unsaved objects will not yet have a valid URL.
if hasattr(obj, 'pk') and obj.pk in (None, ''):
return None

lookup_value = getattr(obj, self.lookup_field)
kwargs = {self.lookup_url_kwarg: lookup_value}

model_name = self.parent.Meta.model._meta.model_name
app_name = self.parent.Meta.model._meta.app_label
view_name = self.get_view_name(app_name, model_name)
return self.reverse(view_name, kwargs=kwargs, request=request, format=format)


class NetBoxAPIHyperlinkedIdentityField(BaseNetBoxHyperlinkedIdentityField):

def get_view_name(self, app_name, model_name):
return f'{app_name}-api:{model_name}-detail'


class NetBoxURLHyperlinkedIdentityField(BaseNetBoxHyperlinkedIdentityField):

def get_view_name(self, app_name, model_name):
return f'{app_name}:{model_name}'

0 comments on commit 0447572

Please sign in to comment.