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

Merge branch '169-virtual-field' into 'master'

Resolve "Unable to Serialize one resource from a virtual field"

Closes #169

See merge request !94
parents ff08cdcc a41f7dcc
Pipeline #5186 passed with stage
in 1 minute and 21 seconds
...@@ -269,23 +269,27 @@ class LDPSerializer(HyperlinkedModelSerializer): ...@@ -269,23 +269,27 @@ class LDPSerializer(HyperlinkedModelSerializer):
def build_property_field(self, field_name, model_class): def build_property_field(self, field_name, model_class):
class JSonLDPropertyField(ReadOnlyField): class JSonLDPropertyField(ReadOnlyField):
def to_representation(self, instance): 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): if isinstance(instance, QuerySet):
data = list(instance) 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), return {'@id': '{}{}{}/'.format(settings.SITE_URL, '{}{}/', self.source),
'@type': 'ldp:Container', '@type': 'ldp:Container',
'ldp:contains': [serializer.to_representation(item) if item is not None else None for item 'ldp:contains': [serializer.to_representation(item) if item is not None else None for item
...@@ -294,7 +298,7 @@ class LDPSerializer(HyperlinkedModelSerializer): ...@@ -294,7 +298,7 @@ class LDPSerializer(HyperlinkedModelSerializer):
['view', 'add']) ['view', 'add'])
} }
else: else:
return instance return serializer.to_representation(instance)
field_class = JSonLDPropertyField field_class = JSonLDPropertyField
field_kwargs = {} field_kwargs = {}
......
...@@ -32,12 +32,15 @@ class JobOffer(Model): ...@@ -32,12 +32,15 @@ class JobOffer(Model):
def recent_skills(self): def recent_skills(self):
return self.skills.filter(date__gte=date.today()) return self.skills.filter(date__gte=date.today())
def some_skill(self):
return self.skills.all().first()
class Meta: class Meta:
anonymous_perms = ['view'] anonymous_perms = ['view']
authenticated_perms = ['inherit', 'change', 'add'] authenticated_perms = ['inherit', 'change', 'add']
owner_perms = ['inherit', 'delete', 'control'] owner_perms = ['inherit', 'delete', 'control']
nested_fields = ["skills"] 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/" container_path = "job-offers/"
lookup_field = 'slug' lookup_field = 'slug'
......
...@@ -62,3 +62,16 @@ class TestGET(APITestCase): ...@@ -62,3 +62,16 @@ class TestGET(APITestCase):
self.assertIn('recent_jobs', response.data) self.assertIn('recent_jobs', response.data)
self.assertEqual(response.data['recent_jobs']['@id'], "http://happy-dev.fr/skills/1/recent_jobs/") 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/")
import json
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.test import TestCase from django.test import TestCase
from rest_framework.test import APIRequestFactory, APIClient from rest_framework.test import APIRequestFactory, APIClient
from djangoldp.tests.models import Resource
class TestTemp(TestCase): class TestTemp(TestCase):
......
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