diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index 28d1d9b230206945905c8e1e064f96ea25b826a3..1394713b53b9141dd8c01e1ab832dcf0079cf21c 100644 --- a/djangoldp/serializers.py +++ b/djangoldp/serializers.py @@ -69,8 +69,8 @@ class LDListMixin: container_permissions.extend( Model.get_permissions(parent_model, self.context['request'].user, ['view'])) - if self.id == '': - self.id = '{}{}'.format(settings.SITE_URL, Model.resource(parent_model)) + if not self.id.startswith('http'): + self.id = '{}{}{}'.format(settings.SITE_URL, Model.resource(parent_model), self.id) return {'@id': self.id, '@type': 'ldp:Container', 'ldp:contains': super().to_representation(filtered_values), @@ -433,7 +433,10 @@ class LDPSerializer(HyperlinkedModelSerializer): @classmethod def many_init(cls, *args, **kwargs): kwargs['child'] = cls(**kwargs) - return ContainerSerializer(*args, **kwargs) + serializer = ContainerSerializer(*args, **kwargs) + if 'context' in kwargs and getattr(kwargs['context']['view'], 'nested_field', None) is not None: + serializer.id = '{}{}/'.format(serializer.id, kwargs['context']['view'].nested_field) + return serializer def get_value(self, dictionary): try: diff --git a/djangoldp/tests/runner.py b/djangoldp/tests/runner.py index 6a4195f8d53fd04720d883ee0e2551fb043bb4d9..9b83a4fc2f7eaa059ada26a6689aceb6e95d284e 100644 --- a/djangoldp/tests/runner.py +++ b/djangoldp/tests/runner.py @@ -37,7 +37,7 @@ settings.configure(DEBUG=False, } }, AUTHENTICATION_BACKENDS=( - 'django.contrib.auth.backends.ModelBackend', 'guardian.backends.ObjectPermissionBackend'), + 'django.contrib.auth.backends.ModelBackend', 'guardian.backends.ObjectPermissionBackend'), ROOT_URLCONF='djangoldp.urls', DJANGOLDP_PACKAGES=['djangoldp.tests'], INSTALLED_APPS=('django.contrib.auth', @@ -49,7 +49,7 @@ settings.configure(DEBUG=False, 'djangoldp.tests', ), SITE_URL='http://happy-dev.fr', - REST_FRAMEWORK = { + REST_FRAMEWORK={ 'DEFAULT_PAGINATION_CLASS': 'djangoldp.pagination.LDPPagination', 'PAGE_SIZE': 5 }, diff --git a/djangoldp/tests/tests_get.py b/djangoldp/tests/tests_get.py index 485fcd37e417a0f64263c1f087d772a04ef13a5e..fb2c0b9925d0868fa4df0cd4b199be9a8ba58a38 100644 --- a/djangoldp/tests/tests_get.py +++ b/djangoldp/tests/tests_get.py @@ -1,9 +1,8 @@ -import json +from rest_framework.test import APIRequestFactory, APIClient, APITestCase -from django.contrib.auth.models import User from rest_framework.test import APIRequestFactory, APIClient, APITestCase -from djangoldp.tests.models import Post, Task, Invoice, JobOffer, Skill +from djangoldp.tests.models import Post, Invoice, JobOffer, Skill, Batch class TestGET(APITestCase): @@ -27,13 +26,13 @@ class TestGET(APITestCase): response = self.client.get('/posts/', content_type='application/ld+json') self.assertEqual(response.status_code, 200) self.assertIn('permissions', response.data) - self.assertEquals(2, len(response.data['permissions'])) # read and add + self.assertEquals(2, len(response.data['permissions'])) # read and add Invoice.objects.create(title="content") response = self.client.get('/invoices/', content_type='application/ld+json') self.assertEqual(response.status_code, 200) self.assertIn('permissions', response.data) - self.assertEquals(1, len(response.data['permissions'])) # read only + self.assertEquals(1, len(response.data['permissions'])) # read only def test_get_empty_container(self): Post.objects.all().delete() @@ -74,4 +73,9 @@ class TestGET(APITestCase): self.assertIn('some_skill', response.data) self.assertEqual(response.data['some_skill']['@id'], "http://testserver/skills/1/") - + def test_get_nested(self): + invoice = Invoice.objects.create(title="invoice") + batch = Batch.objects.create(invoice=invoice, title="batch") + response = self.client.get('/invoices/{}/batches/'.format(invoice.pk), content_type='application/ld+json') + self.assertEqual(response.status_code, 200) + self.assertEquals(response.data['@id'], 'http://happy-dev.fr/invoices/{}/batches/'.format(invoice.pk)) diff --git a/djangoldp/tests/tests_temp.py b/djangoldp/tests/tests_temp.py index 21c77f6c28783d74a54d6aeb48e034dc350c46df..161703c81abc8562d445f07bd2e04fc503ee5de0 100644 --- a/djangoldp/tests/tests_temp.py +++ b/djangoldp/tests/tests_temp.py @@ -4,7 +4,7 @@ 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, JobOffer +from djangoldp.tests.models import Resource, JobOffer, Invoice, Batch class TestTemp(TestCase):