Commit 16e5df94 authored by Jean-Baptiste's avatar Jean-Baptiste

update: fix PUT on m2M relations

parent f8b9306e
Pipeline #5187 passed with stage
in 1 minute and 22 seconds
......@@ -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
......
......@@ -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:
......
......@@ -61,16 +61,16 @@ from django.test.runner import DiscoverRunner
test_runner = DiscoverRunner(verbosity=1)
failures = test_runner.run_tests([
# 'djangoldp.tests.tests_ldp_model',
# 'djangoldp.tests.tests_save',
# 'djangoldp.tests.tests_user_permissions',
# 'djangoldp.tests.tests_anonymous_permissions',
# 'djangoldp.tests.tests_update',
# 'djangoldp.tests.tests_auto_author',
# 'djangoldp.tests.tests_get',
# 'djangoldp.tests.tests_delete',
# 'djangoldp.tests.tests_sources',
# 'djangoldp.tests.tests_pagination',
'djangoldp.tests.tests_ldp_model',
'djangoldp.tests.tests_save',
'djangoldp.tests.tests_user_permissions',
'djangoldp.tests.tests_anonymous_permissions',
'djangoldp.tests.tests_update',
'djangoldp.tests.tests_auto_author',
'djangoldp.tests.tests_get',
'djangoldp.tests.tests_delete',
'djangoldp.tests.tests_sources',
'djangoldp.tests.tests_pagination',
'djangoldp.tests.tests_temp'
])
......
......@@ -17,60 +17,3 @@ class TestTemp(TestCase):
def tearDown(self):
pass
# 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': '/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/aaa/")
# self.assertEqual(response.data['joboffers']['ldp:contains'][0]['title'], "new job")
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'], "job")
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)},
]
}
}
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'], "job")
......@@ -396,6 +396,46 @@ class Update(TestCase):
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 = {
......@@ -413,3 +453,27 @@ class Update(TestCase):
"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")
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