Commit d8e36019 authored by Jean-Baptiste's avatar Jean-Baptiste

update: fix many-to-many on container POST

parent 8aa68d81
Pipeline #5022 passed with stage
in 1 minute and 21 seconds
......@@ -460,8 +460,7 @@ class LDPSerializer(HyperlinkedModelSerializer):
info = model_meta.get_field_info(model_class)
many_to_many = {}
for field_name, relation_info in info.relations.items():
if relation_info.to_many and relation_info.reverse and not (
field_name in validated_data) and not field_name is None:
if relation_info.to_many and relation_info.reverse and not field_name is None:
rel = getattr(instance._meta.model, field_name).rel
if rel.name in validated_data:
related = validated_data[rel.name]
......@@ -485,9 +484,19 @@ class LDPSerializer(HyperlinkedModelSerializer):
nested_list_fields_name = list(filter(lambda key: isinstance(validated_data[key], list), validated_data))
for field_name in nested_list_fields_name:
nested_fields.append((field_name, validated_data.pop(field_name)))
info = model_meta.get_field_info(model)
many_to_many = []
for field_name, relation_info in info.relations.items():
if relation_info.to_many and relation_info.reverse and (
field_name in validated_data) and not field_name is None:
many_to_many.append((field_name, validated_data.pop(field_name)))
validated_data = self.remove_empty_value(validated_data)
instance = model.objects.create(**validated_data)
for field_name, value in many_to_many:
validated_data[field_name] = value
self.save_or_update_nested_list(instance, nested_fields)
return instance
......
......@@ -37,7 +37,7 @@ class JobOffer(Model):
authenticated_perms = ['inherit', 'change', 'add']
owner_perms = ['inherit', 'delete', 'control']
nested_fields = ["skills"]
serializer_fields = ["@id", "title", "skills", "recent_skills"]
serializer_fields = ["@id", "title", "skills", "recent_skills", "resources"]
container_path = "job-offers/"
lookup_field = 'slug'
......@@ -53,6 +53,17 @@ class Conversation(models.Model):
owner_perms = ['inherit', 'change', 'delete', 'control']
class Resource(Model):
joboffers = models.ManyToManyField(JobOffer, blank=True, related_name='resources')
class Meta:
anonymous_perms = ['view', 'add', 'delete', 'add', 'change', 'control']
authenticated_perms = ['inherit']
owner_perms = ['inherit']
serializer_fields = ["@id", "joboffers"]
nested_fields = ["joboffers"]
class UserProfile(Model):
description = models.CharField(max_length=255, blank=True, null=True)
user = models.OneToOneField(settings.AUTH_USER_MODEL)
......
......@@ -3,7 +3,7 @@ from rest_framework.utils import json
from djangoldp.models import Model
from djangoldp.serializers import LDPSerializer
from djangoldp.tests.models import Skill, JobOffer, Invoice, LDPDummy
from djangoldp.tests.models import Skill, JobOffer, Invoice, LDPDummy, Resource
class Save(TestCase):
......@@ -255,6 +255,18 @@ class Save(TestCase):
content_type='application/ld+json')
self.assertEqual(response.status_code, 201)
def test_nested_container(self):
resource = Resource.objects.create()
body = {
'title': "new job",
}
response = self.client.post('/resources/{}/joboffers/'.format(resource.pk),
data=json.dumps(body),
content_type='application/ld+json')
self.assertEqual(response.status_code, 201)
self.assertEqual(response.data['resources']['ldp:contains'][0]['@id'], "http://testserver/resources/{}/".format(resource.pk))
def test_embedded_context_2(self):
body = {
'@id': "./",
......
......@@ -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 Skill, JobOffer, Post
from djangoldp.tests.models import Resource
class TestTemp(TestCase):
......@@ -16,6 +16,3 @@ class TestTemp(TestCase):
def tearDown(self):
pass
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