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

Non-Model Serializer #115

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
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
29 changes: 18 additions & 11 deletions drf_hal_json/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from rest_framework.serializers import BaseSerializer, HyperlinkedModelSerializer, ListSerializer
from rest_framework.utils.field_mapping import get_nested_relation_kwargs
from rest_framework.utils.serializer_helpers import ReturnDict
from rest_framework.serializers import Serializer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please move this to the existing serializers imports.



class HalListSerializer(ListSerializer):
Expand All @@ -30,16 +31,12 @@ def data(self):
)


class HalModelSerializer(HyperlinkedModelSerializer):
"""
Serializer for HAL representation of django models
"""
serializer_related_field = HyperlinkedRelatedField
serializer_url_field = HalHyperlinkedIdentityField
class HalSerializerMixin(Serializer):
ambsw-technology marked this conversation as resolved.
Show resolved Hide resolved

default_list_serializer = HalListSerializer

def __init__(self, instance=None, data=empty, **kwargs):
super(HalModelSerializer, self).__init__(instance, data, **kwargs)
super(HalSerializerMixin, self).__init__(instance, data, **kwargs)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

self.nested_serializer_class = self.__class__
if data != empty and not LINKS_FIELD_NAME in data:
data[LINKS_FIELD_NAME] = dict() # put links in data, so that field validation does not fail
Expand All @@ -56,7 +53,7 @@ class Meta:
list_serializer_class = getattr(meta, 'list_serializer_class', None)
if list_serializer_class is None:
setattr(meta, 'list_serializer_class', cls.default_list_serializer)
return super(HalModelSerializer, cls).many_init(*args, **kwargs)
return super(HalSerializerMixin, cls).many_init(*args, **kwargs)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not call setattr with a constant attribute value, it is not any safer than normal property access.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'


def build_link_object(self, val):
if (type([]) == type(val)):
Expand All @@ -72,7 +69,7 @@ def _get_url(self, item):
return None

def to_representation(self, instance):
ret = super(HalModelSerializer, self).to_representation(instance)
ret = super(HalSerializerMixin, self).to_representation(instance)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

resp = defaultdict(dict)

for field_name in self.link_field_names:
Expand All @@ -87,6 +84,8 @@ def to_representation(self, instance):
for field_name in self.embedded_field_names:
# if a related resource is embedded, it should still
# get a link in the parent object
if field_name not in ret:
continue
if type(ret[field_name]) == list:
embed_self = list(filter(lambda x: x is not None, [self._get_url(x) for x in ret[field_name] if x]))
else:
Expand All @@ -99,7 +98,7 @@ def to_representation(self, instance):
return resp

def get_fields(self):
fields = super(HalModelSerializer, self).get_fields()
fields = super(HalSerializerMixin, self).get_fields()

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'


self.embedded_field_names = []
self.link_field_names = []
Expand Down Expand Up @@ -133,7 +132,7 @@ def build_nested_field(self, field_name, relation_info, nested_depth):
"""
Create nested fields for forward and reverse relationships.
"""
class NestedSerializer(HalModelSerializer):
class NestedSerializer(HalSerializerMixin):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

class Meta:
model = relation_info.related_model
depth = nested_depth - 1
Expand All @@ -143,3 +142,11 @@ class Meta:
field_kwargs = get_nested_relation_kwargs(relation_info)

return field_class, field_kwargs


class HalModelSerializer(HalSerializerMixin, HyperlinkedModelSerializer):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

"""

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

undefined name 'HalSerializerMixin'

Serializer for HAL representation of django models
"""
serializer_related_field = HyperlinkedRelatedField
serializer_url_field = HalHyperlinkedIdentityField