Commit a2dcdd7f authored by Jean-Baptiste's avatar Jean-Baptiste

Merge branch '168-str' into 'master'

Resolve "Issue with ManyToManyField while PUTting resource"

Closes #168

See merge request !95
parents 9c944971 e761b742
Pipeline #5236 passed with stage
in 1 minute and 21 seconds
...@@ -270,35 +270,39 @@ class LDPSerializer(HyperlinkedModelSerializer): ...@@ -270,35 +270,39 @@ class LDPSerializer(HyperlinkedModelSerializer):
class JSonLDPropertyField(ReadOnlyField): class JSonLDPropertyField(ReadOnlyField):
def to_representation(self, instance): def to_representation(self, instance):
from djangoldp.views import LDPViewSet from djangoldp.views import LDPViewSet
try:
model_class = instance.model if isinstance(instance, QuerySet) or isinstance(instance, Model):
except : try:
model_class = instance.__class__ model_class = instance.model
serializer_generator = LDPViewSet(model=model_class, except :
lookup_field=Model.get_meta(model_class, 'lookup_field', 'pk'), model_class = instance.__class__
permission_classes=Model.get_meta(model_class, serializer_generator = LDPViewSet(model=model_class,
'permission_classes', lookup_field=Model.get_meta(model_class, 'lookup_field', 'pk'),
[LDPPermissions]), permission_classes=Model.get_meta(model_class,
fields=Model.get_meta(model_class, 'serializer_fields', []), 'permission_classes',
nested_fields=Model.get_meta(model_class, 'nested_fields', [])) [LDPPermissions]),
parent_depth = max(getattr(self.parent.Meta, "depth", 0) - 1, 0) fields=Model.get_meta(model_class, 'serializer_fields', []),
serializer_generator.depth = parent_depth nested_fields=Model.get_meta(model_class, 'nested_fields', []))
serializer = serializer_generator.build_read_serializer()(context=self.parent.context) parent_depth = max(getattr(self.parent.Meta, "depth", 0) - 1, 0)
if parent_depth is 0: serializer_generator.depth = parent_depth
serializer.Meta.fields = ["@id"] serializer = serializer_generator.build_read_serializer()(context=self.parent.context)
if parent_depth is 0:
if isinstance(instance, QuerySet): serializer.Meta.fields = ["@id"]
data = list(instance)
if isinstance(instance, QuerySet):
return {'@id': '{}{}{}/'.format(settings.SITE_URL, '{}{}/', self.source), data = list(instance)
'@type': 'ldp:Container',
'ldp:contains': [serializer.to_representation(item) if item is not None else None for item return {'@id': '{}{}{}/'.format(settings.SITE_URL, '{}{}/', self.source),
in data], '@type': 'ldp:Container',
'permissions': Model.get_permissions(self.parent.Meta.model, self.context['request'].user, 'ldp:contains': [serializer.to_representation(item) if item is not None else None for item
['view', 'add']) in data],
} 'permissions': Model.get_permissions(self.parent.Meta.model, self.context['request'].user,
['view', 'add'])
}
else:
return serializer.to_representation(instance)
else: else:
return serializer.to_representation(instance) return instance
field_class = JSonLDPropertyField field_class = JSonLDPropertyField
field_kwargs = {} field_kwargs = {}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment