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

update: Allow non-QuerySet on virtual field

parent ff08cdcc
Pipeline #5182 passed with stage
in 1 minute and 21 seconds
......@@ -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 = {}
......
......@@ -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'
......
......@@ -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/")
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):
......
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