From a783dd8caaa41b4583a52430c8e3a34561ed1153 Mon Sep 17 00:00:00 2001
From: Jean-Baptiste <bleme@pm.me>
Date: Mon, 9 Sep 2019 11:14:28 +0200
Subject: [PATCH] update: fix @id on nested GET

---
 djangoldp/serializers.py      |  9 ++++++---
 djangoldp/tests/runner.py     |  4 ++--
 djangoldp/tests/tests_get.py  | 16 ++++++++++------
 djangoldp/tests/tests_temp.py |  2 +-
 4 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py
index 28d1d9b2..1394713b 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 6a4195f8..9b83a4fc 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 485fcd37..fb2c0b99 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 21c77f6c..161703c8 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):
-- 
GitLab