diff --git a/djangoldp/models.py b/djangoldp/models.py index 7507c0bf71d37ee66f8a545e88ec289dc7b9da35..778c5e7d39b14bd9357e0645dc6eeff6a328b3a1 100644 --- a/djangoldp/models.py +++ b/djangoldp/models.py @@ -2,6 +2,7 @@ from django.contrib.auth.models import User from django.db import models from django.db.models.base import ModelBase from django.urls import get_resolver +from django.utils.datastructures import MultiValueDict, MultiValueDictKeyError from django.utils.decorators import classonlymethod from djangoldp.permissions import LDPPermissions @@ -53,7 +54,10 @@ class Model(models.Model): else: object_name = instance_or_model._meta.object_name.lower() view_name = '{}-detail'.format(object_name) - slug_field = '/{}'.format(get_resolver().reverse_dict[view_name][0][0][1][0]) + try : + slug_field = '/{}'.format(get_resolver().reverse_dict[view_name][0][0][1][0]) + except MultiValueDictKeyError: + slug_field = Model.get_meta(instance_or_model, 'lookup_field', 'pk') if slug_field.startswith('/'): slug_field = slug_field[1:] return slug_field diff --git a/djangoldp/serializers.py b/djangoldp/serializers.py index ba22a0398c32b9ff4b8317ffa231c01318c426c5..4a3990f35a901132c0d14f90e2af8734a703420c 100644 --- a/djangoldp/serializers.py +++ b/djangoldp/serializers.py @@ -567,7 +567,7 @@ class LDPSerializer(HyperlinkedModelSerializer): def save_or_update_nested_list(self, instance, nested_fields): for (field_name, data) in nested_fields: manager = getattr(instance, field_name) - slug_field = Model.slug_field(instance) + slug_field = Model.slug_field(manager.model) try: item_pk_to_keep = list(map(lambda e: e[slug_field], filter(lambda x: slug_field in x, data))) except TypeError: @@ -587,8 +587,11 @@ class LDPSerializer(HyperlinkedModelSerializer): saved_item = item elif slug_field in item: kwargs = {slug_field: item[slug_field]} - old_obj = manager.model.objects.get(**kwargs) - saved_item = self.update(instance=old_obj, validated_data=item) + try: + old_obj = manager.model.objects.get(**kwargs) + saved_item = self.update(instance=old_obj, validated_data=item) + except manager.model.DoesNotExist: + saved_item = self.internal_create(validated_data=item, model=manager.model) else: rel = getattr(instance._meta.model, field_name).rel try: diff --git a/djangoldp/tests/models.py b/djangoldp/tests/models.py index 332cc39117285eeafdf5ab4d1aad9ffb119e2674..fe6de31f645f445108295ef3f325f6ea05ea8768 100644 --- a/djangoldp/tests/models.py +++ b/djangoldp/tests/models.py @@ -65,6 +65,7 @@ class Resource(Model): owner_perms = ['inherit'] serializer_fields = ["@id", "joboffers"] nested_fields = ["joboffers"] + depth = 1 class UserProfile(Model): diff --git a/djangoldp/tests/runner.py b/djangoldp/tests/runner.py index 2dd377fa42587522f968a7be6018ae59f476ee70..6a4195f8d53fd04720d883ee0e2551fb043bb4d9 100644 --- a/djangoldp/tests/runner.py +++ b/djangoldp/tests/runner.py @@ -71,7 +71,7 @@ failures = test_runner.run_tests([ 'djangoldp.tests.tests_delete', 'djangoldp.tests.tests_sources', 'djangoldp.tests.tests_pagination', - # 'djangoldp.tests.tests_temp' + 'djangoldp.tests.tests_temp' ]) if failures: diff --git a/djangoldp/tests/tests_temp.py b/djangoldp/tests/tests_temp.py index 6896080d1d3f2712c0adf3ac687131f0cd8a561d..21c77f6c28783d74a54d6aeb48e034dc350c46df 100644 --- a/djangoldp/tests/tests_temp.py +++ b/djangoldp/tests/tests_temp.py @@ -1,7 +1,11 @@ +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, JobOffer + class TestTemp(TestCase): @@ -12,3 +16,4 @@ class TestTemp(TestCase): def tearDown(self): pass + diff --git a/djangoldp/tests/tests_update.py b/djangoldp/tests/tests_update.py index 292b0bafbb1ad3b8c17abd7e4b4e189b9a5c8dfc..431db723282d3467746d77162ca92c2764ac843b 100644 --- a/djangoldp/tests/tests_update.py +++ b/djangoldp/tests/tests_update.py @@ -4,7 +4,7 @@ from rest_framework.test import APIRequestFactory, APIClient from rest_framework.utils import json from djangoldp.serializers import LDPSerializer -from djangoldp.tests.models import Post, UserProfile +from djangoldp.tests.models import Post, UserProfile, Resource from djangoldp.tests.models import Skill, JobOffer, Conversation, Message @@ -395,3 +395,85 @@ class Update(TestCase): content_type='application/ld+json') self.assertEqual(response.status_code, 200) self.assertEqual(response.data['peer_user'], None) + + def test_m2m_new_link(self): + resource = Resource.objects.create() + job = JobOffer.objects.create(title="first title", slug="job") + body = { + 'http://happy-dev.fr/owl/#joboffers': { + '@id': 'http://testserver/job-offers/{}/'.format(job.slug), + } + } + + response = self.client.put('/resources/{}/'.format(resource.pk), + data=json.dumps(body), + content_type='application/ld+json') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data['joboffers']['ldp:contains'][0]['@id'], + "http://testserver/job-offers/{}/".format(job.slug)) + self.assertEqual(response.data['joboffers']['ldp:contains'][0]['title'], "first title") + + def test_m2m_new_link_bis(self): + resource = Resource.objects.create() + job = JobOffer.objects.create(title="first title", slug="job") + body = { + 'http://happy-dev.fr/owl/#joboffers': + { + '@id': "http://testserver/resources/{}/joboffers/".format(resource.pk), + 'ldp:contains': [ + {'@id': 'http://testserver/job-offers/{}/'.format(job.slug), + 'http://happy-dev.fr/owl/#title': "new job", + }, + ] + } + } + + response = self.client.put('/resources/{}/'.format(resource.pk), + data=json.dumps(body), + content_type='application/ld+json') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data['joboffers']['ldp:contains'][0]['@id'], + "http://testserver/job-offers/{}/".format(job.slug)) + self.assertEqual(response.data['joboffers']['ldp:contains'][0]['title'], "new job") + + def test_m2m_new_link_embedded(self): + resource = Resource.objects.create() + body = { + 'http://happy-dev.fr/owl/#joboffers': { + 'http://happy-dev.fr/owl/#slug': 'aaa', + 'http://happy-dev.fr/owl/#title': "new job", + } + } + + response = self.client.put('/resources/{}/'.format(resource.pk), + data=json.dumps(body), + content_type='application/ld+json') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data['joboffers']['ldp:contains'][0]['@id'], + "http://testserver/job-offers/aaa/") + self.assertEqual(response.data['joboffers']['ldp:contains'][0]['title'], "new job") + + def test_m2m_existing_link(self): + resource = Resource.objects.create() + job = JobOffer.objects.create(title="first title", slug="job") + resource.joboffers.add(job) + resource.save() + body = { + 'http://happy-dev.fr/owl/#joboffers': { + # '@id': "http://testserver/resources/{}/joboffers/".format(resource.pk), + 'ldp:contains': [ + { + '@id': 'http://testserver/job-offers/{}/'.format(job.slug), + 'http://happy-dev.fr/owl/#title': "new job", + } + ] + } + } + + response = self.client.put('/resources/{}/'.format(resource.pk), + data=json.dumps(body), + content_type='application/ld+json') + self.assertEqual(response.status_code, 200) + self.assertEqual(response.data['joboffers']['ldp:contains'][0]['@id'], + "http://testserver/job-offers/{}/".format(job.slug)) + self.assertEqual(response.data['joboffers']['ldp:contains'][0]['title'], "new job")