diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index 72b90a4133e91ed318f820211bed681fbd3064f0..ba22a0398c32b9ff4b8317ffa231c01318c426c5 100644 --- a/djangoldp/serializers.py +++ b/djangoldp/serializers.py @@ -269,23 +269,27 @@ class LDPSerializer(HyperlinkedModelSerializer): def build_property_field(self, field_name, model_class): class JSonLDPropertyField(ReadOnlyField): def to_representation(self, instance): + from djangoldp.views import LDPViewSet + try: + model_class = instance.model + except : + model_class = instance.__class__ + serializer_generator = LDPViewSet(model=model_class, + lookup_field=Model.get_meta(model_class, 'lookup_field', 'pk'), + permission_classes=Model.get_meta(model_class, + 'permission_classes', + [LDPPermissions]), + fields=Model.get_meta(model_class, 'serializer_fields', []), + nested_fields=Model.get_meta(model_class, 'nested_fields', [])) + parent_depth = max(getattr(self.parent.Meta, "depth", 0) - 1, 0) + serializer_generator.depth = parent_depth + serializer = serializer_generator.build_read_serializer()(context=self.parent.context) + if parent_depth is 0: + serializer.Meta.fields = ["@id"] + if isinstance(instance, QuerySet): data = list(instance) - model_class = instance.model - from djangoldp.views import LDPViewSet - - serializer_generator = LDPViewSet(model=model_class, - lookup_field=Model.get_meta(model_class, 'lookup_field', 'pk'), - permission_classes=Model.get_meta(model_class, - 'permission_classes', - [LDPPermissions]), - fields=Model.get_meta(model_class, 'serializer_fields', []), - nested_fields=Model.get_meta(model_class, 'nested_fields', [])) - parent_depth = max(getattr(self.parent.Meta, "depth", 0) - 1, 0) - serializer_generator.depth = parent_depth - serializer = serializer_generator.build_read_serializer()(context=self.parent.context) - if parent_depth is 0: - serializer.Meta.fields = ["@id"] + return {'@id': '{}{}{}/'.format(settings.SITE_URL, '{}{}/', self.source), '@type': 'ldp:Container', 'ldp:contains': [serializer.to_representation(item) if item is not None else None for item @@ -294,7 +298,7 @@ class LDPSerializer(HyperlinkedModelSerializer): ['view', 'add']) } else: - return instance + return serializer.to_representation(instance) field_class = JSonLDPropertyField field_kwargs = {} diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py index a024d1b557988523e8bfbfa42b77ab541d7454b6..332cc39117285eeafdf5ab4d1aad9ffb119e2674 100644 --- a/djangoldp/tests/models.py +++ b/djangoldp/tests/models.py @@ -32,12 +32,15 @@ class JobOffer(Model): def recent_skills(self): return self.skills.filter(date__gte=date.today()) + def some_skill(self): + return self.skills.all().first() + class Meta: anonymous_perms = ['view'] authenticated_perms = ['inherit', 'change', 'add'] owner_perms = ['inherit', 'delete', 'control'] nested_fields = ["skills"] - serializer_fields = ["@id", "title", "skills", "recent_skills", "resources"] + serializer_fields = ["@id", "title", "skills", "recent_skills", "resources", "slug", "some_skill"] container_path = "job-offers/" lookup_field = 'slug' diff --git a/djangoldp/tests/tests_get.py b/djangoldp/tests/tests_get.py index 6d25c1d4049b8f94a294b38e44bccb32b87065ad..485fcd37e417a0f64263c1f087d772a04ef13a5e 100644 --- a/djangoldp/tests/tests_get.py +++ b/djangoldp/tests/tests_get.py @@ -62,3 +62,16 @@ class TestGET(APITestCase): self.assertIn('recent_jobs', response.data) self.assertEqual(response.data['recent_jobs']['@id'], "http://happy-dev.fr/skills/1/recent_jobs/") + def test_get_virtual_field(self): + skill = Skill.objects.create(title="Java", obligatoire="ok", slug="1") + skill2 = Skill.objects.create(title="Java", obligatoire="ok", slug="2") + job = JobOffer.objects.create(title="job", slug="1") + job.skills.add(skill) + job.skills.add(skill2) + job.save() + response = self.client.get('/job-offers/{}/'.format(job.slug), content_type='application/ld+json') + self.assertEqual(response.status_code, 200) + self.assertIn('some_skill', response.data) + self.assertEqual(response.data['some_skill']['@id'], "http://testserver/skills/1/") + + diff --git a/djangoldp/tests/tests_temp.py b/djangoldp/tests/tests_temp.py index 9c806dd3efc06bfe0df6f3f34adbbdf1780bf64a..6896080d1d3f2712c0adf3ac687131f0cd8a561d 100644 --- a/djangoldp/tests/tests_temp.py +++ b/djangoldp/tests/tests_temp.py @@ -1,11 +1,7 @@ -import json - from django.contrib.auth.models import User from django.test import TestCase from rest_framework.test import APIRequestFactory, APIClient -from djangoldp.tests.models import Resource - class TestTemp(TestCase):