diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index ae8b6b00b494d5ddfcbba20d7dc1603029a32c6e..815be9068ffdc34903d8dae1af4bd06bca64cae6 100644 --- a/djangoldp/serializers.py +++ b/djangoldp/serializers.py @@ -358,7 +358,7 @@ class LDPSerializer(HyperlinkedModelSerializer): else: rel = getattr(instance._meta.model, field_name).rel try: - if rel.related_name == field_name: + if rel.related_model == manager.model: reverse_id = rel.remote_field.attname item[reverse_id] = instance.pk except AttributeError: diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py index dab3dc1b400c0a0d171ba79c49b9ba5c643e1513..13a0aedf31da3efac36fd9c00bb64b4d73923119 100644 --- a/djangoldp/tests/models.py +++ b/djangoldp/tests/models.py @@ -14,10 +14,12 @@ class JobOffer(models.Model): class Thread(models.Model): description = models.CharField(max_length=255, blank=True, null=True) + author_user = models.ForeignKey(settings.AUTH_USER_MODEL) class Message(models.Model): text = models.CharField(max_length=255, blank=True, null=True) thread = models.ForeignKey(Thread, on_delete=models.DO_NOTHING) + author_user = models.ForeignKey(settings.AUTH_USER_MODEL) diff --git a/djangoldp/tests/tests_update.py b/djangoldp/tests/tests_update.py index e87360ddbcb309f75f397ae69fb580c12658af94..7b232b4455e057d97544dac01999657d2d104ca4 100644 --- a/djangoldp/tests/tests_update.py +++ b/djangoldp/tests/tests_update.py @@ -1,3 +1,4 @@ +from django.contrib.auth.models import User from django.test import TestCase from djangoldp.serializers import LDPSerializer @@ -45,28 +46,32 @@ class Serializer(TestCase): job1 = JobOffer.objects.create(title="job test") job1.skills.add(skill) - job = {"@graph": [{"@id": "https://happy-dev.fr/job-offers/{}/".format(job1.pk), - "title": "job test updated", - "skills": { - "ldp:contains": [ - {"@id": "https://happy-dev.fr/skills/{}/".format(skill1.pk)}, - {"@id": "https://happy-dev.fr/skills/{}/".format(skill2.pk)}, - {"@id": "_.123"}, - ]} - }, - { - "@id": "_.123", - "title": "new skill", - "obligatoire": "okay" - }, - { - "@id": "https://happy-dev.fr/skills/{}/".format(skill1.pk), - }, - { - "@id": "https://happy-dev.fr/skills/{}/".format(skill2.pk), - "title": "skill2 UP" - }] - } + job = {"@graph": + [ + { + "@id": "https://happy-dev.fr/job-offers/{}/".format(job1.pk), + "title": "job test updated", + "skills": { + "ldp:contains": [ + {"@id": "https://happy-dev.fr/skills/{}/".format(skill1.pk)}, + {"@id": "https://happy-dev.fr/skills/{}/".format(skill2.pk)}, + {"@id": "_.123"}, + ]} + }, + { + "@id": "_.123", + "title": "new skill", + "obligatoire": "okay" + }, + { + "@id": "https://happy-dev.fr/skills/{}/".format(skill1.pk), + }, + { + "@id": "https://happy-dev.fr/skills/{}/".format(skill2.pk), + "title": "skill2 UP" + } + ] + } meta_args = {'model': JobOffer, 'depth': 1, 'fields': ("@id", "title", "skills")} @@ -91,33 +96,37 @@ class Serializer(TestCase): job1 = JobOffer.objects.create(title="job test") job1.skills.add(skill) - job = {"@graph":[{"@id": "https://happy-dev.fr/job-offers/{}/".format(job1.pk), - "title": "job test updated", - "skills": { - "@id": "https://happy-dev.fr/job-offers/{}/skills/".format(job1.pk) - } - }, - { - "@id": "_.123", - "title": "new skill", - "obligatoire": "okay" - }, - { - "@id": "https://happy-dev.fr/skills/{}/".format(skill1.pk), - }, - { - "@id": "https://happy-dev.fr/skills/{}/".format(skill2.pk), - "title": "skill2 UP" - }, - { - '@id': "https://happy-dev.fr/job-offers/{}/skills/".format(job1.pk), - "ldp:contains": [ - {"@id": "https://happy-dev.fr/skills/{}/".format(skill1.pk)}, - {"@id": "https://happy-dev.fr/skills/{}/".format(skill2.pk)}, - {"@id": "_.123"}, - ] - }] - } + job = {"@graph": + [ + { + "@id": "https://happy-dev.fr/job-offers/{}/".format(job1.pk), + "title": "job test updated", + "skills": { + "@id": "https://happy-dev.fr/job-offers/{}/skills/".format(job1.pk) + } + }, + { + "@id": "_.123", + "title": "new skill", + "obligatoire": "okay" + }, + { + "@id": "https://happy-dev.fr/skills/{}/".format(skill1.pk), + }, + { + "@id": "https://happy-dev.fr/skills/{}/".format(skill2.pk), + "title": "skill2 UP" + }, + { + '@id': "https://happy-dev.fr/job-offers/{}/skills/".format(job1.pk), + "ldp:contains": [ + {"@id": "https://happy-dev.fr/skills/{}/".format(skill1.pk)}, + {"@id": "https://happy-dev.fr/skills/{}/".format(skill2.pk)}, + {"@id": "_.123"}, + ] + } + ] + } meta_args = {'model': JobOffer, 'depth': 1, 'fields': ("@id", "title", "skills")} @@ -136,30 +145,96 @@ class Serializer(TestCase): self.assertEquals(skills[2].title, "skill2 UP") # title updated def test_update_list_with_reverse_relation(self): - thread = Thread.objects.create(description="Thread 1") - message1 = Message.objects.create(text="Message 1", thread=thread) - message2 = Message.objects.create(text="Message 2", thread=thread) + user1 = User.objects.create() + thread = Thread.objects.create(description="Thread 1", author_user=user1) + message1 = Message.objects.create(text="Message 1", thread=thread, author_user=user1) + message2 = Message.objects.create(text="Message 2", thread=thread, author_user=user1) + + json = {"@graph": [ + { + "@id": "https://happy-dev.fr/messages/{}/".format(message1.pk), + "text": "Message 1 UP" + }, + { + "@id": "https://happy-dev.fr/messages/{}/".format(message2.pk), + "text": "Message 2 UP" + }, + { + '@id': "https://happy-dev.fr/threads/{}/".format(thread.pk), + 'description': "Thread 1 UP", + "message_set": [ + {"@id": "https://happy-dev.fr/messages/{}/".format(message1.pk)}, + {"@id": "https://happy-dev.fr/messages/{}/".format(message2.pk)}, + ] + } + ] + } + meta_args = {'model': Thread, 'depth': 1, 'fields': ("@id", "description", "message_set")} + + meta_class = type('Meta', (), meta_args) + serializer_class = type(LDPSerializer)('ThreadSerializer', (LDPSerializer,), {'Meta': meta_class}) + serializer = serializer_class(data=json, instance=thread) + serializer.is_valid() + result = serializer.save() + + messages = result.message_set.all().order_by('text') + + self.assertEquals(result.description, "Thread 1 UP") + self.assertIs(result.message_set.count(), 2) + self.assertEquals(messages[0].text, "Message 1 UP") + self.assertEquals(messages[1].text, "Message 2 UP") + + def test_add_new_element_with_foreign_key_id(self): + user1 = User.objects.create() + thread = Thread.objects.create(description="Thread 1", author_user=user1) + message1 = Message.objects.create(text="Message 1", thread=thread, author_user=user1) + message2 = Message.objects.create(text="Message 2", thread=thread, author_user=user1) json = {"@graph": [ - {"@id": "https://happy-dev.fr/messages/{}/".format(message1.pk), - "text": "Message 1 UP" + { + "@id": "https://happy-dev.fr/messages/{}/".format(message1.pk), + "text": "Message 1 UP", + "author_user": { + '@id': "https://happy-dev.fr/users/{}/".format(user1.pk) + } + }, + { + "@id": "https://happy-dev.fr/messages/{}/".format(message2.pk), + "text": "Message 2 UP", + "author_user": { + '@id': "https://happy-dev.fr/users/{}/".format(user1.pk) + } + }, + { + "@id": "_:b1", + "text": "Message 3 NEW", + "author_user": { + '@id': "https://happy-dev.fr/users/{}/".format(user1.pk) + } + }, + { + '@id': "https://happy-dev.fr/threads/{}".format(thread.pk), + "author_user": { + '@id': "https://happy-dev.fr/users/{}/".format(user1.pk) }, - {"@id": "https://happy-dev.fr/messages/{}/".format(message2.pk), - "text": "Message 2 UP" - }, - { - '@id': "https://happy-dev.fr/threads/{}/".format(thread.pk), - 'description': "Thread 1 UP", - "message_set": [ - {"@id": "https://happy-dev.fr/messages/{}/".format(message1.pk)}, - {"@id": "https://happy-dev.fr/messages/{}/".format(message2.pk)}, - ] - } + 'description': "Thread 1 UP", + 'message_set': { + "@id": "https://happy-dev.fr/threads/{}/message_set".format(thread.pk) + } + }, + { + '@id': "https://happy-dev.fr/threads/{}/message_set".format(thread.pk), + "ldp:contains": [ + {"@id": "https://happy-dev.fr/messages/{}/".format(message1.pk)}, + {"@id": "https://happy-dev.fr/messages/{}/".format(message2.pk)}, + {"@id": "_:b1"} ] - } + } + ] + } - meta_args = {'model': Thread, 'depth': 1, 'fields': ("@id", "description", "message_set" )} + meta_args = {'model': Thread, 'depth': 1, 'fields': ("@id", "description", "message_set")} meta_class = type('Meta', (), meta_args) serializer_class = type(LDPSerializer)('ThreadSerializer', (LDPSerializer,), {'Meta': meta_class}) @@ -170,6 +245,7 @@ class Serializer(TestCase): messages = result.message_set.all().order_by('text') self.assertEquals(result.description, "Thread 1 UP") - self.assertIs(result.message_set.count(), 2) + self.assertIs(result.message_set.count(), 3) self.assertEquals(messages[0].text, "Message 1 UP") self.assertEquals(messages[1].text, "Message 2 UP") + self.assertEquals(messages[2].text, "Message 3 NEW") diff --git a/djangoldp/tests/urls.py b/djangoldp/tests/urls.py index eeb96677bdb788fca6f79c753647b83f8e62fbd2..0066129ba1d57ef46610773bbcc237d840232f30 100644 --- a/djangoldp/tests/urls.py +++ b/djangoldp/tests/urls.py @@ -1,3 +1,5 @@ +from django.conf import settings + from djangoldp.tests.models import Skill, JobOffer, Message, Thread from djangoldp.views import LDPViewSet from django.conf.urls import url @@ -8,4 +10,5 @@ urlpatterns = [ url(r'^job-offers/', LDPViewSet.urls(model=JobOffer, nested_fields=["skills"], permission_classes=())), url(r'^messages/', LDPViewSet.urls(model=Message, permission_classes=[], fields=["@id", "text"], nested_fields=[])), url(r'^threads/', LDPViewSet.urls(model=Thread, nested_fields=["message_set"], permission_classes=())), + url(r'^users/', LDPViewSet.urls(model=settings.AUTH_USER_MODEL, permission_classes=[])), ] \ No newline at end of file